Cisco Perl lib utility

I have been working and deploying Cisco Voice Unified Communications for a while now and have been using scripts in [perl|python] or apps in Java to speed up deployment, in some case take out my own human fallibility out or just take out the boring repetitive tasks of the project.

Instead of just publishing a number of different scripts I have through it would be more useful to put the scripts in a Object Oriented Perl library that can then be used as needed depending on what is been solved. It’s important to note that you will need some Perl scripting knowledge to use this, not by any means an expert/guru level but have gone pass the “Hello World!” starter script.

Basic pre-requests

1. Basic knowledge of Perl scripting
2. Windows O/S (I know allot of folk use Mac, including myself but at work I HAVE to use Win so can’t test with my Mac)
3. Perl ver 5.10+ (on my Win machine i use ActiveState) to check the current version type perl -v in a dos prompt

Perl version

Perl version

Install and use the lib

To use the library you have 2 options
Option 1
Download the lib and unzip it where your writing your script. Include the following line at the top of your script: use NetPacketLib::Cisco::RouterIos::CommonCmd; Note this path is relative!

Use Lib Path NetPacket

Use Lib Path NetPacket

Option2
Download the lib, unzip it and place it in global directory of modules for Perl. This directory(s) is where Perl searches every time a script is run to find the relevant modules been used by the script. To find the location of these directory(s) the type the following cmd in a dos-prompt perl -e "print \"@INC\""

Perl @INC

Perl @INC

From the screen shot you can see that there 2 directories I can place the NetPacket folder into C:/Perl64/site/lib and C:/Perl64/lib

Dependencies

Dependencies that the lib has will need to be installed for it to work correctly. They can all be download from Perl CPAN. How you install them will depend on the on the platform Perl is running on and what flavour your running. If your using Windows O/S with ActiveState then you can use Perl Package Manager (PPM) to get all the dependencies installed. The default PPM repository will have all the dependencies with the exception of Net::SSH2. You will need to add repository http://www.sisyphusion.tk/ppm/package.xml to PPM and you will find the Net::SSH2 in there.

Download

Current version of the lib NetPacketLib v0.02 7th July 2015

  • Added the deletePhoneItlCert function to allow you to delete the Phones security certificates

Older version of the lib from the following link:NetPacketLib v0.01 1st April 2014

Lib table content
Phone Modules

NetPacketLib::Cisco::IpPhone::Services::CommonCmd

NetPacketLib::Cisco::IpPhone::Services::GetScreenshot

NetPacketLib::Cisco::IpPhone::Services::PushBackground

NetPacketLib::Cisco::IpPhone::Services::DeviceInformation

Router and Switch Modules

NetPacketLib::Cisco::RouterIos::CommonCmd

NetPacketLib::Cisco::RouterIos::FilterCDP

Phone Modules

NetPacketLib::Cisco::IpPhone::Services::CommonCmd

Description

Used to either simulate a key press on the phone, delete the security Certs on the phone or reset a Cisco IP Phone. The user name and password are the user details of the user associated with the phone in CUCM

Dependencies:

Moose
LWP::UserAgent
HTTP::Request::Common

Methods

resetPhone();
sendKeyPress($self, keyCode, KeyName);

Example

#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

use NetPacketLib::Cisco::IpPhone::Services::CommonCmd;

my $resetPhone = NetPacketLib::Cisco::IpPhone::Services::CommonCmd->new(
phoneIP => ‘10.4.5.53’,
usr => ‘alexis’,
pass => ‘@netpacket’,
);

#An example of deleting the security certificates on the phone
$resetPhone->deletePhoneItlCert();
#An example of resetting the phone
$resetPhone->resetPhone();
#An example of just sending the key press “Setting” to the phone
$resetPhone->sendKeyPress(“Key:Settings”,”Settings”)

 

Output

Trying to reset Phone: 10.4.5.53
Successfully sent all key press for resetting the phone

NetPacketLib::Cisco::IpPhone::Services::GetScreenshot

Description

Get the current screen shot been displayed on the IP Phone and save is as “Phone_IP_Address”-“Time”.bmp in the current directory. The user name and password are the user details of the user associated with the phone in CUCM.

