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