| 1 |
#!/usr/local/bin/perl -w |
| 2 |
### |
| 3 |
### Print history of configuration management actions on Cisco router |
| 4 |
### using CISCO-CONFIG-MAN-MIB.my. |
| 5 |
### |
| 6 |
use strict; |
| 7 |
|
| 8 |
use BER "0.72"; |
| 9 |
use SNMP_Session "0.67"; |
| 10 |
use Time::HiRes; |
| 11 |
|
| 12 |
use Getopt::Long; |
| 13 |
|
| 14 |
my $version = '1'; |
| 15 |
|
| 16 |
while (defined $ARGV[0] && $ARGV[0] =~ /^-/) { |
| 17 |
if ($ARGV[0] =~ /^-v/) { |
| 18 |
if ($ARGV[0] eq '-v') { |
| 19 |
shift @ARGV; |
| 20 |
usage (1) unless defined $ARGV[0]; |
| 21 |
} else { |
| 22 |
$ARGV[0] = substr($ARGV[0], 2); |
| 23 |
} |
| 24 |
if ($ARGV[0] eq '1') { |
| 25 |
$version = '1'; |
| 26 |
} elsif ($ARGV[0] eq '2c') { |
| 27 |
$version = '2c'; |
| 28 |
} else { |
| 29 |
usage (1); |
| 30 |
} |
| 31 |
} elsif ($ARGV[0] eq '-h') { |
| 32 |
usage (0); |
| 33 |
exit 0; |
| 34 |
} else { |
| 35 |
usage (1); |
| 36 |
} |
| 37 |
shift @ARGV; |
| 38 |
} |
| 39 |
my $host = shift @ARGV || usage (1); |
| 40 |
my $community = shift @ARGV || "public"; |
| 41 |
usage (1) if $#ARGV >= $[; |
| 42 |
|
| 43 |
my $ccmHistoryEventTime = [1,3,6,1,4,1,9,9,43,1,1,6,1,2]; |
| 44 |
my $ccmHistoryEventCommandSource = [1,3,6,1,4,1,9,9,43,1,1,6,1,3]; |
| 45 |
my $ccmHistoryEventConfigSource = [1,3,6,1,4,1,9,9,43,1,1,6,1,4]; |
| 46 |
my $ccmHistoryEventConfigDestination = [1,3,6,1,4,1,9,9,43,1,1,6,1,5]; |
| 47 |
my $ccmHistoryEventTerminalType = [1,3,6,1,4,1,9,9,43,1,1,6,1,6]; |
| 48 |
my $ccmHistoryEventTerminalNumber = [1,3,6,1,4,1,9,9,43,1,1,6,1,7]; |
| 49 |
my $ccmHistoryEventTerminalUser = [1,3,6,1,4,1,9,9,43,1,1,6,1,8]; |
| 50 |
my $ccmHistoryEventTerminalLocation = [1,3,6,1,4,1,9,9,43,1,1,6,1,9]; |
| 51 |
my $ccmHistoryEventCommandSourceAddress = [1,3,6,1,4,1,9,9,43,1,1,6,1,10]; |
| 52 |
my $ccmHistoryEventVirtualHostName = [1,3,6,1,4,1,9,9,43,1,1,6,1,11]; |
| 53 |
my $ccmHistoryEventServerAddress = [1,3,6,1,4,1,9,9,43,1,1,6,1,12]; |
| 54 |
my $ccmHistoryEventFile = [1,3,6,1,4,1,9,9,43,1,1,6,1,13]; |
| 55 |
my $ccmHistoryEventRcpUser = [1,3,6,1,4,1,9,9,43,1,1,6,1,14]; |
| 56 |
|
| 57 |
my $session = |
| 58 |
($version eq '1' ? SNMPv1_Session->open ($host, $community, 161) |
| 59 |
: $version eq '2c' ? SNMPv2c_Session->open ($host, $community, 161) |
| 60 |
: die "Unknown SNMP version $version") |
| 61 |
|| die "Opening SNMP_Session"; |
| 62 |
|
| 63 |
my $router_boottime = router_boottime ($session); |
| 64 |
|
| 65 |
$session->map_table ([$ccmHistoryEventTime, |
| 66 |
$ccmHistoryEventConfigSource, |
| 67 |
$ccmHistoryEventConfigDestination], |
| 68 |
sub () { |
| 69 |
my ($index, $time, $source, $dest) = @_; |
| 70 |
local $BER::pretty_print_timeticks = 0; |
| 71 |
grep (defined $_ && ($_=pretty_print $_), |
| 72 |
($time, $source, $dest)); |
| 73 |
my ($action); |
| 74 |
|
| 75 |
if ($source == 3 && $dest == 2) { |
| 76 |
$action = "show running-config"; |
| 77 |
} elsif ($source == 4 && $dest == 2) { |
| 78 |
$action = "show configuration"; |
| 79 |
} elsif ($source == 3 && $dest == 4) { |
| 80 |
$action = "write memory"; |
| 81 |
} elsif ($source == 2 && $dest == 3) { |
| 82 |
$action = "configure terminal"; |
| 83 |
} elsif ($source == 6 && $dest == 3) { |
| 84 |
$action = "configure network tftp:"; |
| 85 |
} elsif ($source == 3 && $dest == 6) { |
| 86 |
$action = "write network tftp:"; |
| 87 |
} else { |
| 88 |
$source = pretty_HistoryEventMedium ($source); |
| 89 |
$dest = pretty_HistoryEventMedium ($dest); |
| 90 |
$action = "$source -> $dest"; |
| 91 |
} |
| 92 |
my $localtime = localtime ($time * 1e-2+$router_boottime); |
| 93 |
print $localtime," ",$action,"\n"; |
| 94 |
|
| 95 |
}); |
| 96 |
$session->close () |
| 97 |
|| die "close SNMP session"; |
| 98 |
1; |
| 99 |
|
| 100 |
sub pretty_HistoryEventMedium ($) { |
| 101 |
my ($medium) = @_; |
| 102 |
if ($medium == 1) { |
| 103 |
return 'erase'; |
| 104 |
} elsif ($medium == 2) { |
| 105 |
return 'commandSource'; |
| 106 |
} elsif ($medium == 3) { |
| 107 |
return 'running'; |
| 108 |
} elsif ($medium == 4) { |
| 109 |
return 'startup'; |
| 110 |
} elsif ($medium == 5) { |
| 111 |
return 'local'; |
| 112 |
} elsif ($medium == 6) { |
| 113 |
return 'networkTftp'; |
| 114 |
} elsif ($medium == 7) { |
| 115 |
return 'networkRcp'; |
| 116 |
} else { |
| 117 |
return "$medium???"; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
sub usage ($) { |
| 122 |
warn <<EOM; |
| 123 |
Usage: $0 [-v (1|2c)] router [community] |
| 124 |
$0 -h |
| 125 |
|
| 126 |
-h print this usage message and exit. |
| 127 |
|
| 128 |
-v version can be used to select the SNMP version. The default |
| 129 |
is SNMPv1, which is what most devices support. If your box |
| 130 |
supports SNMPv2c, you should enable this by passing "-v 2c" |
| 131 |
to the script. SNMPv2c is much more efficient for walking |
| 132 |
tables, which is what this tool does. |
| 133 |
|
| 134 |
router hostname or IP address of a Cisco IOS device |
| 135 |
|
| 136 |
community SNMP community string to use. Defaults to "public". |
| 137 |
EOM |
| 138 |
exit (1) if $_[0]; |
| 139 |
} |
| 140 |
|
| 141 |
### router_boottime SESSION |
| 142 |
### |
| 143 |
### Returns the boot time of the SNMP agent reachable through SESSION |
| 144 |
### in Unix seconds |
| 145 |
### |
| 146 |
sub router_boottime ($) { |
| 147 |
my ($session) = @_; |
| 148 |
|
| 149 |
my $uptime = uptime_hundredths ($session); |
| 150 |
my ($seconds,$microseconds) = Time::HiRes::gettimeofday (); |
| 151 |
return $seconds + $microseconds * 1e-6 - $uptime * 1e-2; |
| 152 |
} |
| 153 |
|
| 154 |
sub uptime_hundredths ($) { |
| 155 |
my ($session) = @_; |
| 156 |
local $BER::pretty_print_timeticks = 0; |
| 157 |
if ($session->get_request_response (encode_oid (1,3,6,1,2,1,1,3,0))) { |
| 158 |
my $response = $session->pdu_buffer; |
| 159 |
my ($bindings) = $session->decode_get_response ($response); |
| 160 |
my $binding; |
| 161 |
while ($bindings ne '') { |
| 162 |
($binding,$bindings) = decode_sequence ($bindings); |
| 163 |
my ($oid,$value) = decode_by_template ($binding, "%O%@"); |
| 164 |
my ($uptime) = pretty_print ($value); |
| 165 |
return $uptime; |
| 166 |
} |
| 167 |
} else { |
| 168 |
die "cannot get sysUpTime.0 from $host"; |
| 169 |
} |
| 170 |
} |