Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Want to write a script to do the batch conversion from domain name to IP.

Reply
Thread Tools

Want to write a script to do the batch conversion from domain name to IP.

 
 
Hongyi Zhao
Guest
Posts: n/a
 
      01-30-2009
Hi all,

Suppose I've the entries like the following in my file:

------------------
116.52.155.237:80
ip-72-55-191-6.static.privatedns.com:3128
222.124.135.40:80
217.151.231.34:3128
202.106.121.134:80
211.161.197.182:80
hpc.be.itu.edu.tr:80
static3-117-183.worldinternetworkcorporation.com:80
------------------

Now, I want to convert the domain name to IP by using a perl script,
any hints on this?

--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      01-30-2009

Hongyi Zhao wrote:
> Hi all,
>
> Suppose I've the entries like the following in my file:
>
> ------------------
> 116.52.155.237:80
> ip-72-55-191-6.static.privatedns.com:3128
> 222.124.135.40:80
> 217.151.231.34:3128
> 202.106.121.134:80
> 211.161.197.182:80
> hpc.be.itu.edu.tr:80
> static3-117-183.worldinternetworkcorporation.com:80
> ------------------
>
> Now, I want to convert the domain name to IP by using a perl script,
> any hints on this?
>


I'd use split /:/ to separate the address from the port number.

I'd use a regular expression to check if the address looks like an
ip-address. If your are confident of your data you can be a bit sloppy
and use something simple like m/^[0-9.]+$/ Otherwise read this
http://www.perlmonks.org/?node_id=221512

For addresses that look like a hostname+domain I'd use gethostbyname

--
RGB
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      01-30-2009
Hongyi Zhao wrote:
>
> Suppose I've the entries like the following in my file:
>
> ------------------
> 116.52.155.237:80
> ip-72-55-191-6.static.privatedns.com:3128
> 222.124.135.40:80
> 217.151.231.34:3128
> 202.106.121.134:80
> 211.161.197.182:80
> hpc.be.itu.edu.tr:80
> static3-117-183.worldinternetworkcorporation.com:80
> ------------------
>
> Now, I want to convert the domain name to IP by using a perl script,
> any hints on this?


$ echo "
116.52.155.237:80
ip-72-55-191-6.static.privatedns.com:3128
222.124.135.40:80
217.151.231.34:3128
202.106.121.134:80
211.161.197.182:80
hpc.be.itu.edu.tr:80
static3-117-183.worldinternetworkcorporation.com:80
" | perl -MSocket -lne'
my $address = ( split /:/ )[ 0 ] or next;
my $number = inet_aton $address;
my $ip = inet_ntoa $number;
print "$address -> $ip";
'
116.52.155.237 -> 116.52.155.237
ip-72-55-191-6.static.privatedns.com -> 72.55.191.6
222.124.135.40 -> 222.124.135.40
217.151.231.34 -> 217.151.231.34
202.106.121.134 -> 202.106.121.134
211.161.197.182 -> 211.161.197.182
hpc.be.itu.edu.tr -> 160.75.90.69
static3-117-183.worldinternetworkcorporation.com -> 203.145.117.183



John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
 
Reply With Quote
 
Hongyi Zhao
Guest
Posts: n/a
 
      01-30-2009
On Fri, 30 Jan 2009 03:04:18 -0800, "John W. Krahn"
<> wrote:

>Hongyi Zhao wrote:
>>
>> Suppose I've the entries like the following in my file:
>>
>> ------------------
>> 116.52.155.237:80
>> ip-72-55-191-6.static.privatedns.com:3128
>> 222.124.135.40:80
>> 217.151.231.34:3128
>> 202.106.121.134:80
>> 211.161.197.182:80
>> hpc.be.itu.edu.tr:80
>> static3-117-183.worldinternetworkcorporation.com:80
>> ------------------
>>
>> Now, I want to convert the domain name to IP by using a perl script,
>> any hints on this?

>
>$ echo "
>116.52.155.237:80
>ip-72-55-191-6.static.privatedns.com:3128
>222.124.135.40:80
>217.151.231.34:3128
>202.106.121.134:80
>211.161.197.182:80
>hpc.be.itu.edu.tr:80
>static3-117-183.worldinternetworkcorporation.com:80
>" | perl -MSocket -lne'
>my $address = ( split /:/ )[ 0 ] or next;
>my $number = inet_aton $address;
>my $ip = inet_ntoa $number;
>print "$address -> $ip";
>'
>116.52.155.237 -> 116.52.155.237
>ip-72-55-191-6.static.privatedns.com -> 72.55.191.6
>222.124.135.40 -> 222.124.135.40
>217.151.231.34 -> 217.151.231.34
>202.106.121.134 -> 202.106.121.134
>211.161.197.182 -> 211.161.197.182
>hpc.be.itu.edu.tr -> 160.75.90.69
>static3-117-183.worldinternetworkcorporation.com -> 203.145.117.183
>
>
>
>John


Very good, thanks a lot, it does the trick.

--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
 
Reply With Quote
 
Hongyi Zhao
Guest
Posts: n/a
 
      01-30-2009
On Fri, 30 Jan 2009 03:04:18 -0800, "John W. Krahn"
<> wrote:
>$ echo "
>116.52.155.237:80
>ip-72-55-191-6.static.privatedns.com:3128
>222.124.135.40:80
>217.151.231.34:3128
>202.106.121.134:80
>211.161.197.182:80
>hpc.be.itu.edu.tr:80
>static3-117-183.worldinternetworkcorporation.com:80
>" | perl -MSocket -lne'
>my $address = ( split /:/ )[ 0 ] or next;
>my $number = inet_aton $address;
>my $ip = inet_ntoa $number;
>print "$address -> $ip";
>'
>116.52.155.237 -> 116.52.155.237
>ip-72-55-191-6.static.privatedns.com -> 72.55.191.6
>222.124.135.40 -> 222.124.135.40
>217.151.231.34 -> 217.151.231.34
>202.106.121.134 -> 202.106.121.134
>211.161.197.182 -> 211.161.197.182
>hpc.be.itu.edu.tr -> 160.75.90.69
>static3-117-183.worldinternetworkcorporation.com -> 203.145.117.183


In my case, I also want the port number be preserved in the result,
i.e.,

ip-72-55-191-6.static.privatedns.com:3128

should be converted into the following:

72.55.191.6:3128

What revision should be done on the above code to achieve this aim?

--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
 
Reply With Quote
 
Hongyi Zhao
Guest
Posts: n/a
 
      01-30-2009
On Fri, 30 Jan 2009 03:04:18 -0800, "John W. Krahn"
<> wrote:
>$ echo "
>116.52.155.237:80
>ip-72-55-191-6.static.privatedns.com:3128
>222.124.135.40:80
>217.151.231.34:3128
>202.106.121.134:80
>211.161.197.182:80
>hpc.be.itu.edu.tr:80
>static3-117-183.worldinternetworkcorporation.com:80
>" | perl -MSocket -lne'
>my $address = ( split /:/ )[ 0 ] or next;
>my $number = inet_aton $address;
>my $ip = inet_ntoa $number;
>print "$address -> $ip";
>'
>116.52.155.237 -> 116.52.155.237
>ip-72-55-191-6.static.privatedns.com -> 72.55.191.6
>222.124.135.40 -> 222.124.135.40
>217.151.231.34 -> 217.151.231.34
>202.106.121.134 -> 202.106.121.134
>211.161.197.182 -> 211.161.197.182
>hpc.be.itu.edu.tr -> 160.75.90.69
>static3-117-183.worldinternetworkcorporation.com -> 203.145.117.183


Based on your code, I use the following lines in my case:

#!/usr/bin/perl
cat mydomain.txt | perl -MSocket -lne'
my $address = ( split /:/ )[ 0 ] or next;
my $number = inet_aton $address;
my $ip = inet_ntoa $number;
print "$address -> $ip";
'
Where, mydomain.txt include all of the domain names and IP addresses
which I want to deal with. Then, I meet the error like this:

Bad name after lne' at ./11.pl line 2.

On the other hand, if I remove the first line, i.e.,

#!/usr/bin/perl

it will works.

Why?

--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
 
Reply With Quote
 
Hongyi Zhao
Guest
Posts: n/a
 
      01-30-2009
On Fri, 30 Jan 2009 19:33:35 +0800, Hongyi Zhao
<> wrote:
>In my case, I also want the port number be preserved in the result,
>i.e.,
>
>ip-72-55-191-6.static.privatedns.com:3128
>
>should be converted into the following:
>
>72.55.191.6:3128
>
>What revision should be done on the above code to achieve this aim?


I've sorted it out by using the following code snippet:

cat cybersyndrome.pla5.txt | perl -MSocket -lne'
my $address = ( split /:/ )[ 0 ] or next;
my $port = ( split /:/ )[ 1 ] or next;
my $number = inet_aton $address;
my $ip = inet_ntoa $number;
print "$address:$port -> $ip:$port";
'

Thanks again.

