ping is a Unix (or windows) command which is
used to test whether another machine is listening on the
network. The ping() function is Perl performs a
similar function to determine if a specified host can be
reached.
The ping() function is not included with the Perl
interpreter but is generally available as part of the Net
modules in most perl installations. As with most of the Net
modules, Net::Ping uses an object oriented syntax. A ping
"object" is created and referenced by a scalar
variable. This object then has various "methods" or
functions applied to it. The theory of object oriented
programming is beyond the scope of this course so we will not go
into the details.
Example lab13_0.pl
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab13 Example 00 # Description: Example of Net::Ping #========================================================================== # bring in the Net::Ping functions use Net::Ping; # set the hosts to query $host1 = 'metlab99'; $host2 = 'metlab19'; # create new ping object. $p = Net::Ping->new(); # check the hosts &Check_host($host1); &Check_host($host2); # destroy the ping object. $p->close(); exit; # DONE sub Check_host { # get the arguments my $host = $_[0]; print "Checking host: $host\n"; # ping the host and see if it is on the network. $ret_val = $p->ping($host); # print return value to the screen print "Return value is $ret_val\n"; # check the value and if($ret_val) { print "$host is alive!\n"; }else{ print "$host is missing in action\n"; } } |
When executed, the script above produces the following output:
|
||||
[mark@platypus LAB13] ./lab13_0.pl Return value is metlab99 is missing in action Checking host: metlab19 Return value is 1 metlab19 is alive! [mark@platypus LAB13] |
Example lab13_1.pl
|
|||
|
When executed, the script above produces the following output:
|
||||
[mark@platypus LAB13] ./lab13_1.pl [mark@platypus LAB13] |
Example lab13_2.pl
|
|||
|
When executed, the script above produces the following output:
|
||||
[mark@platypus LAB13] ./lab13_2.pl [mark@platypus LAB13] |
last updated: 04 May 2004 19:49