Chapter 13


1.

Try to install the XML::RSS Perl module. As you will see in the bonus chapter, we use this module in our sysadmin application to parse an RSS data stream that contains the latest security headlines.

you can use the cpan shell to install xml::rss , like so: # perl -mcpan -e shell cpan- install xml::rss

2.

Change the hello.pl program to display your own hometown.

this is a rather trivial change. simply modify the $city and $country variables: $city= 'indianapolis'; $country = 'us';

3.

Implement an application that is similar to list_users.pl to display the system groups: the gid followed by the group name . Hint: The group information is stored in /etc/group .

as you will see, this code is nearly identical to list_users.pl , with the exception of three lines. see if you can identify the differences: #!/usr/local/bin/perluse strict; ## enable `strict` pragmamy (@data); ## pre-declare variablesopen (file, '/etc/group') ## open the file die `cannot open file: $!\n`;while (-file-) {## read each record @data = split (/:/, $_, 4); ## separate the elements print pack (`a16a*`, $data[2], $data[0]), `\n`;## print ## gid, group }close (file); ## close file exit(0);## exit application

4.

Write a small snippet of code to invoke the lsmod command and display its output. The lsmod application shows information about all loaded kernel modules. Hint: Try to use either backticks or a piped open to communicate with the external application, as you will reuse this code in the next question.

since perl allows us to communicate with external applications and tools using a number of techniques, we will look at the three most popular ones. first, we ll start with backticks: $lsmod = `/sbin/lsmod`;print $lsmod; next, we ll look at the piped open: open (mypipe, '/sbin/lsmod ')  die `cannot create pipe: $!\n`;while (-mypipe-) { ## read one line at a time into $_ print; ## equivalent to: print $_; }close (mypipe); and finally, we ll end with the system command: system ('/sbin/lsmod');

5.

Extend the snippet of code you wrote for the previous question to display only the kernel module name and its size . There are many ways to accomplish this task, but using a regular expression will garner you bonus points.

we will look at two solutions, both of which use the piped open to communicate with the lsmod application. the first example uses a regular expression to extract the first two columns, while the second example uses the split command to do the same. use strict; ## enable `strict` pragmaopen (mypipe, '/sbin/lsmod ')  die `cannot create pipe: $!\n`;my $record;while (-mypipe-) {## read a line into $_ ($record) = /^(\s+\s+\s+)/; ## grab the first two columns print `$record\n`;## and store in $record }close (mypipe); exit(0); if you don t want to use a regular expression, you can also use the split command, which we have used in multiple examples: #!/usr/local/bin/perluse strict; ## enable `strict` pragmaopen (mypipe, '/sbin/lsmod ')  die `cannot create pipe: $!\n`;my ($module, $size);while (-mypipe-) {## read a line into $_ ($module, $size, undef) = split (/\s+/, $_, 3); ## split into three, ignore last set ## format and print info. print pack (`a16a*`, $module, $size), `\n`; } close (mypipe);exit(0);

6.

Change the Disk Usage Monitor application to send the system administrator, or root@localhost.localdomain , a copy of the warning message. Hint: Use the perldoc command to learn more about the Mail::Send module and its functionality.

before we get to the answer, here is how you would use the perldoc command to get documentation on the mail::send module: $ perldoc mail::send now to the code. the disk usage monitor application uses the send_email subroutine to send e-mail, so that is where we need to add some extra code: sub send_email { ...$mail= new mail::send; ## creates new mail::send object$list= ''; ## string to hold list of files $admin = 'root@localhost.localdomain'; $mail--to ($user);$mail--cc ($admin);## cc to administrator $mail--subject ('disk usage alert');$fh = $mail--open(); ...}

Answers

1.

You can use the CPAN shell to install XML::RSS , like so:

 # perl -MCPAN -e shell         cpan> install XML::RSS 

2.

This is a rather trivial change. Simply modify the $city and $country variables :

 $city    = 'Indianapolis';         $country = 'US'; 

3.

As you will see, this code is nearly identical to list_users.pl , with the exception of three lines. See if you can identify the differences:

 #!/usr/local/bin/perl         use strict;                                 ## Enable "strict" pragma         my (@data);                                 ## Pre-declare variables         open (FILE, '/etc/group')                   ## Open the file                   die "Cannot open file: $!\n";         while (<FILE>) {                            ## Read each record                 @data = split (/:/, $_, 4);             ## Separate the elements              print pack ("A16A*", $data[2], $data[0]), "\n";  ## Print                                                                   ## gid, group           }         close (FILE);                               ## Close file         exit  (0);                                  ## Exit application 

4.

Since Perl allows us to communicate with external applications and tools using a number of techniques, we will look at the three most popular ones. First, we ll start with backticks:

 $lsmod = `/sbin/lsmod`;              print $lsmod; 

Next, we ll look at the piped open:

 open (MYPIPE, '/sbin/lsmod ')  die "Cannot create pipe: $!\n";         while (<MYPIPE>) {       ## Read one line at a time into $_                 print;               ## Equivalent to: print $_;         }         close (MYPIPE); 

And finally, we ll end with the system command:

 system ('/sbin/lsmod'); 

5.

We will look at two solutions, both of which use the piped open to communicate with the lsmod application. The first example uses a regular expression to extract the first two columns , while the second example uses the split command to do the same.

 use strict;                             ## Enable "strict" pragma         open (MYPIPE, '/sbin/lsmod ')  die "Cannot create pipe: $!\n";         my $record;         while (<MYPIPE>) {                ## Read a line into $_             ($record) = /^(\S+\s+\S+)/;   ## Grab the first two columns             print "$record\n";            ## and store in $record         }         close (MYPIPE);         exit  (0); 

If you don t want to use a regular expression, you can also use the split command, which we have used in multiple examples:

 #!/usr/local/bin/perl         use strict;                       ## Enable "strict" pragma         open (MYPIPE, '/sbin/lsmod ')  die "Cannot create pipe: $!\n";         my ($module, $size);         while (<MYPIPE>) {                ## Read a line into $_                 ($module, $size, undef)                   = split (/\s+/, $_, 3);   ## Split into three, ignore last set                                             ## Format and print info.               print pack ("A16A*", $module, $size), "\n";             }          close (MYPIPE);          exit  (0); 

6.

Before we get to the answer, here is how you would use the perldoc command to get documentation on the Mail::Send module:

 $ perldoc Mail::Send 

Now to the code. The Disk Usage Monitor application uses the send_email subroutine to send e-mail, so that is where we need to add some extra code:

 sub send_email         {                 ...                 $mail  = new Mail::Send;         ## Creates new Mail::Send object                $list  = '';                     ## String to hold list of files                 $admin = 'root@localhost.localdomain';                $mail->to ($user);                $mail->cc ($admin);              ## CC to administrator               $mail->subject ('Disk Usage Alert');               $fh = $mail->open();                ...          } 



Beginning Fedora 2
Beginning Fedora 2
ISBN: 0764569961
EAN: 2147483647
Year: 2006
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net