Dependencies:

Moose;
LWP::UserAgent;
HTTP::Request::Common;

Methods

getImg();

Example

#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

use NetPacketLib::Cisco::IpPhone::Services::GetScreenshot;

my $phoneSS = NetPacketLib::Cisco::IpPhone::Services::GetScreenshot->new(
phoneIP => ‘10.4.5.53’,
usr => ‘alexis’,
pass => ‘@netpacket’,
);
$phoneSS->getImg();

Output

Trying to get Screenshot from:10.4.5.53
Response received from:10.4.5.53
Screenshot saved to:10.4.5.53 13-04-30.bmp

NetPacketLib::Cisco::IpPhone::Services::PushBackground

Description

Push out an image from an Web Server to an IP Phone. The user name and password are the user details of the user associated with the phone in CUCM. To setup the Web browser and details on the image format and size please read the instructions here

Dependencies:

Moose;
LWP::UserAgent;
HTTP::Request::Common;

Methods

pushBackground();

Example

#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use NetPacketLib::Cisco::IpPhone::Services::PushBackground;

my $services = NetPacketLib::Cisco::IpPhone::Services::PushBackground->new(
phoneIP => ‘10.4.5.53’,
usr => ‘alexis’,
pass => ‘@netpacket’,
webServerIP => ‘http://10.4.4.6/Desktop’,
imgFullSize => ‘imgFullSize.png’,
imgThumSize => ‘imgThumSize.png’,
DEBUG => 0
);

if($services->pushBackground())
{
print “OK\n”
}
else
{
print “Error\n”
}

Output

Trying to push image to 10.4.5.53 IP Phone
Image uploaded to IP Phone successfully
OK

NetPacketLib::Cisco::IpPhone::Services::DeviceInformation

Description

Used to query a Phone and retrieve information about it. Used as a basis to all other methods.

Dependencies:

Moose;
XML::LibXML;
LWP::UserAgent;

Methods

getPhoneModel();
getMACAddress();
getHostName();
getAppLoadID();
getVersionID();
getSerialNumber();

Example

#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

use NetPacketLib::Cisco::IpPhone::Services::DeviceInformation;

my $deviceInfo = NetPacketLib::Cisco::IpPhone::Services::DeviceInformation->new(
phoneIP => ‘10.4.5.53’,
);

print ‘Phone Model :’ .$deviceInfo->getPhoneModel() .”\n”;
print ‘Phone MAC :’ .$deviceInfo->getMACAddress() .”\n”;
print ‘Phone Host :’ .$deviceInfo->getHostName() .”\n”;
print ‘Phone App ID :’ .$deviceInfo->getAppLoadID() .”\n”;
print ‘Phone Ver ID :’ .$deviceInfo->getVersionID() .”\n”;
print ‘Phone Serial No:’ .$deviceInfo->getSerialNumber() .”\n”;

Output

Phone Model :CP-7945G
Phone MAC :D824BD2650A5
Phone Host :SEPDG24FF2650A5
Phone App ID :jar45sccp.9-3-1ES8.sbn
Phone Ver ID :9.3(1SR1.1S)
Phone Serial No:FCH2549AQG

Router and Switch Modules

NetPacketLib::Cisco::RouterIos::CommonCmd

Description

Contain the basic cmd in regards to a Cisco Switch or Router running IOS. Currently only supports SSH. The basicAudit() method will log into the device and execute the following cmd’s [term len 0,show clock, dir, sh ip int brief, sh ver, sh inventory & sh run] the output is write to a file.

Dependencies:

use Moose;
use Net::SSH2;

Methods

logIntoDevice()
enableMode()
sendCmd()
basicAudit()

Example

#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

use NetPacketLib::Cisco::RouterIos::CommonCmd;

my $gwLiv = NetPacketLib::Cisco::RouterIos::CommonCmd->new(
deviceIP => ‘192.168.1.1’,
usr => ‘alexis’,
pass => ‘@netpacket’,
DEBUG => ‘0’,
);
$gwLiv->basicAudit();

Output

