Notes: Perl Lab 13
Network Functions
- Net::Ping
- Net::FTP
- Net::SMTP()
-
Net::Ping
The 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
|
use Net::Ping;
$host1 = 'metlab99';
$host2 = 'metlab19';
$p = Net::Ping->new();
&Check_host($host1);
&Check_host($host2);
$p->close();
exit;
sub Check_host {
my $host = $_[0];
print "Checking host: $host\n";
$ret_val = $p->ping($host);
print "Return value is $ret_val\n";
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]
|
-
Net::FTP
The Net::FTP module allows file retrieval and uploading within
perl. An ftp "object" is created and used for all
actions related to the ftp session.
Example lab13_1.pl
|
use Net::FTP;
$host = 'ftp.ibiblio.org';
$ftp_user = 'anonymous';
$ftp_pass = 'mark.tucker@lyndonstate.edu';
$remote_path = '/pub/Linux';
$ftp = Net::FTP->new($host, -Passive=>'true');
$ftp->login($ftp_user,$ftp_pass) or die "Cannot login: ", $ftp->message;
$ftp->cwd($remote_path) or die "Cannot change directory: ", $ftp->message;
$ftp->get("README") or die "Cannot get file: ", $ftp->message;
$ftp->quit;
exit;
|
When executed, the script above produces the following output:
|
[mark@platypus LAB13] ./lab13_1.pl
[mark@platypus LAB13]
|
-
Net::SMTP
Net::SMTP allows mail transfers within a perl script. Again,
using an object oriented syntax for all operations.
Example lab13_2.pl
|
use Net::SMTP;
$from = 'student@metlab15.lsc.vsc.edu';
$mailhost = 'email.lsc.vsc.edu';
$address = 'tuckerm@apollo.lsc.vsc.edu';
$subject = 'Hi Mom!';
$smtp = Net::SMTP->new($mailhost, Timeout=>30);
$smtp->mail($from);
$smtp->to($address);
$smtp->data();
$smtp->datasend("Subject: $subject\n\n"); $smtp->datasend("This is my message.\n");
$smtp->datasend("Another line.\n");
$smtp->dataend();
$smtp->quit;
exit;
|
When executed, the script above produces the following output:
|
[mark@platypus LAB13] ./lab13_2.pl
[mark@platypus LAB13]
|
last updated: 18 Mar 2012 12:49