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
On Fri, 30 Jan 2009 05:17:00 -0800, "John W. Krahn"
<(E-Mail Removed)> wrote:
>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?

--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      01-30-2009
Hongyi Zhao <(E-Mail Removed)> wrote:
> On Fri, 30 Jan 2009 05:17:00 -0800, "John W. Krahn"
><(E-Mail Removed)> wrote:



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



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


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



print $OTHER "$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?
>



--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
 
Hongyi Zhao
Guest
Posts: n/a
 
      01-30-2009
On Fri, 30 Jan 2009 08:52:33 -0600, Tad J McClellan
<(E-Mail Removed)> wrote:

>Hongyi Zhao <(E-Mail Removed)> wrote:
>> On Fri, 30 Jan 2009 05:17:00 -0800, "John W. Krahn"
>><(E-Mail Removed)> wrote:

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

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

--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      01-30-2009
Hongyi Zhao <(E-Mail Removed)> wrote:
> On Fri, 30 Jan 2009 08:52:33 -0600, Tad J McClellan
><(E-Mail Removed)> wrote:



>> 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' $!";



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?



--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      01-30-2009
On Fri, 30 Jan 2009 11:27:44 -0600 Tad J McClellan <(E-Mail Removed)> wrote:

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
 
Reply With Quote
 
Hongyi Zhao
Guest
Posts: n/a
 
      01-31-2009
On Fri, 30 Jan 2009 11:27:44 -0600, Tad J McClellan
<(E-Mail Removed)> wrote:

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


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 [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      01-31-2009
On 2009-01-30 17:27, Tad J McClellan <(E-Mail Removed)> wrote:
> Hongyi Zhao <(E-Mail Removed)> wrote:
>> On Fri, 30 Jan 2009 08:52:33 -0600, Tad J McClellan
>><(E-Mail Removed)> wrote:
>>> 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' $!";

>
>
> 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
 
Reply With Quote
 
Hongyi Zhao
Guest
Posts: n/a
 
      01-31-2009
On Fri, 30 Jan 2009 05:17:00 -0800, "John W. Krahn"
<(E-Mail Removed)> wrote:

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

--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-31-2009
Hongyi Zhao wrote:
> On Fri, 30 Jan 2009 05:17:00 -0800, "John W. Krahn"
> <(E-Mail Removed)> wrote:
>
>> #!/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.



#!/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
--
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
 
      02-01-2009
On Sat, 31 Jan 2009 08:53:47 -0800, "John W. Krahn"
<(E-Mail Removed)> wrote:

>Hongyi Zhao wrote:
>> On Fri, 30 Jan 2009 05:17:00 -0800, "John W. Krahn"
>> <(E-Mail Removed)> wrote:
>>
>>> #!/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.

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

--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
 
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