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

C:\Documents and Settings\Gunnu\Desktop\Code\Gen 2>ping.pl

Enter a hostname/IP www.yahoo.com
87.248.113.14 is not alive

C:\ping 87.248.113.14
Pinging 87.248.113.14 with 32 bytes of data:
Reply from 87.248.113.14: bytes=32 time=188ms TTL=49
Reply from 87.248.113.14: bytes=32 time=207ms TTL=49

Your code is poorly laid out - it is extremely difficult to spot an errors with
no whitespace and indenting.
That was rude!
Net::Ping tries to connect on the tcp echo port (port 7) by default, and many
commercial http servers won't respond on that port. Unforunately there isn't a
nice way to change the port number, so you have to mess with the object's
internals like this

  my $p = Net::Ping->new;
  $p->{port_num} = 80;               # HTTP port number

then you should get a response from Yahoo.

Thanks for the suggestion.

HTH,

Rob




--
Gunwant Singh.

"What is the sound of Perl? Is it not the sound of Wall that people bang their heads against?"


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


Reply via email to