r4324 added RHEL6
[racktables] / gateways / deviceconfig / nxos4.connector
1 #!/usr/bin/perl
2
3 # a working NX-OS connector for RackTables by Dmitry Tejblum
4
5 use strict;
6 use File::FnMatch qw(:fnmatch);
7 use Net::Telnet::Cisco;
8 $#ARGV == 2 or die "usage";
9 my $endpoint = shift @ARGV;
10 my $command = shift @ARGV;
11 my $workfile = shift @ARGV;
12
13 $0 =~ m,^(.*)/[^/]+$,;
14 my $mydir = $1;
15 open(SECRETS, "<$mydir/switch.secrets.php") || die "switch.secrets.php: $!";
16 my $skip = 1;
17 my ($username, $password, $enable_password);
18 while (<SECRETS>) {
19 chomp;
20 if ($skip && ($_ eq "# S-T-A-R-T")) {
21 $skip = 0;
22 next;
23 }
24 if (!$skip && ($_ eq "# S-T-O-P")) {
25 $skip = 1;
26 next;
27 }
28 next if ($skip);
29 next if m,^\s*(#.*)?$,;
30 my @f = split;
31 if (fnmatch($f[0], $endpoint)) {
32 $username = $f[4];
33 $password = $f[5];
34 $enable_password = $f[6];
35 last;
36 }
37 }
38
39 if (!defined($username)) {
40 exit(3);
41 }
42
43 my $session = Net::Telnet::Cisco->new('Host' => $endpoint,
44 Prompt => ' /(?:^|[\x0a\x0d0\0]+)[\w.-]+\s?(?:\(config[^\)]*\))?\s?[\$#>]\s?(?:\(enable\))?\s*$/',
45 );
46 $session->more_prompt('/(?:^|[\x0a\x0d\0]+)\s*(?:\x1b\S{1,3})?\s*--More--/');
47 $session->login($username, $password);
48 if ($enable_password ne "-") {
49 $session->enable($enable_password);
50 }
51 if ($command eq "get8021q") {
52 open(WORKFILE, ">$workfile") || die "$workfile: $!";
53 $session->cmd ('terminal length 0');
54 $session->cmd ('terminal no monitor');
55 my @r = $session->cmd ('show running-config vlan 1-4094');
56 print WORKFILE @r, "\n\n";
57 @r = $session->cmd('show running-config interface');
58 print WORKFILE @r, "\n\n";;
59 exit(0);
60 }
61 if ($command eq "getcdpstatus") {
62 open(WORKFILE, ">$workfile") || die "$workfile: $!";
63 $session->cmd ('terminal length 0');
64 $session->cmd ('terminal no monitor');
65 my @r = $session->cmd ('show cdp neighbors detail');
66 print WORKFILE @r, "\n\n";;
67 exit(0);
68 }
69 if ($command eq "getportstatus") {
70 open(WORKFILE, ">$workfile") || die "$workfile: $!";
71 $session->cmd ('terminal length 0');
72 $session->cmd ('terminal no monitor');
73 my @r = $session->cmd ('show int status');
74 print WORKFILE @r, "\n\n";;
75 exit(0);
76 }
77 if ($command eq "getmaclist") {
78 open(WORKFILE, ">$workfile") || die "$workfile: $!";
79 $session->cmd ('terminal length 0');
80 $session->cmd ('terminal no monitor');
81 my @r = $session->cmd ('show mac address-table dynamic');
82 print WORKFILE @r, "\n\n";;
83 exit(0);
84 }
85 if ($command eq "deploy") {
86 open(WORKFILE, "<$workfile") || die "$workfile: $!";
87 while (<WORKFILE>) {
88 $session->cmd($_);
89 }
90 exit(0);
91 }