read commands from STDIN if used as pipe
[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, default_ssh_opts => [-o => "StrictHostKeyChecking=no"]);
35 my ($pty ,$pid) = $ssh->open2pty();
36 if ( ! $pty ) {
37         warn "ERROR: can't connect to $ip, skipping";
38         exit 0;
39 }
40
41 my $buff;
42
43 sub send_pty {
44         my $string = shift;
45         sleep 0.05; # we really need to wait for slow PowerConnect 5324
46         foreach (split //, $string) {
47                 print STDERR "[$_]" if $debug;
48                 syswrite $pty, $_;
49                 #$pty->flush;
50                 sleep 0.01;
51
52                 sysread $pty, my $echo, 1;
53                 print STDERR $echo;
54                 $buff .= $echo;
55         }
56 }
57
58 mkdir 'log' unless -d 'log';
59
60 chdir 'log';
61
62 sub save_log {
63         my ($ip, $hostname, $command, $buff) = @_;
64
65         return unless $command;
66         return if $ENV{NO_LOG};
67         
68         my $file = "${ip}_${hostname}_${command}.log";
69         open my $log, '>', $file;
70         $buff =~ s/\r//gs; # strip CR, leave LF only
71         print $log $buff;
72         if ( -e '.git' ) {
73                 system 'git', 'add', $file;
74                 system 'git', 'commit', '-m', "$ip $hostname", $file;
75         }
76 }
77
78 my $command;
79 my @commands_while = ( @commands );
80
81 while() {
82         my $data;
83         my $read = sysread($pty, $data, 1);
84         print STDERR $data;
85         $buff .= $data;
86         if ( $buff =~ m/User Name:/ ) {
87                 send_pty "$login\n";
88                 $buff = '';
89         } elsif ( $buff =~ m/Password:/ ) {
90                 send_pty "$passwd\n";
91                 $buff = '';
92         } elsif ( $buff =~ m/([\w\-\(\)]+)#$/ ) {
93                 my $hostname = $1;
94                 if ( $buff ) {
95                         save_log $ip, $hostname, $command, $buff;
96                         $buff = '';
97                 }
98                 if ( $command = shift @commands_while ) {
99                         $command =~ s/[\n\r]+$//;
100                         send_pty "$command\n";
101                         $buff = '';
102                 } else  {
103                         send_pty "exit\n";
104                         close($pty);
105                         last;
106                 }
107         } elsif ( $buff =~ m/% Unrecognized command/ ) {
108                 exit 1;
109         } elsif ( $buff =~ s{More: <space>,  Quit: q.*One line: <return>\s*}{} ) {
110                 send_pty " ";
111         } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
112                 send_pty " ";
113         } elsif ( $buff =~ s{\e\[0m\s*\r\s+\r}{} ) {
114                 # nop
115         } elsif ( $buff =~ m/^[\r\n]+[\w\-]+>$/ ) {
116                 send_pty "enable\n";
117         } elsif ( $buff =~ m{\QOverwrite file [startup-config] ?[Yes/press any key for no]....\E} ) {
118                 send_pty "y";
119                 $buff = '';
120         } elsif ( $buff =~ s{Management access will be blocked for the duration of the transfer.*Are you sure you want to start\? \(y/n\) }{}s ) {
121                 send_pty 'y';
122         } elsif ( $buff =~ s{\QThis command will reset the whole system and disconnect your current session.\E}{}s ) { # reload
123                 warn "\nRELOAD detected\n";
124                 sleep 0.5;
125                 send_pty 'y';
126         } elsif ( $buff =~ m{MikroTik RouterOS} ) {
127                 warn "\nERROR: don't know how to talk to MicroTik - ABORTING";
128                 exit 0;
129         }
130 }
131
132 __DATA__
133 show system
134 show arp
135 show vlan
136 show running-config
137 show bridge address
138 show interfaces status
139