Notes: Perl Lab 13

Network Functions

  1. Net::Ping
  2. Net::FTP
  3. Net::SMTP()

  1. 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
     
    #!/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] 
    

  2. 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
     
        
        
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab13 Example 01
    # Description: Example of Net::FTP
    #==========================================================================
    
    # add in the Net::FTP functions
    use Net::FTP;
    
    # set FTP variables
    $host = 'ftp.ibiblio.org';
    $ftp_user = 'anonymous';
    $ftp_pass = 'mark.tucker@lyndonstate.edu';
    $remote_path = '/pub/Linux';
    
    # create new ftp object.
    $ftp = Net::FTP->new($host, -Passive=>'true');
    
    # login anonymously
    $ftp->login($ftp_user,$ftp_pass) or die "Cannot login: ", $ftp->message;
    
    # change working directory 
    $ftp->cwd($remote_path) or die "Cannot change directory: ", $ftp->message;
    
    # retrieve the file
    $ftp->get("README") or die "Cannot get file: ", $ftp->message;
    
    # end the ftp session
    $ftp->quit;
    
    exit;
    # DONE

    When executed, the script above produces the following output:
     
    [mark@platypus LAB13] ./lab13_1.pl
    
    [mark@platypus LAB13] 
    

  3. Net::SMTP

    Net::SMTP allows mail transfers within a perl script. Again, using an object oriented syntax for all operations.

    Example lab13_2.pl
     
        
      
      
        
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab13 Example 02
    # Description: Example of Net::SMTP
    #==========================================================================
    
    # bring in the Net::SMTP functions
    use Net::SMTP;
    
    # set mail variables.
    $from = 'student@metlab15.lsc.vsc.edu';
    $mailhost = 'email.lsc.vsc.edu';
    $address = 'tuckerm@apollo.lsc.vsc.edu';
    $subject = 'Hi Mom!';
    
    # create new SMTP object
    $smtp = Net::SMTP->new($mailhost, Timeout=>30);
    
    # set from address
    $smtp->mail($from);
    
    # set the recipient address
    $smtp->to($address);
    
    # start sending data
    $smtp->data();
    
    # send message data
    $smtp->datasend("Subject: $subject\n\n");  # Note the two newlines
    $smtp->datasend("This is my message.\n");
    $smtp->datasend("Another line.\n");
    
    # terminate the message
    $smtp->dataend();
    
    # quit the smtp connection
    $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