| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Getopt::Long qw(:config no_ignore_case); #bundling | ||
| 6 | use Pod::Usage; | ||
| 7 | |||
| 8 | use Cisco::SNMP::Config; | ||
| 9 | |||
| 10 | my %opt; | ||
| 11 | my ($opt_help, $opt_man); | ||
| 12 | |||
| 13 | GetOptions( | ||
| 14 | 'community=s' => \$opt{community}, | ||
| 15 | 'dest=s' => \$opt{dest}, | ||
| 16 | 'P|protocol=s' => \$opt{protocol}, | ||
| 17 | 'password=s' => \$opt{password}, | ||
| 18 | 'source=s' => \$opt{source}, | ||
| 19 | 'tftp=s' => \$opt{server}, # legacy | ||
| 20 | 'S|server=s' => \$opt{server}, | ||
| 21 | 'username=s' => \$opt{username}, | ||
| 22 | 'help!' => \$opt_help, | ||
| 23 | 'man!' => \$opt_man | ||
| 24 | ) or pod2usage(-verbose => 0); | ||
| 25 | |||
| 26 | pod2usage(-verbose => 1) if defined $opt_help; | ||
| 27 | pod2usage(-verbose => 2) if defined $opt_man; | ||
| 28 | |||
| 29 | # Make sure at least one host was provided | ||
| 30 | if (!@ARGV) { | ||
| 31 | pod2usage(-verbose => 0, -message => "$0: host required\n") | ||
| 32 | } | ||
| 33 | |||
| 34 | $opt{community} = $opt{community} || 'private'; | ||
| 35 | $opt{protocol} = $opt{protocol} || 'tftp'; | ||
| 36 | $opt{username} = $opt{useranme} || 'cisco'; | ||
| 37 | $opt{password} = $opt{password} || 'cisco'; | ||
| 38 | $opt{dest} = $opt{dest} || 'start'; | ||
| 39 | $opt{source} = $opt{source} || 'run'; | ||
| 40 | |||
| 41 | if (($opt{dest} !~ /^run(?:ning)?(?:-config)?$/i) && | ||
| 42 | ($opt{dest} !~ /^start(?:up)?(?:-config)?$/i) && | ||
| 43 | ($opt{source} !~ /^run(?:ning)?(?:-config)?$/i) && | ||
| 44 | ($opt{source} !~ /^start(?:up)?(?:-config)?$/i)) { | ||
| 45 | print "$0: source or dest must be run or start\n"; | ||
| 46 | exit 1 | ||
| 47 | } | ||
| 48 | |||
| 49 | if (((($opt{dest} !~ /^run(?:ning)?(?:-config)?$/i) && | ||
| 50 | ($opt{dest} !~ /^start(?:up)?(?:-config)?$/i)) || | ||
| 51 | (($opt{source} !~ /^run(?:ning)?(?:-config)?$/i) && | ||
| 52 | ($opt{source} !~ /^start(?:up)?(?:-config)?$/i))) && (!defined $opt{server})) { | ||
| 53 | print "$0: server required for source or dest not run or start\n"; | ||
| 54 | exit 1 | ||
| 55 | } | ||
| 56 | |||
| 57 | if ((defined $opt{server}) && | ||
| 58 | (($opt{dest} =~ /^run(?:ning)?(?:-config)?$/i) && | ||
| 59 | ($opt{source} =~ /^start(?:up)?(?:-config)?$/i)) || | ||
| 60 | (($opt{source} =~ /^run(?:ning)?(?:-config)?$/i) && | ||
| 61 | ($opt{dest} =~ /^start(?:up)?(?:-config)?$/i))) { | ||
| 62 | print "$0: server requires source or dest\n"; | ||
| 63 | exit 1 | ||
| 64 | } | ||
| 65 | |||
| 66 | # No output buffering | ||
| 67 | #$|=1; | ||
| 68 | |||
| 69 | for (@ARGV) { | ||
| 70 | print "\n-- $_ --\n"; | ||
| 71 | |||
| 72 | my $cm; | ||
| 73 | if (!defined($cm = Cisco::SNMP::Config->new( | ||
| 74 | hostname => $_, | ||
| 75 | community => $opt{community} | ||
| 76 | ))) { | ||
| 77 | printf "Error: %s\n", Cisco::SNMP::Config->error; | ||
| 78 | next | ||
| 79 | } | ||
| 80 | |||
| 81 | # copy local (run / start) | ||
| 82 | if (!defined $opt{server}) { | ||
| 83 | print "$_: copy $opt{source} $opt{dest} - "; | ||
| 84 | if (defined(my $conf = $cm->config_copy( | ||
| 85 | source => $opt{source}, | ||
| 86 | dest => $opt{dest} | ||
| 87 | ))) { | ||
| 88 | printf "START: %s / END: %s\n", $conf->ccStartTime, $conf->ccEndTime | ||
| 89 | } else { | ||
| 90 | printf "Error: %s\n", Cisco::SNMP::Config->error | ||
| 91 | } | ||
| 92 | next | ||
| 93 | } | ||
| 94 | |||
| 95 | # get | ||
| 96 | my $src = $opt{source}; | ||
| 97 | my $dest = $opt{dest}; | ||
| 98 | if (($opt{source} =~ /^run(?:ning)?(?:-config)?$/i) && | ||
| 99 | ($opt{dest} =~ /^start(?:up)?(?:-config)?$/i)) { | ||
| 100 | $dest = $opt{server} . ":/" . $opt{dest} | ||
| 101 | # put | ||
| 102 | } else { | ||
| 103 | $src = $opt{server} . ":/" . $opt{source} | ||
| 104 | } | ||
| 105 | printf "$_: copy $src $dest - "; | ||
| 106 | if (defined(my $conf = $cm->config_copy( | ||
| 107 | server => $opt{server}, | ||
| 108 | protocol => $opt{protocol}, | ||
| 109 | username => $opt{username}, | ||
| 110 | password => $opt{password}, | ||
| 111 | source => $opt{source}, | ||
| 112 | dest => $opt{dest} | ||
| 113 | ))) { | ||
| 114 | printf "START: %s / END: %s\n", $conf->ccStartTime, $conf->ccEndTime; | ||
| 115 | } else { | ||
| 116 | printf "Error: %s\n", Cisco::SNMP::Config->error | ||
| 117 | } | ||
| 118 | |||
| 119 | $cm->close() | ||
| 120 | } | ||
| 121 | |||
| 122 | __END__ |