John W. Krahn wrote:
> Gunwant Singh wrote:
>>
>> I have written a code to ping remote sites:
>> -----------------------------------
>> use strict;
>> use warnings;
>> use Socket;
>> use Net::Ping;
>>
>> print 'Enter a hostname/IP ';
>> my $ip,my $host;
>> $ip=<STDIN>;
>> chomp($ip);
>> if ($ip =~  /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/)
>> {
>> my $p = Net::Ping->new() or die "Can't create new ping object: $!\n";
>> if ($p->ping($ip)) { print "$ip is alive"; $p->close; exit;} else {print 
>> "$ip is not alive";exit;}
>> }
>> else
>> {
>> $host=gethostbyname($ip);
>> chomp($host);
>> my $aip=inet_ntoa($host);
>> my $p = Net::Ping->new() or die "Can't create new ping object: $!\n";
>> if ($p->ping($aip)) {print "$aip is alive";}  else {print "$aip is not 
>> alive";}
>> $p->close;exit;
>> }
>> ------------------------------------------------------------------------
>> If I run the code and ping www.yahoo.com, it resolves the hostname to 
>> ip, and then it says it is NOT alive.
>> If I ping the same IP using the Ping utility, it is definitely replying. 
>> Whats wrong?
> 
> The ping program will first try to use the 'icmp' protocol on port 7 and 
> if you are not logged in as root will then try the 'tcp' protocol on a 
> different port.
> 
> Try it with the HTTP port:
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Socket;
> use Net::Ping;
> 
> print 'Enter a hostname/IP ';
> chomp( my $input = <STDIN> );
> my $ip = inet_ntoa inet_aton $input;
> my $p = Net::Ping->new() or die "Can't create new ping object: $!\n";
> $p->port_number( 80 );  # HTTP port
> print "$ip is ", $p->ping( $ip ) ? '' : 'not ', "alive\n";
> $p->close;

Ah I see the latest revision 2.35 of Net::Ping has a port_number method just as
it should. It wasn't there in version 2.31!

The default protocol hasn't changed though - it uses echo on tcp if you don't
say otherwise.

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to