Tuesday, September 17, 2013

A Perl script to collect information from Cisco IOS routers/switches


This is a journal about creating a perl script that establishes SSH session to Cisco IOS routers/switches and collect a list of information.



I started with:

google: perl script to ssh to cisco router
found: http://forums.freebsd.org/showthread.php?t=31396 posted by 'Business_Woman'. 

As per the post, she needed advice to enter the privileged EXEC mode, after a successful SSH session login. 

I checked my perl setup:
Platform: Windows 7
Perl: ActivePerl by ActiveState
Version: v5.10.1 (command: perl -v)

My perl doesn't support 'Net::SSH2'. Then:

google: Net::SSH2 for windows
found: http://www.perlmonks.org/?node_id=949926

I chose to try 1st option (cpan install) and as my network behind a proxy, then:

google: perl cpan set http_proxy
found: http://abhijit.name/setting_cpan_for_proxy.html

Followed the instructions, but got:

error:  LWP failed with code[301] message[Moved Permanently]
Warning: no success downloading 'C:\Perl\cpan\sources\authors\01mailrc.txt.gz.tmp7232'. Giving up on it. at C:\Perl\lib/CPAN/Index.pm line 225

Next, I tried the 2nd option, which was using Strawberry Perl. I downloaded and installed Strawberry Perl, which is now v5.18.1 (http://strawberryperl.com/). Now that I've Net::SSH2 library on my perl, I prepared my script based on 'Business_Woman' post and saved it as cisco.pl as follow:

use Net::SSH2;
use warnings;
use strict;
my $host = "10.10.1.1";            ### 10.10.1.1 is my target device
my $user = "admin";                 ### my ssh login is admin/cisco
my $password = "cisco";
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();
my $command = $channel->exec("sh ver") or die $_;
my $output;
my $len = $channel->read($output,2048);

Running it by the command: perl cisco.pl
Bingo! At this point, I got the same condition with 'Business_Woman'. The next step is to find way to enter the privileged EXEC mode:

google: perl script show running-config cisco ==>results: not on target
google: perl script cisco privilege mode ==>results: not on target
google: net::SSH2 documentation ==>results: not on target
google: perl net::ssh2 examples
found: http://www.perlmonks.org/?node_id=569657

On this URL, I came to the part '#to run multiple commands use a shell', then I modified my script as follow:

use Net::SSH2;
use warnings;
use strict;
my $host = "10.10.1.1";            ### 10.10.1.1 is my target device
my $user = "admin";                 ### my ssh login is admin/cisco
my $password = "cisco";
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 "cisco\n";     ### enable password is cisco
print $channel "term len 0\n";
print "LINE : $_" while <$channel>;
print $channel "show clock\n";
print "LINE : $_" while <$channel>;
print $channel "dir\n";
print "LINE : $_" while <$channel>;

After few trials and error, my script (last 9 lines) became:

my $channel = $ssh->channel();
$channel->blocking(0);
$channel->shell();
print $channel "enable\n";
print $channel "cisco\n";     ### enable password is cisco
print $channel "term len 0\n";
print $channel "show clock\n";
print $channel "dir\n";
print "$_" while <$channel>;

At this point, it had achieved my target. To further shape the script:
1) remove the passwords from the scripts
2) write the output to a file instead of screen

google: perl input argument
found: http://stackoverflow.com/questions/361752/how-can-i-pass-command-line-arguments-to-a-perl-program

google: perl write to file
found: http://perl.about.com/od/perltutorials/a/readwritefiles_2.htm

From the found URLs, I made my target host as input variable, as well as the SSH password and enable secret. With that my script has the flexibility to be used for any devices with any password (not hardcoded on the script). Secondly, I wrote the show information results onto a file and I kept it as a log file. My script became:

use Net::SSH2;
use warnings;
use strict;
my $host = $ARGV[0];            ### target host as input string1
my $user = "admin";
my $password = $ARGV[1];        ### ssh password as input string2
my $secret = $ARGV[2];        ### enable password as input 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 password 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
print $OUTPUTFILE "$_" while <$channel>;
close $OUTPUTFILE or die "$OUTPUTFILE: $!";

Running it by the command: perl cisco.pl 10.10.1.1 cisco cisco
(My target host is '10.10.1.1', ssh password is 'cisco', enable password is 'cisco'; generated output filename is '10.10.1.1.log')
At this point, the result written on the file contained many newlines between the messages, so:

google: perl trim empty lines  ==>results: not on target
google: perl remove newline from $_ ==>results: not on target
google: perl $_ condition
found: http://perlmaven.com/the-default-variable-of-perl

This URL shows the elegant way of using $_ (perl default variable) and from the example, it shows the usage of 'chomp' too. I modified my script (last 6 lines) as follow:

open (my $OUTPUTFILE, ">$host.log") or die "Can't open $host.log: $!";   ### target host as filename
while (<$channel>) {
chomp;
print $OUTPUTFILE "$_";
} close $OUTPUTFILE or die "$OUTPUTFILE: $!";

This is the end of the journal, my script is now looks like the following:
#
# usage: perl cisco.pl (target_host) (ssl_password) (enable_password)
#
use Net::SSH2;
use warnings;
use strict;
my $host = $ARGV[0];            ### target host as input string1
my $user = "admin";
my $password = $ARGV[1];        ### ssh password as input string2
my $secret = $ARGV[2];        ### enable password as input 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 password 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 "$_";
}
 
close $OUTPUTFILE or die "$OUTPUTFILE: $!";

Thursday, August 27, 2009

High CPU on a Catalyst switch running IOS | Jeff Greene

High CPU on a Catalyst switch running IOS | Jeff Greene

When searching for High CPU caused by PIM Process, found the above URL explaining the steps of troubleshooting High CPU due to multicast configuration.

Monday, August 10, 2009

Sys-reset Script:: Network Stuff

Sys-reset Script:: Network Stuff

When trying to reset the F5 Big-IP LTM to its factory default settings, found solutions from the above URL.

Note: on using "sys-reset" command, I encountered "unrecoverable error" in few attempts, hence I used "sys-reset -u" instead.


NIL - Designing Fast Converging BGP Networks

NIL - Designing Fast Converging BGP Networks

Found this URL, when handling queries about network convergence issue involving BGP protocol.

Monday, August 03, 2009

An Illustrated Guide to IPsec

An Illustrated Guide to IPsec

When looking for IPsec information found this very useful and easy-to-understand paper.

Cisco Support Wiki

Cisco Support Wiki

A collaborative site for Cisco users

Cisco Learning Home - The Cisco Learning Network

Cisco Learning Home - The Cisco Learning Network

Useful information for learning Cisco products

System Test Architecture Reference Manual for IP Telephony


http://www.cisco.com/en/US/docs/voice_ip_comm/uc_system/GB4.3/ipt_emea_starm/GB4E.pdf

When looking for Cisco IP Telephony UAT template, found the above URL

Cisco Connection Documentation

Cisco Connection Documentation

Direct access to CCO Documentation page.

Cisco Unity Integration with Multiple Clusters of Cisco Unified Communications Manager (All Versions of Cisco Unity)� [Cisco Unity] - Cisco Systems

Cisco Unity Integration with Multiple Clusters of Cisco Unified Communications Manager (All Versions of Cisco Unity)� [Cisco Unity] - Cisco Systems

When looking for the maximum number of CallManager cluster supported by Cisco Unity, found the above URL.

QoS for the Cisco 6500 (Revisited)

QoS for the Cisco 6500 (Revisited)

Useful information about QoS implementation on Catalyst 6500 series.

Wednesday, July 02, 2008

Cisco Support Wiki

Cisco Support Wiki
A collaborative site for Cisco users

Tuesday, August 29, 2006

Configuring CallManager Express and CUE

August 2006
Category: Configuration

The following scenario shows how to setup the CME and CUE. In this
setup there are two types of call flow.

Download detail @ http://nuone.freewebpage.org

Friday, August 18, 2006

IPMA Wizard can be re-run.

January 2006
Category: Configuration
CallManager Version: Unknown

In any cases, you may need to run IPMA Wizard more than once. However,
the wizard will allow you to run it once only. I found two posts on
Cisco Netpro explaining that IPMA Wizard can be re-run. The first post
was published by Jason Young on Nov-18, 2003. However, this post is no longer available now, use the following URL instead:

http://tinyurl.com/n5l8a

The second post was published by Gonzalo Gasca on Jan-30, 2005, at the
following URL:

http://tinyurl.com/nvf5p

Thursday, August 17, 2006

Configuring XMediusFAX with Cisco IOS Gateway

August 2006
Category: Configuration

The scenario shows how to setup the XMediusFAX server to work with Cisco IOS gateway. In this setup in/outbound fax will be terminated only at the XMediusFAX server. The integration with Microsoft Outlook as the client’s email application is beyond the scope of this document.

Download detail @ http://nuone.freewebpage.org

Veritas NetBackup process failed after 1 hour

January 2006
Category: Troubleshooting

When activating the Veritas NetBackup process on the Backup Server, the backup run smoothly only for about 1 hour. After 1 hour, Veritas NetBackup application reported that the backup process had failed and terminated the backup session.
Veritas logs\bpbrm:
13:01:01.546 [3456.3464] <2> bpbrm send_parent_msg: WROTE qwerty01_1136261265 60032 0 28106.806
13:01:03.046 [3456.3464] <2> bpbrm read_media_msg: read from media manager: WROTE qwerty01_1136261265 60032 0 28108.797
13:01:03.046 [3456.3464] <2> bpbrm send_parent_msg: WROTE qwerty01_1136261265 60032 0 28108.797
13:01:05.046 [3456.3464] <2> get_long: (1) cannot read (byte 1) from network: An existing connection was forcibly closed by the remote host.
13:01:05.046 [3456.3464] <16> bpbrm read_parent_msg: Cannot read from parent socket 500, An existing connection was forcibly closed by the remote host. (10054)
.....
13:01:07.578 [3456.3464] <2> bpbrm read_media_msg: read from media manager: WROTE qwerty01_1136261265 60032 0 28111.175
13:01:07.578 [3456.3464] <2> bpbrm read_media_msg: read from media manager: ERROR 150
13:01:07.578 [3456.3464] <2> bpbrm Exit: ERROR 13 sent to parent process

Veritas logs\bpkar:
1:01:05.781 PM: [1460.3264] <2> tar_base::V_vTarMsgW: ERR - bpbkar exiting because backup is aborting
1:01:05.781 PM: [1460.3264] <2> tar_base::V_vTarMsgW: INF - EXIT STATUS 40: network connection broken


Download detail @ http://nuone.freewebpage.org

Non-Cisco wireless client failed to associate with Vxworks AP using 802.1x PEAP

February 2006
Category: Troubleshooting

Wireless clients with Acer laptop had connectivity and authentication problem at 6th floor, but had no issue at 2nd floor. Wireless clients withDell laptop had no problems regardless where they were. Users experienced problem when using 802.1x PEAP authentication method.

Download detail @ http://nuone.freewebpage.org