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

H

Hongyi Zhao

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
 
R

RedGrittyBrick

Hongyi said:
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

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
 
J

John W. Krahn

Hongyi said:
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

$ 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
 
H

Hongyi Zhao

Hongyi said:
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

$ 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.
 
H

Hongyi Zhao

$ 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?
 
H

Hongyi Zhao

$ 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?
 
H

Hongyi Zhao

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.
 
J

John W. Krahn

Hongyi said:
$ 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
 
J

John W. Krahn

Hongyi said:
$ 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
 
H

Hongyi Zhao

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

Thanks, I've got it.
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

If I want to redirect the output to a file, say myIP.txt, I know that
the following line will be OK:

../11.pl mydomain.txt > myIP.txt

But, it should be also possiable to redirect the output to a file
within the above perl script, any hints on this?
 
T

Tad J McClellan

Hongyi Zhao said:
On Fri, 30 Jan 2009 05:17:00 -0800, "John W. Krahn"


my $outfile = '/some/other/myIP.txt';
open my $OTHER, '>', $outfile or die "could not open '$outfile' $!";



print $OTHER "$address:$port -> $ip:$port\n";
 
H

Hongyi Zhao

my $outfile = '/some/other/myIP.txt';
open my $OTHER, '>', $outfile or die "could not open '$outfile' $!";

I use the following two lines:

my $outfile = '~/myIP.txt';
open my $OTHER, '>', $outfile or die "could not open '$outfile' $!";

But, I'll mee the errors like this:

$ ./DomainName2IP.pl cybersyndrome.pla5.txt
Missing comma after first argument to open function at
../DomainName2IP.pl line 1
3, near "'>>./you.txt';"
Execution of ./DomainName2IP.pl aborted due to compilation errors.
 
T

Tad J McClellan

Hongyi Zhao said:
On Fri, 30 Jan 2009 08:52:33 -0600, Tad J McClellan


I use the following two lines:

my $outfile = '~/myIP.txt';
open my $OTHER, '>', $outfile or die "could not open '$outfile' $!";


Using tilde (~) to mean "home directory" is a shell feature.

Perl is not the shell, so that feature is not available.

my $outfile = '/home/zhao/myIP.txt';
or
my $outfile = 'myIP.txt'; # write to current directory
or
my $outfile = './myIP.txt'; # same thing

But, I'll mee the errors like this:

$ ./DomainName2IP.pl cybersyndrome.pla5.txt
Missing comma after first argument to open function at
./DomainName2IP.pl line 1
3, near "'>>./you.txt';"
^^^^^^^
^^^^^^^

Where did "you.txt" come from?

Are you sure you have the right code going with the right error message?
 
T

Ted Zlatanov

TJM> Using tilde (~) to mean "home directory" is a shell feature.

TJM> Perl is not the shell, so that feature is not available.

Well, glob("~/.cshrc") works OK for me, so the feature is available. It
uses File::Glob in recent Perls...

Ted
 
H

Hongyi Zhao

^^^^^^^
^^^^^^^

Where did "you.txt" come from?

Are you sure you have the right code going with the right error message?

You're right, there's a bug line in the code used by me, which is come
from the previous attempt by me.

I've sorted it out, thanks again.

Regards,
 
P

Peter J. Holzer

Using tilde (~) to mean "home directory" is a shell feature.

Perl is not the shell, so that feature is not available.

my $outfile = '/home/zhao/myIP.txt';

Only if the $HOME is '/home/zhao'.

my $outfile = "$ENV{HOME}/myIP.txt";

is the equivalent to ~/myIP.txt.

(or use glob, as Ted suggested)

hp
 
H

Hongyi Zhao

#!/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__

If I want to do the opposite thing, i.e., conversion from IP to domain
name, what should I revise this script to do the trick.

In fact, I've post another news on this thing, but that script meet
errors in my case, and furthermore, it looks more complex then this
one.
 
J

John W. Krahn

Hongyi said:
If I want to do the opposite thing, i.e., conversion from IP to domain
name, what should I revise this script to do the trick.


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

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

__END__




John
 
H

Hongyi Zhao

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

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

__END__

Thanks a lot, John.

Thanks for others who give me helps or constructive advices again.

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top