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

Discussion in 'Perl Misc' started by Hongyi Zhao, Jan 30, 2009.

  1. Hongyi Zhao

    Hongyi Zhao Guest

    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
     
    Hongyi Zhao, Jan 30, 2009
    #1
    1. Advertisements

  2. 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
     
    RedGrittyBrick, Jan 30, 2009
    #2
    1. Advertisements

  3. $ 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
     
    John W. Krahn, Jan 30, 2009
    #3
  4. Hongyi Zhao

    Hongyi Zhao Guest

    Very good, thanks a lot, it does the trick.
     
    Hongyi Zhao, Jan 30, 2009
    #4
  5. Hongyi Zhao

    Hongyi Zhao Guest

    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, Jan 30, 2009
    #5
  6. Hongyi Zhao

    Hongyi Zhao Guest

    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, Jan 30, 2009
    #6
  7. Hongyi Zhao

    Hongyi Zhao Guest

    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, Jan 30, 2009
    #7
  8. 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
     
    Jürgen Exner, Jan 30, 2009
    #8


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




    John
     
    John W. Krahn, Jan 30, 2009
    #9
  10. 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
     
    John W. Krahn, Jan 30, 2009
    #10
  11. Hongyi Zhao

    Hongyi Zhao Guest

    Thanks, I've got it.
    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?
     
    Hongyi Zhao, Jan 30, 2009
    #11

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



    print $OTHER "$address:$port -> $ip:$port\n";
     
    Tad J McClellan, Jan 30, 2009
    #12
  13. Hongyi Zhao

    Hongyi Zhao Guest

    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.
     
    Hongyi Zhao, Jan 30, 2009
    #13

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

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

    Where did "you.txt" come from?

    Are you sure you have the right code going with the right error message?
     
    Tad J McClellan, Jan 30, 2009
    #14
  15. Hongyi Zhao

    Ted Zlatanov Guest

    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
     
    Ted Zlatanov, Jan 30, 2009
    #15
  16. Hongyi Zhao

    Hongyi Zhao Guest

    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,
     
    Hongyi Zhao, Jan 31, 2009
    #16
  17. 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
     
    Peter J. Holzer, Jan 31, 2009
    #17
  18. Hongyi Zhao

    Hongyi Zhao Guest

    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.
     
    Hongyi Zhao, Jan 31, 2009
    #18

  19. #!/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
     
    John W. Krahn, Jan 31, 2009
    #19
  20. Hongyi Zhao

    Hongyi Zhao Guest

    Thanks a lot, John.

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

    Regards,
     
    Hongyi Zhao, Feb 1, 2009
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.