CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Net::DNS & Net::Ping HELP

 



mackiew
Deleted

Sep 19, 2000, 9:58 AM

Post #1 of 9 (1509 views)
Net::DNS & Net::Ping HELP Can't Post

How to use Net::Ping module??
I do :

$p = Net::Ping->new("tcp");
print "$host is alive.\n" if $p->ping($host);
$p->close();

whith a reacheable $host and not print "$host is alive"
in a Linux S.O.

What must be $host?

$host = 126.73.100.100 or $host = "126.73.100.100"
or $host = 12673100100 or etc.....

Im beginner whit Perl, but not beginer programmer and need
work with this modules, help please.
Thanks


Kanji
User / Moderator

Sep 19, 2000, 4:26 PM

Post #2 of 9 (1509 views)
Re: Net::DNS & Net::Ping HELP [In reply to] Can't Post

$host = "127.0.0.1"; or $host = "localhost.my.domain";.

As for why it isn't working, you appear to have ignored the docs: the tcp/udp methods only work if the remote host is running the echo service, which any sensible admin will not.

You want to use the icmp method, but that requires you to be root(like) in order for the ping to work.

If you don't have root, then you can use the ping program as that usually executes with root privileges ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my @ping = qw( /path/to/ping -c 1 );


sub ping {
my $host = shift &#0124; &#0124; return;


open( PING, "-|" )
or exec( @ping, $host )
or exit;


return if eof( PING );


while ( <PING> ) {
return 1 if /, (\d+)% packet loss/
&& $1 < 100;
}
return 0;


}


print "$host is alive.\n" if ping( $host );


my $up = ping( $host );


if ( $up ) { print "YES" }
elsif ( defined $up ) { print "NO" }
else { print "ERR" }</pre><HR></BLOCKQUOTE>

The last four lines show all the possibilities.

The good thing about this method is you could define at what level of ping loss the host should be considered down. 100% is a definite, but depending on your situation, anything more than 50% may qualify as site down.

An even simpler method would be ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

print "$host is alive.\n" if system( "/path/to/ping -c 1 $host 1>/dev/null 2>&1" ) == 0;</pre><HR></BLOCKQUOTE>


mackiew
Deleted

Sep 21, 2000, 9:56 AM

Post #3 of 9 (1509 views)
Re: Net::DNS & Net::Ping HELP [In reply to] Can't Post

Hi Kanji!
I not ignored the tcp and udp metod for example I do:

$host = 'www.host.com'
$p = Net::Ping->new("tcp");
print "$host is alive.\n" if $p->ping($host);
$p->close();

it work fine

I do:

$host = '164.73.155.20'
$p = Net::Ping->new("tcp");
print "$host is alive.\n" if $p->ping($host);
$p->close();

it not work. Why?
What I'm doing wrong?

Your simpler method dont work.

Im waiting your help
Thank you.



Kanji
User / Moderator

Sep 21, 2000, 11:04 AM

Post #4 of 9 (1509 views)
Re: Net::DNS & Net::Ping HELP [In reply to] Can't Post

Neither of your examples work: www.host.com doesn't have the echo service turned on (so a tcp ping will never return true), and 164.73.155.20 doesn't even appear to be online so won't return true either.

That host and IP don't map to each other, so I'm guessing you're giving us wrong input which makes things really difficult to solve.

As for my simple method, it works on all the OSes I have available, although I did need to remove the -c 1 under Solaris ... you did change /path/to/ping to the appropriate path to ping, yes?

Better yet, what OS are you using and what happens when you try running /path/to/ping -c 1 www.host.com from the command line?


mackiew
Deleted

Sep 21, 2000, 11:31 AM

Post #5 of 9 (1509 views)
Re: Net::DNS & Net::Ping HELP [In reply to] Can't Post

Excuse me but the IP and host name are generics.

I do exactly $host = 'seciu.edu.uy' and
$host = '164.73.128.5' this host is ever reachable.
In the first $host work, and the second don't work.
Set this values in my previous examples y see..


Kanji
User / Moderator

Sep 21, 2000, 3:00 PM

Post #6 of 9 (1509 views)
Re: Net::DNS & Net::Ping HELP [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -l


use Net::Ping;


$p = Net::Ping->new("tcp");


$host = 'seciu.edu.uy';
print "$host is alive." if $p->ping($host);


$host = '10.10.10.10'; # Dead IP.
print "$host is alive." if $p->ping($host);


$host = '164.73.128.5';
print "$host is alive." if $p->ping($host);


$p->close();


__END__
$ ./ping.pl
seciu.edu.uy is alive.
164.73.128.5 is alive.</pre><HR></BLOCKQUOTE>
That text after __END__ is what happens when I run the script. As you can see, your host returns alive in both cases, but 10.10.10.10 -- which I know is down -- doesn't return at all.

Perhaps you are misquoting your IPs? And if your script is working okay with hostnames, why not just use those instead?


mackiew
Deleted

Sep 25, 2000, 8:57 AM

Post #7 of 9 (1509 views)
Re: Net::DNS & Net::Ping HELP [In reply to] Can't Post

Hi Kanji

Your posted code is exatly what I do !!

I need IP address because I have IPs to check and not host names

Is ___END___ necessary?

Regards


Kanji
User / Moderator

Sep 25, 2000, 4:07 PM

Post #8 of 9 (1509 views)
Re: Net::DNS & Net::Ping HELP [In reply to] Can't Post

Everything after (and including) __END__ is not necessary.


mackiew
Deleted

Sep 28, 2000, 10:29 PM

Post #9 of 9 (1509 views)
Re: Net::DNS & Net::Ping HELP [In reply to] Can't Post

Hi Kanji:
I solved my problem !!
You help me very much.
Zank you very much !!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives