13.07.2015 Views

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

APPENDIX ■ PERL TIPSIt can also handle options with values, options with multiple names, and a widevariety of other possibilities. (It may also be overkill for a basic script, but it’suseful for more complicated ones.)• LWP::Simple: Grabs a web page with a single command.use LWP::Simple;my $document = get("http://www.itv.com/sport/tourdefrance/default.html")or die "Couldn't get TdF page";if ($document =~ /Armstrong/) { print "Highly unusual lack of Lance!");• There’s very little error handling (you get undef on failure and the content onsuccess), but it does what it does admirably straightforwardly.• Mail::Sendmail: Provides a simple module for sending mail from a script. It’ll usewhatever your default mail server is.use Mail::Sendmail;%mail = ( To => 'me@gmail.com',From => 'me@example.com',Message => "Talking to myself...");sendmail(%mail) or die $Mail::Sendmail::error;• Net::LDAP and Net::LDAPS: Provides various LDAP interface options. See recipe 2-10 for details.• Net::Ping: Provides a simple module that you can use to ping other hosts and getthe return time and other information.use Net::Ping;my $ping = Net::Ping->new();my ($ret, $duration, $ip) = $ping->ping($server, 5.5);• Perl::Tidy (aka perltidy): Tidies up badly formatted code (useful if you’re takingover someone else’s code and that person didn’t have your own high readabilitystandards) and will also output nicely formatted HTML with the -html option.> perltidy badscript.plBe warned! The more horrendous the input was, the higher the possibility thatperltidy will break it. Make sure that you test properly before and after to ensurethat the code is still doing the same thing.• Text::Table: Prints output in a nice table. You create the table and then load thedata in, and Text::Table handles the alignment and presentation. It’s basic butuseful.• Time::HiRes: Implements high-resolution (microsecond) timers. This is often usedby other modules, such as Net::Ping.use Net::Ping;use Time::HiRes;my $ping = Net::Ping->new();$ping->hires();250Download at WoweBook.Com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!