LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 02-23-2002, 12:42 PM   #1
Jon-
Member
 
Registered: Dec 2001
Distribution: RH5.2/6.2/7.0/7.1/7.2/9/ES21/ES31
Posts: 91

Rep: Reputation: 15
Fastest way in perl to reverse dns lookup


Hey all,

I'm trying to write a perl script that can reverse lookup 250K+ IP addresses.

I tried to use Gethostbyname. That seemed to work -- it flew through the entries -- thousands/min. But, as soon as it got stuck on a IP, it would wait an insanely long time before moving onto the next entry.

So, I switched to Net::DNS. (<--not sure how to dump the smiley here...heh... that's a D) This worked -- I set the timeout to 5 seconds. But, it was way slower, by like a factor of 10+. I guess this is because it's opening a seperate socket for each connection? So, it took 12+ hours to lookup about 220K IPs.

Anyone know a better way to do this? I thought I might just modify the Gethostbyname code to allow a timeout (or, probably just change it). But, not sure how Perl is implementing that, and what else it would affect.

Thanks for any feedback :)

Jon

Last edited by Jon-; 02-23-2002 at 12:48 PM.
 
Old 02-23-2002, 01:27 PM   #2
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,600

Rep: Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083
There is a checkbox for disabling smiles for each post. Something like this should work for you:

Code:
use constant TIMEOUT => 2;
$SIG{ALRM} = sub {die "timeout"};

while (<>) { s/^(\S+)/lookup($1)/e; } 
continue { print; }

sub lookup {
    my $ip = shift;
    return $ip unless $ip=~/\d+\.\d+\.\d+\.\d+/;
    unless (exists $CACHE{$ip}) {
        my @h = eval <<'END';
        alarm(TIMEOUT);
        my @i = gethostbyaddr(pack('C4',split('\.',$ip)),2);
        alarm(0);
        @i;
END
    $CACHE{$ip} = $h[0] || undef;
    }
    return $CACHE{$ip} || $ip;
}
 
Old 02-24-2002, 12:16 AM   #3
Jon-
Member
 
Registered: Dec 2001
Distribution: RH5.2/6.2/7.0/7.1/7.2/9/ES21/ES31
Posts: 91

Original Poster
Rep: Reputation: 15
Hey Jeremy,

I totally forgot about alarm... that's the ticket... and, caching stuff -- good idea..

I'll give this a shot...thanks again!

Jon
 
Old 05-28-2008, 02:37 AM   #4
raghupathi
LQ Newbie
 
Registered: May 2008
Posts: 1

Rep: Reputation: 0
In which machine we have run this script

Whether it shows the reverse lookup for dns with Increment Ipaddress or not.

Last edited by raghupathi; 05-28-2008 at 07:03 AM.
 
Old 07-20-2016, 06:47 AM   #5
cnd
LQ Newbie
 
Registered: May 2005
Posts: 10

Rep: Reputation: 0
max possible speed

DNS lookups are short UDP packets - the fastest possible way to do those lookups would be to run tcpdump to capture the replies, then write a short perl script to pump every DNS query packet you want out all at once (Net:NS helps you construct that).
to make it even faster, you could spread the load and use multiple different servers (eg: send some requests to 8.8.8.8 and others to 8.8.4.4 etc).

The entire run should take just 10 or 20 seconds this way (depends a bit on your outgoing traffic bandwidth)
 
Old 07-20-2016, 08:37 AM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by Jon- View Post
So, I switched to Net::DNS. (<--not sure how to dump the smiley here...heh... that's a D)
Code:
[noparse]Net::DNS[/noparse]
Works in quotes too. but usage is a bit tedious after multiple replies.
Or as Jeremy said select "Disable smilies in text" in Additional Options of Reply to Thread Window.
 
Old 07-20-2016, 10:00 AM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
use threads?
 
Old 07-20-2016, 12:50 PM   #8
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 13,600

Rep: Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083Reputation: 4083
Closing this thread, as it's over a decade old.

--jeremy
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DNS Reverse lookup problem pazvant Linux - Networking 3 10-10-2005 06:36 AM
Ident / Reverse DNS Lookup maq Linux - Networking 1 03-31-2005 05:59 PM
DNS reverse lookup problems... pdmackenzie Linux - Networking 2 09-19-2003 05:00 PM
Reverse DNS lookup, or any way to contact an IP.. MasterC Linux - Networking 6 02-21-2003 03:34 AM
reverse DNS lookup phil1076 Linux - General 1 01-22-2002 03:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration