remove empty last command
[dell-switch] / dell-switch.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5
6 # example usage as pipe:
7 # ./ips | sed 's/^/ping /' | NO_LOG=1 ./dell-switch.pl sw-dpc
8
9 use Net::OpenSSH;
10 use Data::Dump qw(dump);
11 use Time::HiRes qw(sleep);
12
13 our $login;
14 our $passwd;
15 our $debug = $ENV{DEBUG} || 0;
16
17 use lib '.';
18 require 'config.pl';
19
20 #$Net::OpenSSH::debug = ~0;
21
22 my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n";
23 $ip = $1 if `host $ip` =~ m/has address (\S+)/;
24 my @commands = @ARGV;
25 if ( ! @commands && ! -t STDIN && -p STDIN ) { # we are being piped into
26         while(<>) {
27                 push @commands, $_;
28         }
29 } else {
30         @commands = <DATA> unless @commands;
31 }
32
33 warn "\n## ssh $ip\n";
34 my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd, 
35     ssh_cmd => '/usr/bin/ssh1', # apt-get install openssh-client-ssh1
36     master_opts => [
37         -o => "StrictHostKeyChecking=no",
38         -F => '/home/dpavlin/dell-switch/ssh1-config'
39         ],
40     default_ssh_opts => [
41         -o => "StrictHostKeyChecking=no",
42         -F => '/home/dpavlin/dell-switch/ssh1-config'
43         ],
44 );
45 my ($pty ,$pid) = $ssh->open2pty();
46 if ( ! $pty ) {
47         warn "ERROR: can't connect to $ip, skipping";
48         exit 0;
49 }
50
51 my $buff;
52
53 sub send_pty {
54         my $string = shift;
55         sleep 0.05; # we really need to wait for slow PowerConnect 5324
56         foreach (split //, $string) {
57                 print STDERR "[$_]" if $debug;
58                 syswrite $pty, $_;
59                 #$pty->flush;
60                 sleep 0.01;
61
62                 sysread $pty, my $echo, 1;
63                 print STDERR $echo;
64                 $buff .= $echo;
65         }
66 }
67
68 mkdir 'log' unless -d 'log';
69
70 chdir 'log';
71
72 sub save_log {
73         my ($ip, $hostname, $command, $buff) = @_;
74
75         return unless $command;
76         return if $ENV{NO_LOG};
77         
78         my $file = "${ip}_${hostname}_${command}.log";
79         open my $log, '>', $file;
80         $buff =~ s/\r//gs; # strip CR, leave LF only
81         print $log $buff;
82         if ( -e '.git' ) {
83                 system 'git', 'add', $file;
84                 system 'git', 'commit', '-m', "$ip $hostname", $file;
85         }
86 }
87
88 my $command;
89 my @commands_while = ( @commands );
90
91 while() {
92         my $data;
93         my $read = sysread($pty, $data, 1);
94         print STDERR $data;
95         $buff .= $data;
96         if ( $buff =~ m/User Name:/ ) {
97                 send_pty "$login\n";
98                 $buff = '';
99         } elsif ( $buff =~ m/Password:/ ) {
100                 send_pty "$passwd\n";
101                 $buff = '';
102         } elsif ( $buff =~ m/[\n\r\b]([\w\-\(\)]+)#\s*$/ ) {
103                 my $hostname = $1;
104                 if ( $buff ) {
105                         save_log $ip, $hostname, $command, $buff;
106                         $buff = '';
107                 }
108                 if ( $command = shift @commands_while ) {
109                         $command =~ s/[\n\r]+$//;
110                         send_pty "$command\n";
111                         $buff = '';
112                 } else  {
113                         send_pty "exit\n";
114                         close($pty);
115                         last;
116                 }
117         } elsif ( $buff =~ m/% Unrecognized command/ ) {
118                 exit 1;
119         } elsif ( $buff =~ s{More: <space>,  Quit: q.*One line: <return>\s*}{} ) {
120                 send_pty " ";
121         } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
122                 send_pty " ";
123         } elsif ( $buff =~ s{\e\[0m\s*\r\s+\r}{} ) {
124                 # nop
125         } elsif ( $buff =~ m/^[\r\n]+[\w\-]+>$/ ) {
126                 send_pty "enable\n";
127         } elsif ( $buff =~ m{\QOverwrite file [startup-config] ?[Yes/press any key for no]....\E} ) {
128                 send_pty "y";
129                 $buff = '';
130         } elsif ( $buff =~ s{Management access will be blocked for the duration of the transfer.*Are you sure you want to start\? \(y/n\) }{}s ) {
131                 send_pty 'y';
132         } elsif ( $buff =~ s{\QThis command will reset the whole system and disconnect your current session.\E}{}s ) { # reload
133                 warn "\nRELOAD detected\n";
134                 sleep 0.5;
135                 send_pty 'y';
136         } elsif ( $buff =~ m{MikroTik RouterOS} ) {
137                 warn "\nERROR: don't know how to talk to MicroTik - ABORTING";
138                 exit 0;
139         }
140 }
141
142 __DATA__
143 show system
144 show arp
145 show vlan
146 show running-config
147 show bridge address
148 show interfaces status