Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Want to write a script to do the batch conversion from domain name to IP. (http://www.velocityreviews.com/forums/t909573-want-to-write-a-script-to-do-the-batch-conversion-from-domain-name-to-ip.html)

Hongyi Zhao 01-30-2009 09:23 AM

Want to write a script to do the batch conversion from domain name to IP.
 
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 :.

RedGrittyBrick 01-30-2009 09:46 AM

Re: Want to write a script to do the batch conversion from domainname to IP.
 

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

John W. Krahn 01-30-2009 11:04 AM

Re: Want to write a script to do the batch conversion from domainname to IP.
 
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

Hongyi Zhao 01-30-2009 11:26 AM

Re: Want to write a script to do the batch conversion from domain name to IP.
 
On Fri, 30 Jan 2009 03:04:18 -0800, "John W. Krahn"
<someone@example.com> 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 :.

Hongyi Zhao 01-30-2009 11:33 AM

Re: Want to write a script to do the batch conversion from domain name to IP.
 
On Fri, 30 Jan 2009 03:04:18 -0800, "John W. Krahn"
<someone@example.com> 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 :.

Hongyi Zhao 01-30-2009 11:57 AM

Re: Want to write a script to do the batch conversion from domain name to IP.
 
On Fri, 30 Jan 2009 03:04:18 -0800, "John W. Krahn"
<someone@example.com> 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 :.

Hongyi Zhao 01-30-2009 12:20 PM

Re: Want to write a script to do the batch conversion from domain name to IP.
 
On Fri, 30 Jan 2009 19:33:35 +0800, Hongyi Zhao
<hongyi.zhao@gmail.com> 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 :.

Jürgen Exner 01-30-2009 12:42 PM

Re: Want to write a script to do the batch conversion from domain name to IP.
 
Hongyi Zhao <hongyi.zhao@gmail.com> 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

John W. Krahn 01-30-2009 01:08 PM

Re: Want to write a script to do the batch conversion from domainname to IP.
 
Hongyi Zhao wrote:
> On Fri, 30 Jan 2009 03:04:18 -0800, "John W. Krahn"
> <someone@example.com> 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

John W. Krahn 01-30-2009 01:17 PM

Re: Want to write a script to do the batch conversion from domainname to IP.
 
Hongyi Zhao wrote:
> On Fri, 30 Jan 2009 03:04:18 -0800, "John W. Krahn"
> <someone@example.com> 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


All times are GMT. The time now is 07:46 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.