Tuesday, 29 April 2014

Perl Script for Cisco IOS routers/switches for Information collection

Even though I had started out my life in IT moonlighting at a Calcutta (India) based start-up search-engine and greeting-card site (now arguably the largest in the country) as a Perl Programmer in mid-nineties,  about 14 years of hacking Cisco IOS, IOS-XR, NX-OS, BGP, MPLS, WAN Optimization, Juniper etc., had left me almost devoid of my ready skills in Perl, Python, C etc.

Woke up one morning few months back and realised that advent of SDN, NFV, Cloud etc. demands that I start to pick up my almost forgotten programming skills. So I enrolled myself to couple of Coursera modules for Python to get back into the game.  Strangely enough, I struggled to find a similar programme for Perl either on Coursera or eDX.

My industry background is a network-engineer bred on Cisco’s tech. Hence, as always, for me a good place (and comfort zone) to let lose the trial runs of scripts is the good old reliable Cisco IOS. I fired off a few scripts, needed Perl manuals big time for a starter, but felt the old commands slowly creeping back to the tips of my decrepit fingers. Perhaps there is hope for me after all!

My first stop was at http://stdioe.blogspot.co.uk/2011/09/how-to-connect-cisco-router-with-perl.html

The scripts did work, but I was not really satisfied. What I wanted was a bit more flexibility and to actually be able to collect the show command outputs in a separate file.

Perl Script for Cisco IOS routers/switches for Information collection; Connect through SSH

After a few trials with hard-coded target-hosts which is cumbersome because one needs to keep changing the script every time the need for connection comes up, I was finally able to define the target-host, the CLI password and enable secret as input variables. Thus the script hopefully achieved the flexibility to be used for any devices with any password without any need for hardcoding in the script itself. I also got ambitious and somehow managed to define the output of the show commands to be generated into a saved log file.

use Net::SSH2;

use warnings;

use strict;

my $host = $ARGV[0]; ### Input target host


my $user = "admin";

my $password = $ARGV[1]; ### Input CLI/ssh password string2

my $secret = $ARGV[2]; ### Input enable pssswd as string3

my $ssh = Net::SSH2->new();


print("Connection Failed\n");




     print("Authentication Failed");



my $channel = $ssh->channel();




print $channel "enable\n";

print $channel "$secret\n"; ### enable psswd from input string3

print $channel "term len 0\n";

print $channel "show clock\n";

print $channel "dir\n";

open (my $OUTPUTFILE, ">$host.log") or die "Can't open $host.log: $!"; ### target host as filename

    while (<$channel>) { 


    print $OUTPUTFILE "$_"; 

    }                      ### Get rid of the message lines

close $OUTPUTFILE or die "$OUTPUTFILE: $!";


Run command:  perl grabcisco.pl password cisco -> Target host is ''; ssh password is 'password', enable password is 'cisco' and output filename is '')

My Perl skills are still not exactly great and the old fluidity of writing scripts are still a far cry, but perhaps not as far as they were, say, three months back. Persevere, persevere, persevere and lack of time generated by full-time professional engagement and a 1 year old son at home may one day wear off to get the creativity back!...”Yeah, and pigs might fly”, you say! J









No comments:

Breakfast At Serengeti

Breakfast At Serengeti
Lion's Share

The Ngorongoro Family

The Ngorongoro Family
Click on the Picture Above To Make It Larger

Tabloid Time: The Aliens Are a'Landing ?!.. ;-)

At the risk of being ridiculed and being labelled a freak, I shall like to draw everyone's attention to the following recent events....If you watch the videos then turn on the sound for the commentary...

Fireball across Ausin, Texas (16th Feb 2009). According to BBC, apparently, its NOT debris from a recent satellite collision...:

Same in Idaho in recent times. NO meteor remains found yet: http://news.bbc.co.uk/1/hi/sci/tech/7744585.stm

Exactly same in Sweden: http://news.bbc.co.uk/1/hi/world/europe/7836656.stm?lss

This was recorded on 25th Feb 2007 in Dakota, US:

This year has seen three of the spookiest UFO videos surface, with people in India, Mexico and even in space, NASA, spotting things they couldn't explain: http://www.youtube.com/watch?v=7WYRyuL4Z5I&feature=related

CHECK out this one on 24th Januray, 2009 in Argentina close to Buenos Aires:
You tube: www.youtube.com/
Press Coverage

AND Lastly, and more importantly, from Buzz Aldrin on Apollo 11 : http://www.youtube.com/watch?v=XlkV1ybBnHI

Heh?! Don't know how authentic these news are... don't even know if these are UFO's or meteors or ball lightning or something else. But, if meteors, then where are the meteorites ? However, I see no reason why life cannot exist in other planets and why they could not be sneaking around here :-) . I for one, have long suspected some of my relations to be space aliens or at least X-people from X-files :-)

I am waiting for a job on an Alien spaceship myself. :-)

Giraffes in Parallel Universe

Giraffes in Parallel Universe
At Lake Manyara

Serengeti Shall Never Die

Serengeti Shall Never Die
Wildebeeste Calf starts running only 5 min. after being born. CLICK on the pitcture to view Slideshow