Starting audit for:192.168.1.1
In enable mode.
Sending cmd: term len 0
Done
Sending cmd: show clock
Done
Sending cmd: dir
Done
Sending cmd: sh ip int brief
Done
Sending cmd: sh ver
Done
Sending cmd: sh inventory
Done
Sending cmd: sh run
Done
Audit for:192.168.1.1 done
!Note The output of the cmd’s above are write to file 192.168.1.1-“TIME”.log

NetPacketLib::Cisco::RouterIos::FilterCDP

Description

Log into a switch/router run the cmd show cdp neighbors detail take the output and put it into quick easy to read format. Output can be printed on screen, written to file, push to a server etc.

Dependencies:

NetPacketLib::Cisco::RouterIos::CommonCmd;

Methods

getCDPdata();

Example

#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

use NetPacketLib::Cisco::RouterIos::FilterCDP;

my @resultArr = $cdpTest->getCDPdata();
foreach(@resultArr)
{
my %port = %{$_};
say “*****************************”;
say “deviceID :$port{deviceID}”;
say “deviceIP :$port{deviceIP}”;
say “devicePlat :$port{devicePlat}”;
say “deviceIntIn :$port{deviceIntIn}”;
say “deviceIntOut :$port{deviceIntOut}”;
say “deviceSoft :$port{deviceSoft}”;
say “intDuplex :$port{intDuplex}”;
say “deviceNatVlan:$port{deviceNatVlan}”;
say “deviceMangIP :$port{deviceMangIP}”;
}

Output

*****************************
deviceID :FOC15111VW5
deviceIP :192.1.2.61
devicePlat :Cisco NAM
deviceIntIn :GigabitEthernet3/40
deviceIntOut :External
deviceSoft :5.1(1a)
intDuplex :N/A
deviceNatVlan:N/A
deviceMangIP :N/A
*****************************
deviceID :VO01-01OCTS01.corp.org
deviceIP :10.2.2.134
devicePlat :Cisco 2811
deviceIntIn :GigabitEthernet3/12
deviceIntOut :FastEthernet0/0
deviceSoft :Version 12.4(25b)
intDuplex :full
deviceNatVlan:N/A
deviceMangIP :N/A
*****************************
deviceID :V001-020DAC01.corp.org
deviceIP :10.4.9.3
devicePlat :cisco WS-C4507R-E
deviceIntIn :TenGigabitEthernet1/4
deviceIntOut :TenGigabitEthernet3/1
deviceSoft :Version 12.2(53)SG1
intDuplex :full
deviceNatVlan:2104
deviceMangIP :10.4.9.3
*****************************
deviceID :MLIV001-020AAC01.corp.org
deviceIP :10.1.2.51
devicePlat :cisco WS-C4507R-E
deviceIntIn :TenGigabitEthernet1/1
deviceIntOut :TenGigabitEthernet3/1
deviceSoft :Version 12.2(53)SG1
intDuplex :full
deviceNatVlan:2101
deviceMangIP :10.4.9.1
*****************************
deviceID :V001-010BAC01.corp.org
deviceIP :10.4.1.5
devicePlat :cisco WS-C3750E-48PD
deviceIntIn :TenGigabitEthernet1/2
deviceIntOut :TenGigabitEthernet1/0/1
deviceSoft :Version 12.2(58)SE2
intDuplex :full
deviceNatVlan:2102
deviceMangIP :10.4.9.5
*****************************
deviceID :V001-010CWC01
deviceIP :10.4.4.27
devicePlat :AIR-CT5508-K9
deviceIntIn :GigabitEthernet3/1
deviceIntOut :GigabitEthernet0/0/1
deviceSoft :Product Version: 7.2.110.0
intDuplex :full
deviceNatVlan:N/A
deviceMangIP :N/A
*****************************
deviceID :010CCO02.corp..org
deviceIP :10.4.2.3
devicePlat :cisco WS-C6506-E
deviceIntIn :TenGigabitEthernet5/5
deviceIntOut :TenGigabitEthernet5/5
deviceSoft :Version 12.2(33)SXH5
intDuplex :full
deviceNatVlan:N/A
deviceMangIP :10.4.2.3

Tags:

About Alexis Katsavras

Working as Freelance Cisco Unified Communications Consultant in the UK. www.NetPacket.co.uk