Just another Cisco blog
Posts tagged Script
Great Perl Script
Jan 15th
Jason from SYN/ACK Networks did a write up on his Perl script (rtrcommander) which helps when you need to modify a large number of routers quickly. I figured I’d post it here so I’ll never lose it, hopefully some of you guys will find it useful as well.
Check out his post for a good explanation. Here’s the script itself:
#!/usr/bin/perl
#
# This file is part of Mr. Audit.
#
# Mr. Audit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mr. Audit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
#
###################################################################################
#
# This script gets the configs for every device in the audit database.
# The configs are used by other scripts for the automated audit.
#
# Mr. Audit was written by Jason Rowley - jrowley<at>convergedinnovations<dot>com
#
# This script is version 2.00
# Last updated September 17, 2009 @ 18:14 EST
#
###################################################################################
use Net::Telnet::Cisco;
use Getopt::Std;
use IO::Prompt;
use FileHandle;
### variables
#my $DEBUG = "true";
my $DEBUG = "false";
my $VERSION = "2.0";
my $logfile = "";
my $combined = 0;
my $username = "";
my $password = "";
my $host = "";
### arrays
my @routerlist = ();
my @commandlist = ();
###
### Begin main
###
init();
getrtrs();
getcmds();
foreach (@routerlist)
{
chomp($_);
$host = $_;
print "\nHOSTNAME: $host\n";
openrtr();
sendcmds();
closertr();
}
exit;
###
### Initializes stuff
###
sub init
{
usage() unless $ARGV[0];
my $opt_string = 'hu:p:r:c:l:';
getopts( "$opt_string", \%opt ) or &usage;
usage() if $opt{h};
if (!$opt{u})
{
$username = prompt("username: ");
chomp($username);
}
else
{
$username = $opt{u};
}
if (!$opt{p})
{
### got username, prompt for password
$password = prompt("password: ", -e => '*');
chomp($password);
}
else
{
$password = $opt{p};
}
if (!$opt{r})
{
print "Missing router list\n";
usage();
exit;
}
if (!$opt{c})
{
print "Missing command file\n";
usage();
exit;
}
if ($opt{l})
{
$logfile = $opt{l};
$combined = 1;
}
}
###
### Displays help
###
sub usage
{
print STDERR << "EOF";
New and Improved Router Commander $VERSION
Usage:
$0 [-h] -u <username> [-p <password>] -r <rtrlist> -c <cmdlist> [-l <loglocation>]
-h : prints this message
-u : username
-p : password - if not specified, will be prompted
-r : file containing list of routers
-c : file containing commands to run
-l : file where we should log to; defaults to "ipaddress.log"
Examples:
rtrcmd -u username -p password -r routerlist -c commandlist
rtrcmd -u username -r routerlist -c commandlist -l mycombinedlogfile.txt
EOF
exit;
}
###
### Get routers
###
sub getrtrs
{
my $rf = $opt{r};
open (RF, $rf);
@routerlist = <RF>;
close(RF);
}
###
### Get commands
###
sub getcmds
{
my $cf = $opt{c};
open (CF, $cf);
@commandlist = <CF>;
close(CF);
}
###
### Send commands
###
sub sendcmds
{
foreach (@commandlist)
{
chomp($_);
print "Sending: $_\n";
my @temp = $::OPENRTR->cmd("$_");
if ($combined == 1)
{
open LOGFILE, ">>$logfile" or die $!;
print LOGFILE @temp;
close LOGFILE;
}
}
}
sub openrtr
{
if ($combined == 1)
{
if ($::OPENRTR = Net::Telnet::Cisco->new(Host => $host, Errmode => "return"))
{
if ($::OPENRTR->login($username, $password))
{
my @temp = $::OPENRTR->cmd("term len 0");
}
else
{
print "Invalid username or password while trying $host\n";
$::OPENRTR->close;
exit;
}
}
else
{
print "Could not connect to $host\n";
exit;
}
}
else
{
if ($::OPENRTR = Net::Telnet::Cisco->new(Host => $host, Input_log => "$host.log", Errmode => "return"))
{
if ($::OPENRTR->login($username, $password))
{
my @temp = $::OPENRTR->cmd("term len 0");
}
else
{
print "Invalid username or password while trying $host\n";
$::OPENRTR->close;
exit;
}
}
else
{
print "Could not connect to $host\n";
exit;
}
}
}
sub closertr
{
$::OPENRTR->close;
} |
Playing With EEM
Jan 5th
Yesterday someone asked me how to periodically save the running config using an EEM (Embedded Event Manager) applet. I’d never used EEM before (I’m terrified of code), but I decided to do some research to see how it could be done. It was surprisingly easy. Here’s what EEM is according to Cisco:
Embedded Event Manager (EEM) is a powerful and flexible subsystem in Cisco IOS that provides real-time network event detection and onboard automation. Using EEM, customers can adapt the behavior of their network devices to align with their business needs.
This applet was needed so the work done by his helpdesk team would be written to the config. They are given very limited access and are unable to execute the “wr” command. The first thing I gave him was this:
event manager applet WR_CONFIG event syslog pattern ".*%SYS-5-CONFIG_I.*" action 1.1 cli command "enable" action 1.2 cli command "wr" action 1.3 syslog msg "Config changed by user, new config has been written" |
TCL Ping Script
Dec 29th
TCL is a scripting language built into IOS (since 12.3(2)T according to Cisco.com), it can be very handy. I use this TCL script quite a bit, but it’s always few and far between, so I find myself Googling for it every time. I decided to post it here so I always know where to find it, heh. This TCL script it pretty basic, you just type it out in a text editor and past it in the console.
Here’s a simple example:
foreach address {
192.168.10.1
192.168.15.1
192.168.25.1} { ping $address } |
With that we get this:
EDGE#tclsh
EDGE(tcl)#foreach address {
+> 192.168.10.1
+> 192.168.15.1
+> 192.168.25.1} { ping $address }
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.10.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/3/4 ms
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.15.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/3/4 ms
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.25.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/3/8 ms |
We could get a bit more complex using some extended ping options, like this:
foreach address {
192.168.10.1
192.168.15.1
192.168.25.1} { ping $address source lo254 repeat 3 } |
In this one we are pinging with a source address of Loopback 254, and only sending 3 pings. It looks like this:
EDGE#tclsh
EDGE(tcl)#foreach address {
+> 192.168.10.1
+> 192.168.15.1
+> 192.168.25.1} { ping $address source lo254 repeat 3 }
Type escape sequence to abort.
Sending 3, 100-byte ICMP Echos to 192.168.10.1, timeout is 2 seconds:
Packet sent with a source address of 192.168.254.254
!!!
Success rate is 100 percent (3/3), round-trip min/avg/max = 1/3/4 ms
Type escape sequence to abort.
Sending 3, 100-byte ICMP Echos to 192.168.15.1, timeout is 2 seconds:
Packet sent with a source address of 192.168.254.254
!!!
Success rate is 100 percent (3/3), round-trip min/avg/max = 1/3/8 ms
Type escape sequence to abort.
Sending 3, 100-byte ICMP Echos to 192.168.25.1, timeout is 2 seconds:
Packet sent with a source address of 192.168.254.254
!!!
Success rate is 100 percent (3/3), round-trip min/avg/max = 1/3/4 ms |
Short post, just something I needed a place for. Hope it’s useful to some readers.
Recent Comments