Writing perl program on the linux platform


Objective:  

Write and test a simple perl program on the linux platform.  You may use any linux system you wish, including the Suse linux installed.

Activities.

To run linux in the lab use Vmware Workstation to run the virtual machine “Clone of OpenSuse 12.1”

Your program is going to function like the linux “ifconfig” command that is used to obtain information about network interfaces.  You will have to install the Net::Int::Stats and Net::Ifconfig::Wrapper packages using CPAN.  The CPAN documentation for the packages explains how the packages are used.  When you run CPAN on the computers in the lab you must run it with root privileges.  First, open a command prompt.  On the lab computers the command prompt is called XTerm and is listed in the menu under System Tools.  At the prompt enter

sudo perl -MCPAN -e shell

you will be asked for the root password, which is “tcpip”.   CPAN may ask for detailed configuration.
The linux ifconfig command can also be used to configure network interfaces, and, with no parameters will display information about all of the available interfaces.  Our program will not be able to set information, and will expect an interface name as a parameter.  It will then display information similar to what would be obtained by a command like “ifconfig eth0”.
To run the ifconfig command on the computers in the lab you must run the command with root privileges.  First, open a command prompt.  At the prompt enter

sudo /sbin/ifconfig

you will be asked for the root password, which is “tcpip”.

The Net::Int::Stats package has a constructor method called “new” which returns an object reference

$get_Iface_data = Net::Int::Stats->new();

A method of this object can be used to retrieve information about an interface like this

my $Iface     = $ARGV[0];
my $rx_packets  = $get_Iface_data->value($Iface, 'rx_packets');

The Net::Ifconfig::Wrapper package returns a complicated hash ref, from which information about an interface can be extracted.  This statement gets the hash ref:

my $Iface_Info = Net::Ifconfig::Wrapper::Ifconfig('list', '', '', '');

Then statements like these provide access to the data:

print "\tether ". $Iface_Info->{$Iface}{'ether'}."\n";

In this statement $Iface_Info is a hash ref, $Iface_Info->{$Iface} refers to the data in the hash corresponding to the key value contained in $Iface.  That data is also a hash, and $Iface_Info->{$Iface}{'ether'} refers to the data in this hash corresponding to the key value 'ether'.

This statement gets address and mask values, and since an interface can have more than one address it has to loop through a list contained in a hash,

while (my ($Addr, $Mask) = each(%{$Info->{$int}{'inet'}}))
      { print "\tinet $Mask $Addr"; };

Deliverable:

Include comments in your program that explain, for example,  what the object methods do, and how the data returned by Net::Ifconfig::Wrapper  is structured.  Private variables are to be used and the “use strict” pragma should be invoked at the start of the program.  The -w option should be present in the shabang line.

Request for Solution File

Ask an Expert for Answer!!
PL-SQL Programming: Writing perl program on the linux platform
Reference No:- TGS0545

Expected delivery within 24 Hours