--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-30-2009
Hongyi Zhao <> wrote:
>cat cybersyndrome.pla5.txt | perl -MSocket -lne'


Of course this has nothing to do with Perl, nevertheless it's useless
use of cat, see http://partmaps.org/era/unix/award.html

jue
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-30-2009
Hongyi Zhao wrote:
> On Fri, 30 Jan 2009 03:04:18 -0800, "John W. Krahn"
> <> wrote:
>> $ echo "
>> 116.52.155.237:80
>> ip-72-55-191-6.static.privatedns.com:3128
>> 222.124.135.40:80
>> 217.151.231.34:3128
>> 202.106.121.134:80
>> 211.161.197.182:80
>> hpc.be.itu.edu.tr:80
>> static3-117-183.worldinternetworkcorporation.com:80
>> " | perl -MSocket -lne'
>> my $address = ( split /:/ )[ 0 ] or next;
>> my $number = inet_aton $address;
>> my $ip = inet_ntoa $number;
>> print "$address -> $ip";
>> '
>> 116.52.155.237 -> 116.52.155.237
>> ip-72-55-191-6.static.privatedns.com -> 72.55.191.6
>> 222.124.135.40 -> 222.124.135.40
>> 217.151.231.34 -> 217.151.231.34
>> 202.106.121.134 -> 202.106.121.134
>> 211.161.197.182 -> 211.161.197.182
>> hpc.be.itu.edu.tr -> 160.75.90.69
>> static3-117-183.worldinternetworkcorporation.com -> 203.145.117.183

>
> In my case, I also want the port number be preserved in the result,
> i.e.,
>
> ip-72-55-191-6.static.privatedns.com:3128
>
> should be converted into the following:
>
> 72.55.191.6:3128
>
> What revision should be done on the above code to achieve this aim?




perl -MSocket -lne'
my ( $address, $port ) = split /:/ or next;
my $number = inet_aton $address;
my $ip = inet_ntoa $number;
print "$address -> $ip:$port";
'




John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-30-2009
Hongyi Zhao wrote:
> On Fri, 30 Jan 2009 03:04:18 -0800, "John W. Krahn"
> <> wrote:
>> $ echo "
>> 116.52.155.237:80
>> ip-72-55-191-6.static.privatedns.com:3128
>> 222.124.135.40:80
>> 217.151.231.34:3128
>> 202.106.121.134:80
>> 211.161.197.182:80
>> hpc.be.itu.edu.tr:80
>> static3-117-183.worldinternetworkcorporation.com:80
>> " | perl -MSocket -lne'
>> my $address = ( split /:/ )[ 0 ] or next;
>> my $number = inet_aton $address;
>> my $ip = inet_ntoa $number;
>> print "$address -> $ip";
>> '
>> 116.52.155.237 -> 116.52.155.237
>> ip-72-55-191-6.static.privatedns.com -> 72.55.191.6
>> 222.124.135.40 -> 222.124.135.40
>> 217.151.231.34 -> 217.151.231.34
>> 202.106.121.134 -> 202.106.121.134
>> 211.161.197.182 -> 211.161.197.182
>> hpc.be.itu.edu.tr -> 160.75.90.69
>> static3-117-183.worldinternetworkcorporation.com -> 203.145.117.183

>
> Based on your code, I use the following lines in my case:
>
> #!/usr/bin/perl
> cat mydomain.txt | perl -MSocket -lne'
> my $address = ( split /:/ )[ 0 ] or next;
> my $number = inet_aton $address;
> my $ip = inet_ntoa $number;
> print "$address -> $ip";
> '
> Where, mydomain.txt include all of the domain names and IP addresses
> which I want to deal with. Then, I meet the error like this:
>
> Bad name after lne' at ./11.pl line 2.
>
> On the other hand, if I remove the first line, i.e.,
>
> #!/usr/bin/perl
>
> it will works.
>
> Why?


Because with that line Perl will try to run it and fail but without that
line the shell will run it successfully.

If you want to save the program in the file '11.pl' then use this:

#!/usr/bin/perl
use warnings;
use strict;
use Socket;

while ( <> ) {
chomp;
my ( $address, $port ) = split /:/ or next;
my $number = inet_aton $address;
my $ip = inet_ntoa $number;
print "$address:$port -> $ip:$port\n";
}

__END__


And then run it as:

../11.pl mydomain.txt




John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Want to write a script to do the batch conversion from domain name to IP. Hongyi Zhao Python 6 01-31-2009 09:44 AM
batch tiff to jpeg conversion script rtilley@vt.edu Python 11 01-30-2006 03:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57