e07d0f7d3f547b70dfde422d1533b855c34f0731
[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 => ' /(?m:^\015?\000?[\w.-]+(?:\(config[^\)]*\))?\s?[\$#>]\s?)/'
45 );
46 $session->login($username, $password);
47 if ($enable_password ne "-") {
48 $session->enable($enable_password);
49 }
50 if ($command eq "get8021q") {
51 open(WORKFILE, ">$workfile") || die "$workfile: $!";
52 $session->cmd ('terminal length 0');
53 $session->cmd ('terminal no monitor');
54 my @r = $session->cmd ('show running-config vlan 1-4094');
55 print WORKFILE @r;
56 @r = $session->cmd('show running-config interface');
57 print WORKFILE @r;
58 exit(0);
59 }
60 if ($command eq "getcdpstatus") {
61 open(WORKFILE, ">$workfile") || die "$workfile: $!";
62 $session->cmd ('terminal length 0');
63 $session->cmd ('terminal no monitor');
64 my @r = $session->cmd ('show cdp neighbors detail');
65 print WORKFILE @r;
66 exit(0);
67 }
68 if ($command eq "deploy") {
69 open(WORKFILE, "<$workfile") || die "$workfile: $!";
70 while (<WORKFILE>) {
71 $session->cmd($_);
72 }
73 exit(0);
74 }