Perl Beginnings

After reading some advice from some very smart people, I’ve decided to try my hand at learning a scripting language to aid in my network administration tasks. After some quick searching I’ve decided to learn Perl. From what I’ve gathered it has strong text manipulation capabilities which will come in handy parsing device configs or SNMP/SSH responses. I’m not going to attempt to teach programming basics, but what I hope to cover will be some useful examples and building blocks that we can use as network administrators.

For my scripting setup I will be focusing on a Windows perspective. Instead of using ActivePerl I decided to use Cygwin and the Perl modules, so hopefully most of what I write will also apply to *nix users. I’m also using Notepad++ for my editing/coding, but you could also use the default Windows Notepad or if you want to be hardcore, Cygwin and VIM. I may include some VIM tips/shortcuts in future posts.

My first Perl script is not “Hello, World”, but instead a simple Ping script. I used Net::Ping to conduct a simple ICMP Ping and then output the results. The example I first used had a hard-coded variable for the host, which I left commented out for reference purposes. Instead, I added a line to receive a command line argument for the host variable.

use Net::Ping;
use strict;

#Setting a variable
#my $host = "www.google.com";

#Using an arguement
my $host = "$ARGV[$0]";
my $pingobj = Net::Ping->new('icmp');
my ($status,$time,$ip) = $pingobj->ping($host);

if ($status) {
print "Host $host ($ip) responded in $time secondsn";
} else {
print "Host $host ($ip) unreachablen";
}

Voila!

I plan on using this script as a building block to test connectivity before performing more complex tasks in the future. Hopefully this will motivate somebody to get their hands dirty and add another tool to their skill set.

  1. Perl – Reading from File « Robert Juric - pingback on December 20, 2011 at 9:13 pm
  2. I really miss writing code. I used to do it a lot more when I worked on more servers than network gear. Since moving over to soley a network guy I haven’t been able to write a single line of code.
    I have vowed to learn TCL and perl once I finish my CCIE. This post just makes me want to do it all the more. :)

    • I’ve dabbled with coding in the past, so it is refreshing. However I’m taking the approach of learning it and then I’ll find a use for it. I’m still trying to narrow down the specific use cases for my work environment.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks and Pingbacks: