6 # example usage as pipe:
7 # ./ips | sed 's/^/ping /' | NO_LOG=1 ./dell-switch.pl sw-dpc
10 use Data::Dump qw(dump);
11 use Time::HiRes qw(sleep);
15 our $debug = $ENV{DEBUG} || 0;
20 #$Net::OpenSSH::debug = ~0;
22 my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n";
23 $ip = $1 if `host $ip` =~ m/has address (\S+)/;
25 if ( ! @commands && ! -t STDIN && -p STDIN ) { # we are being piped into
30 @commands = <DATA> unless @commands;
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", ],
39 my ($pty ,$pid) = $ssh->open2pty();
41 warn "ERROR: can't connect to $ip, skipping";
49 sleep 0.05; # we really need to wait for slow PowerConnect 5324
50 foreach (split //, $string) {
51 print STDERR "[$_]" if $debug;
56 sysread $pty, my $echo, 1;
62 mkdir 'log' unless -d 'log';
67 my ($ip, $hostname, $command, $buff) = @_;
69 return unless $command;
70 return if $ENV{NO_LOG};
72 my $file = "${ip}_${hostname}_${command}.log";
73 open my $log, '>', $file;
74 $buff =~ s/\r//gs; # strip CR, leave LF only
77 system 'git', 'add', $file;
78 system 'git', 'commit', '-m', "$ip $hostname", $file;
83 my @commands_while = ( @commands );
87 my $read = sysread($pty, $data, 1);
90 if ( $buff =~ m/User Name:/ ) {
93 } elsif ( $buff =~ m/Password:/ ) {
96 } elsif ( $buff =~ m/([\w\-\(\)]+)#$/ ) {
99 save_log $ip, $hostname, $command, $buff;
102 if ( $command = shift @commands_while ) {
103 $command =~ s/[\n\r]+$//;
104 send_pty "$command\n";
111 } elsif ( $buff =~ m/% Unrecognized command/ ) {
113 } elsif ( $buff =~ s{More: <space>, Quit: q.*One line: <return>\s*}{} ) {
115 } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
117 } elsif ( $buff =~ s{\e\[0m\s*\r\s+\r}{} ) {
119 } elsif ( $buff =~ m/^[\r\n]+[\w\-]+>$/ ) {
121 } elsif ( $buff =~ m{\QOverwrite file [startup-config] ?[Yes/press any key for no]....\E} ) {
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 ) {
126 } elsif ( $buff =~ s{\QThis command will reset the whole system and disconnect your current session.\E}{}s ) { # reload
127 warn "\nRELOAD detected\n";
130 } elsif ( $buff =~ m{MikroTik RouterOS} ) {
131 warn "\nERROR: don't know how to talk to MicroTik - ABORTING";
142 show interfaces status