read commands from STDIN if used as pipe
[dell-switch] / dell-switch.pl
index e3c3b0d..c48184f 100755 (executable)
@@ -3,6 +3,9 @@ use warnings;
 use strict;
 use autodie;
 
+# example usage as pipe:
+# ./ips | sed 's/^/ping /' | NO_LOG=1 ./dell-switch.pl sw-dpc
+
 use Net::OpenSSH;
 use Data::Dump qw(dump);
 use Time::HiRes qw(sleep);
@@ -19,7 +22,13 @@ require 'config.pl';
 my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n";
 $ip = $1 if `host $ip` =~ m/has address (\S+)/;
 my @commands = @ARGV;
-@commands = <DATA> unless @commands;
+if ( ! @commands && ! -t STDIN && -p STDIN ) { # we are being piped into
+       while(<>) {
+               push @commands, $_;
+       }
+} else {
+       @commands = <DATA> unless @commands;
+}
 
 warn "\n## ssh $ip\n";
 my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd, default_ssh_opts => [-o => "StrictHostKeyChecking=no"]);
@@ -110,6 +119,13 @@ while() {
                $buff = '';
        } elsif ( $buff =~ s{Management access will be blocked for the duration of the transfer.*Are you sure you want to start\? \(y/n\) }{}s ) {
                send_pty 'y';
+       } elsif ( $buff =~ s{\QThis command will reset the whole system and disconnect your current session.\E}{}s ) { # reload
+               warn "\nRELOAD detected\n";
+               sleep 0.5;
+               send_pty 'y';
+       } elsif ( $buff =~ m{MikroTik RouterOS} ) {
+               warn "\nERROR: don't know how to talk to MicroTik - ABORTING";
+               exit 0;
        }
 }