#!#/usr/bin/perl
use
vars
qw($AUTOLOAD @ISA $VERSION $DEBUG)
;
@ISA
=
qw(Net::Telnet::Cisco)
;
$VERSION
=
"0.5beta"
;
sub
new {
my
$class
=
shift
;
my
(
$self
,
$host
,
%args
);
$self
=
$class
->SUPER::new(
@_
) or
return
;
our
(
$platform
,
$model
,
$iosver
,
@config
);
return
$self
;
}
sub
login {
my
$self
=
shift
;
$self
->SUPER::login(
@_
) or
return
;
$self
->cmd(
"terminal length 0"
);
return
;
}
sub
getModVer {
return
$VERSION
;
}
sub
getIOSVer {
my
$cmd
=
"show version"
;
my
$self
=
shift
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /, Version (.+),/ ) {
return
$1;
}
}
return
0;
}
sub
getCPU {
my
$cmd
=
"show process cpu"
;
my
%ret
= (
"5sec"
=>
"na"
,
"1min"
=>
"na"
,
"5min"
=>
"na"
);
my
$self
=
shift
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /five seconds: (.+)\/.+; one minute: (.+); five minutes: (.+)/ ) {
$ret
{
"5sec"
} = $1;
$ret
{
"1min"
} = $2;
$ret
{
"5min"
} = $3;
}
}
return
%ret
;
}
sub
listInts {
my
$self
=
shift
;
my
@ret
;
my
$int
;
my
$cmd
=
"sh ip interface brief"
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /^Interface/i ) { }
elsif
(
$line
=~ /^-----/ ) { }
elsif
(
$line
=~ /^\s/ ) { }
elsif
(
$line
=~ /^\W/ ) { }
else
{
my
$int
=
substr
(
$line
, 0, 23 );
$int
=~ s/\s+$//g;
push
(
@ret
,
$int
);
}
}
return
@ret
;
}
sub
listVLANs {
my
$self
=
shift
;
my
@ret
;
my
$cmd
=
"show vlan brief"
;
my
(
$vlanid
,
$vlanname
);
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /^VLAN/i ) { }
elsif
(
$line
=~ /^----/ ) { }
elsif
(
$line
=~ /^\s+/ ) { }
else
{
$vlanid
=
substr
(
$line
, 0, 4);
$vlanname
=
substr
(
$line
, 5, 30 );
if
(
$vlanid
=~ /100[2-5]/ ) { }
else
{
$vlanid
=~ s/\s+$//g;
push
(
@ret
,
$vlanid
);
}
}
}
return
@ret
;
}
sub
getIntState {
my
(
$self
,
@args
) =
@_
;
my
%result
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"sh interface "
.
$if
;
my
@output
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@output
) {
if
(
$line
=~ /
$if
is (.+), line protocol is (.+) / ) {
$result
{
'port'
} = $1;
$result
{
'lineprotocol'
} = $2;
return
%result
;
}
else
{ }
}
}
sub
getIntDesc {
my
(
$self
,
@args
) =
@_
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"sh interface "
.
$if
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /Description: (.+)/ ) {
return
$1;
}
else
{ }
}
return
0;
}
sub
getEthSpeed {
my
(
$self
,
@args
) =
@_
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"sh interface "
.
$if
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /Auto Speed \((.+)\),/ ) {
return
$1;
}
elsif
(
$line
=~ /, (.+)Mb\/s/ ) {
return
$1;
}
}
return
0;
}
sub
getEthDuplex {
my
(
$self
,
@args
) =
@_
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"sh interface "
.
$if
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /\s+Auto-duplex \((.{4})\),/ ) {
return
$1;
}
elsif
(
$line
=~ /\s+(.+)-duplex/ ) {
if
( $1 eq
"Auto"
) { }
else
{
return
$1;
}
}
}
return
0;
}
sub
getIntBandwidth {
my
(
$self
,
@args
) =
@_
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"sh interface "
.
$if
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /\s+BW (.+) Kbit/ ) {
return
$1;
}
else
{ }
}
}
sub
getIntInputRate {
my
(
$self
,
@args
) =
@_
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"sh interface "
.
$if
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /5 minute input rate (.+) bits/ ) {
return
$1;
}
else
{ }
}
}
sub
getIntInputErrors {
my
(
$self
,
@args
) =
@_
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"sh interface "
.
$if
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /(.+) input errors,/ ) {
return
$1;
}
else
{ }
}
return
0;
}
sub
getIntOutputRate {
my
(
$self
,
@args
) =
@_
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"sh interface "
.
$if
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /5 minute output rate (.+) bits/ ) {
return
$1;
}
else
{ }
}
}
sub
getIntOutputErrors {
my
(
$self
,
@args
) =
@_
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"sh interface "
.
$if
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /(.+) output errors,/ ) {
return
$1;
}
else
{ }
}
return
0;
}
sub
findVLAN {
my
(
$self
,
@args
) =
@_
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"sh interface "
.
$if
.
" status"
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /^Port/ ) { }
elsif
(
$line
=~ /^-----/ ) { }
elsif
(
$line
=~ /^\s+/ ) { }
else
{
my
$vlan
=
substr
(
$line
, 40, 8 );
$vlan
=~ s/\s+$//g;
return
$vlan
;
}
}
return
0;
}
sub
getConfig {
my
$self
=
shift
;
my
$cmd
=
"show running-config"
;
my
@result
=
$self
->cmd(
$cmd
);
return
@result
;
}
sub
getModel {
my
$self
=
shift
;
my
$cmd
=
"sh ver"
;
my
$plat
;
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /^IOS \(tm\) (.+) Software/ ) {
return
$1;
}
elsif
(
$line
=~ /cisco (.+) processor/ ) {
return
$1;
}
}
return
0;
}
sub
getPlatform {
my
(
$self
,
@args
) =
@_
;
my
$platform
;
my
$model
=
$args
[0];
if
(
$model
=~ /29.0/ ||
$model
=~ /3750/ ) {
return
"s"
;
}
elsif
(
$model
=~ /RSP/ ||
$model
=~ /7200/ ) {
return
"r"
;
}
return
0;
}
sub
getIntCAM {
my
(
$self
,
@args
) =
@_
;
my
$int
= harmonizeInts(
$args
[0] );
my
$cmd
=
"show mac-address-table interface "
.
$int
;
my
@ret
;
my
@output
=
$self
->cmd(
$cmd
);
my
$model
=
$self
->getModel();
foreach
my
$line
(
@output
) {
my
$mac
;
if
(
$line
=~ /(\w{4}\.\w{4}\.\w{4})/ ) {
push
(
@ret
, $1 );
}
}
return
@ret
;
}
sub
getIntARP {
my
(
$self
,
@args
) =
@_
;
my
$int
= harmonizeInts(
$args
[0] );
my
$cmd
=
"show arp"
;
my
@ret
;
my
@output
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@output
) {
chomp
$line
;
if
(
$line
=~ /
$int
/ &&
$line
=~ /(\w{4}\.\w{4}\.\w{4})/ ) {
push
(
@ret
, $1 );
}
}
return
@ret
;
}
sub
arpLookup {
my
(
$self
,
@args
) =
@_
;
my
$cmd
=
"show arp"
;
my
@output
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@output
) {
chomp
$line
;
if
(
$line
=~ /
$args
[0]/ ) {
my
$ip
=
substr
(
$line
, 10, 15 );
if
(
length
(
$ip
) == 0 ) { }
else
{
return
$ip
;
}
}
}
return
0;
}
sub
getACLs {
my
$self
=
shift
;
my
$cmd
=
"show access-lists"
;
my
@ret
;
my
@output
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@output
) {
if
(
$line
=~ /access list (.+)\n/ ) {
push
(
@ret
, $1 );
}
}
return
@ret
;
}
sub
getSNMPComm {
my
$self
=
shift
;
my
@ret
;
my
@output
=
$self
->getConfig();
foreach
my
$line
(
@output
) {
if
(
$line
=~ /snmp-server community (.+) / ) {
push
(
@ret
, $1 );
}
}
return
@ret
;
}
sub
privLevel {
my
$self
=
shift
;
my
$cmd
=
"show privilege"
;
my
$ret
= 0;
my
@output
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@output
) {
if
(
$line
=~ /^Current privilege level is (\d{1,2})/ ) {
$ret
= $1;
}
}
return
$ret
;
}
sub
getVTP {
my
$self
=
shift
;
my
$cmd
=
"show vtp status"
;
my
(
$ver
,
$mode
,
$domain
);
my
%ret
;
my
@output
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@output
) {
if
(
$line
=~ /^VTP Version\s+: (.)\n/ ) {
$ver
= $1;
}
elsif
(
$line
=~ /^VTP Operating Mode\s+: (.+)\n/ ) {
$mode
= $1;
}
elsif
(
$line
=~ /^VTP Domain Name\s+: (.+)\n/ ) {
$domain
= $1;
}
}
%ret
= (
version
=>
$ver
,
mode
=>
$mode
,
domain
=>
$domain
,
);
return
%ret
;
}
sub
getIntACL {
my
(
$self
,
@args
) =
@_
;
my
$inacl
= 0;
my
$outacl
= 0;
my
$cmd
=
"show ip int "
.
$args
[0];
my
@outacl
= (
"Outgoing access list is "
,
"Outbound access list is "
);
my
@inacl
= (
"Inbound access list is "
,
"Inbound access list is "
);
my
%ret
;
my
@output
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@output
) {
ACL:
{
foreach
my
$acl
(
@outacl
) {
if
(
$line
=~ /
$acl
(.+)/ ) {
$outacl
= $1;
last
ACL;
}
}
foreach
my
$acl
(
@inacl
) {
if
(
$line
=~ /
$acl
(.+)/ ) {
$inacl
= $1;
last
ACL;
}
}
}
}
if
(
$inacl
eq
"not set"
||
$inacl
eq
""
) {
$inacl
= 0;
}
if
(
$outacl
eq
"not set"
||
$outacl
eq
""
) {
$outacl
= 0;
}
%ret
= (
inbound
=>
$inacl
,
outbound
=>
$outacl
,
);
return
%ret
;
}
sub
getIPRoute {
my
(
$self
,
@args
) =
@_
;
my
%ret
= (
route
=> 0,
protocol
=> 0,
nexthop
=> 0);
my
$cmd
=
"show ip route "
.
$args
[0];
my
@result
=
$self
->cmd(
$cmd
);
foreach
my
$line
(
@result
) {
if
(
$line
=~ /Routing entry
for
(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2})/ ) {
$ret
{
'route'
} = $1;
}
if
(
$line
=~ /Known via \"(.+)\"/ ) {
$ret
{
'protocol'
} = $1;
}
if
(
$line
=~ /directly connected, via (.+)\W/ ) {
$ret
{
'nexthop'
} = $1;
}
if
(
$line
=~ /\* (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) {
$ret
{
'nexthop'
} = $1;
}
}
return
%ret
;
}
sub
getNTP {
my
$self
=
shift
;
my
%ret
;
my
(
$server
,
$source
,
$mode
);
my
@result
=
$self
->getConfig();
foreach
my
$line
(
@result
) {
if
(
$line
=~ /^ntp server (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) source (\w+) (\w+)/ ) {
$server
= $1;
$source
= harmonizeInts( $2 );
$mode
= $3;
}
elsif
(
$line
=~ /^ntp server (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) prefer/ ) {
$server
= $1;
$mode
=
"prefer"
;
}
elsif
(
$line
=~ /^ntp source (.+)$/ ) {
$source
= harmonizeInts( $1 );
}
}
%ret
= (
server
=>
$server
,
source
=>
$source
,
mode
=>
$mode
);
return
%ret
;
}
sub
disableInt {
my
(
$self
,
@args
) =
@_
;
my
$result
;
my
$if
=
&harmonizeInts
(
$args
[0] );
my
$cmd
=
"shutdown"
;
eval
{
$self
->cmd(
"configure terminal"
);
$self
->cmd(
"interface $if"
);
$self
->cmd(
"$cmd"
);
$self
->cmd(
"exit\nexit"
);
};
if
(
length
(
$self
->errmsg() ) > 0 ) {
return
0;
}
return
1;
}
sub
saveConfig {
my
$self
=
shift
;
my
$cmd
=
"write memory"
;
if
( !
$self
->cmd(
$cmd
) ) {
print
"Couldn't do it"
;
<STDIN>;
return
0;
}
return
1;
}
sub
harmonizeInts {
my
$input
=
shift
;
my
@FastEthernet
=
qw(FastEthernet FastEth Fast FE Fa F)
;
my
@GigEthernet
=
qw(GigabitEthernet GigEthernet GigEth GE Gi G)
;
my
@Ethernet
=
qw(Ethernet Eth E)
;
my
@Serial
=
qw(Serial Se S)
;
my
@PortChannel
=
qw(PortChannel Port-Channel Po)
;
my
@POS
=
qw(POS P)
;
my
@VLAN
=
qw(VLAN VL V)
;
my
@LOOPBACK
=
qw(Loopback Loop Lo)
;
IFS:
{
foreach
my
$fe
(
@FastEthernet
)
{
if
(
$input
=~ /^
$fe
\d/i )
{
$input
=~ /^
$fe
(.+)\b/i;
$input
=
"FastEthernet"
. $1;
last
IFS;
}
}
foreach
my
$ge
(
@GigEthernet
)
{
if
(
$input
=~ /^
$ge
\d/i )
{
$input
=~ /^
$ge
(.+)\b/i;
$input
=
"GigabitEthernet"
. $1;
last
IFS;
}
}
foreach
my
$e
(
@Ethernet
)
{
if
(
$input
=~ /^
$e
\d/i )
{
$input
=~ /^
$e
(.+)\b/i;
$input
=
"Ethernet"
. $1;
last
IFS;
}
}
foreach
my
$s
(
@Serial
)
{
if
(
$input
=~ /^
$s
\d/i )
{
$input
=~ /^
$s
(.+)\b/i;
$input
=
"Serial"
. $1;
last
IFS;
}
}
foreach
my
$po
(
@PortChannel
)
{
if
(
$input
=~ /^
$po
\d/i )
{
$input
=~ /^
$po
(.+)\b/i;
$input
=
"Port-channel"
. $1;
last
IFS;
}
}
foreach
my
$pos
(
@POS
)
{
if
(
$input
=~ /^
$pos
\d/i )
{
$input
=~ /^
$pos
(.+)\b/i;
$input
=
"POS"
. $1;
last
IFS;
}
}
foreach
my
$vlan
(
@VLAN
)
{
if
(
$input
=~ /^
$vlan
\d/i )
{
$input
=~ /^
$vlan
(.+)\b/i;
$input
=
"VLAN"
. $1;
last
IFS;
}
}
foreach
my
$lb
(
@LOOPBACK
)
{
if
(
$input
=~ /^
$lb
\d/i )
{
$input
=~ /^
$lb
(.+)\b/i;
$input
=
"Loopback"
. $1;
last
IFS;
}
}
return
0;
}
$input
=~ s/\s+//g;
return
$input
;
}
1;
Hide Show 50 lines of Pod
=head1 NAME
Net::Telnet::Cisco::IOS -- Manage Cisco IOS Devices
=head1 DESCRIPTION
Net::Telnet::Cisco::IOS (NTCI) is an extension of Joshua Kereos
's Net::Telnet::Cisco module and provides an easy way to manage and monitor Cisco IOS devices. I'
ll mention this a lot, but make sure you
read
up on Net::Telnet::Cisco
for
a lot of information.
=head1 WHEN TO USE NTCI
NTCI can
do
a lot, but it
's not the best way to do all of it. I'
d suggest you take a look at some SNMP solutions. It
's up to you to figure out when and where you want to use it, but don'
t
say
I didn't
warn
you. :)
=head1 METHODS
=head1 SYNOPSIS
$connection
= Net::Telnet::Cisco::IOS->new(
Host
=>
'hostname'
);
$connection
->login(
Name
=>
'username'
,
Password
=>
'password'
);
if
(
$ver
=
$connection
->getIOSVer() ) {
print
"The device is running version "
.
$ver
.
"\n"
;
}
else
{
print
"Can't get the version:\n"
;
print
$connection
->errmsg();
}
$connection
->
close
();
=head1 MORE INFO
=head1 AUTHOR
NTCI is written by Aaron Conaway. He can be reached at aaron at aconaway period com.
=head1 COPYRIGHT AND LICENSE
(c) 2005 by Aaron Conaway.
NTCI is distributed under the GPL and may be used by anyone without changes.