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
string1
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();
if(!$ssh->connect($host)){
print("Connection Failed\n");
exit(1);
}
if(!$ssh->auth_password($user,$password)){
print("Authentication Failed");
exit(1);
}
my $channel = $ssh->channel();
$channel->blocking(0);
$channel->shell();
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>) {
chomp;
print $OUTPUTFILE "$_";
} ### Get rid of the message lines
close $OUTPUTFILE or die "$OUTPUTFILE: $!";
Run command: perl grabcisco.pl 10.1.1.1 password cisco -> Target host is '10.1.1.1'; ssh password is 'password', enable password is 'cisco' and output filename is '10.1.1.1.log')
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:
Post a Comment