r4324 added RHEL6
[racktables] / gateways / deviceconfig / jun10.connector
CommitLineData
9910a8b3
AA
1#!/usr/bin/perl
2
3use strict;
4use File::FnMatch qw(:fnmatch);
5use Net::Telnet::Cisco;
6@ARGV == 3 or @ARGV == 4 or die "usage: $0 endpoint command workfile";
7
8my ($endpoint, $command, $workfile, $sudo_call) = @ARGV;
9
10$0 =~ m,^(.*)/[^/]+$,;
11my $mydir = $1;
12open(SECRETS, "<$mydir/switch.secrets.php") || die "switch.secrets.php: $!";
13my $skip = 1;
14my ($protocol, $username, $password, $enable_password, $sudo_username);
15while (<SECRETS>) {
16 chomp;
17 if ($skip && ($_ eq "# S-T-A-R-T")) {
18 $skip = 0;
19 next;
20 }
21 if (!$skip && ($_ eq "# S-T-O-P")) {
22 $skip = 1;
23 next;
24 }
25 next if ($skip);
26 next if m,^\s*(#.*)?$,;
27 my @f = split;
28 if (fnmatch($f[0], $endpoint)) {
29 $protocol = $f[1];
30 $username = $f[4];
31 $password = $f[5];
32 $enable_password = $f[6];
33 $sudo_username = $f[7];
34 last;
35 }
36}
37
38if (!defined($username)) {
39 exit(3);
40}
41
42my $is_reader = ($command =~ /deploy/);
43my $is_writer = ($command =~ /get8021q/);
44
45my $current_user = scalar getpwuid $<;
46if ($sudo_username and $sudo_username ne '-' and $current_user ne $sudo_username) {
47 if ($sudo_call) {
48 die "cant sudo as $sudo_username";
49 }
50 else {
51 my $child_command = "sudo -u '$sudo_username' '$0' @ARGV sudo_call";
52 my $code;
53 if ($is_reader) {
54 $code = system("$child_command <'$workfile'");
55 }
56 elsif ($is_writer) {
57 $code = system("$child_command >'$workfile'");
58 }
59 exit ($code >> 8);
60 }
61}
62
63if ($is_reader) {
64 if ($sudo_call) {
65 open (WORKFILE, "<&STDIN");
66 }
67 else {
68 open(WORKFILE, "<$workfile") || die "$workfile: $!";
69 }
70}
71elsif ($is_writer) {
72 if ($sudo_call) {
73 open(WORKFILE, ">&STDOUT");
74 }
75 else {
76 open(WORKFILE, ">$workfile") || die "$workfile: $!";
77 }
78}
79
80if ($protocol eq 'telnet') {
81 my $session = Net::Telnet->new('Host' => $endpoint,
82 Prompt => '/\S+\@\S+[>#] $/',
83 Timeout => 40,
84 );
85 $session->login($username, $password);
86 if ($command eq "get8021q") {
87 $session->cmd ('set cli screen-length 0');
88 print WORKFILE $session->cmd ('show vlans detail'), "# END OF VLAN LIST\n";
89 print WORKFILE $session->cmd ('show configuration groups'), "# END OF GROUP LIST\n";
90 print WORKFILE $session->cmd ('show configuration interfaces'), "# END OF CONFIG\n";
91 exit(0);
92 }
93 elsif ($command eq "deploy") {
94 while (my $cmd = <WORKFILE>) {
95 $cmd =~ s/[\r\n]//g;
96 my $result = join '', $session->cmd ($cmd);
97 if ($result =~ /\berror:\b/si) {
98 die "error saving configuration";
99 }
100 }
101 exit(0);
102 }
103}
104elsif ($protocol eq 'ssh') {
105 # open filehandles
106 if ($is_writer and ! $sudo_call) {
107 close STDOUT;
108 open (STDOUT, ">&WORKFILE");
109 }
110 open(SSH, "| ssh -T -o StrictHostKeyChecking=no '$username'\@'$endpoint'" . ($is_reader ? ' >/dev/null' : '')) or die "cant run ssh: $!";
111
112 if ($command eq "get8021q") {
113 print SSH "set cli screen-length 0\n";
114 print SSH "show vlans detail\n", "# END OF VLAN LIST\n";
115 print SSH "show configuration groups\n", "# END OF GROUP LIST\n";
116 print SSH "show configuration interfaces\n", "# END OF CONFIG\n";
117 exit(0);
118 }
119 elsif ($command eq "deploy") {
120 while (my $cmd = <WORKFILE>) {
121 $cmd =~ s/[\r\n]//g;
122 print SSH $cmd, "\n";
123 }
124 exit(0);
125 }
126}