| 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::Management; | ||
| 9 | |||
| 10 | my $PASSWORD_LINES = "( password 7 )|(-server key 7 )|( key-string 7 )"; | ||
| 11 | my %opt; | ||
| 12 | my ($opt_help, $opt_man); | ||
| 13 | |||
| 14 | GetOptions( | ||
| 15 | 'encrypt:s' => \$opt{encrypt}, | ||
| 16 | 'help!' => \$opt_help, | ||
| 17 | 'man!' => \$opt_man | ||
| 18 | ) or pod2usage(-verbose => 0); | ||
| 19 | |||
| 20 | pod2usage(-verbose => 1) if defined $opt_help; | ||
| 21 | pod2usage(-verbose => 2) if defined $opt_man; | ||
| 22 | |||
| 23 | # Make sure at least one host was provided | ||
| 24 | if (!@ARGV) { | ||
| 25 | pod2usage(-verbose => 0, -message => "$0: argument required\n") | ||
| 26 | } | ||
| 27 | |||
| 28 | if (defined($opt{encrypt})) { | ||
| 29 | if (my $passwd = Cisco::Management->password_encrypt($ARGV[0], $opt{encrypt})) { | ||
| 30 | print "$_\n" for (@{$passwd}) | ||
| 31 | } else { | ||
| 32 | printf "Error - %s\n", Cisco::Management->error | ||
| 33 | } | ||
| 34 | } else { | ||
| 35 | my @passwords; | ||
| 36 | my $linecnt = 0; | ||
| 37 | if (-e $ARGV[0]) { | ||
| 38 | open(IN, $ARGV[0]); | ||
| 39 | while (<IN>) { | ||
| 40 | chomp $_; | ||
| 41 | if ($_ =~ $PASSWORD_LINES) { push @passwords, "($linecnt) " . $_; } | ||
| 42 | $linecnt++ | ||
| 43 | } | ||
| 44 | close (IN) | ||
| 45 | } else { | ||
| 46 | push @passwords, $ARGV[0] | ||
| 47 | } | ||
| 48 | |||
| 49 | for (@passwords) { | ||
| 50 | my @parts = split(/ /,$_); | ||
| 51 | if (my $passwd = Cisco::Management->password_decrypt($parts[$#parts])) { | ||
| 52 | if ($linecnt > 0) { | ||
| 53 | print "@parts = $passwd\n" | ||
| 54 | } else { | ||
| 55 | print "$passwd\n" | ||
| 56 | } | ||
| 57 | } else { | ||
| 58 | printf "Error - %s\n", Cisco::Management->error | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | __END__ |