filter log/ filenames into sw.command
[dell-switch] / dell-switch.pl
index e3c3b0d..485d29c 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,10 +22,26 @@ 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"]);
+my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd, 
+    ssh_cmd => '/usr/bin/ssh1', # apt-get install openssh-client-ssh1
+    master_opts => [
+       -o => "StrictHostKeyChecking=no",
+       -F => '/home/dpavlin/dell-switch/ssh1-config'
+       ],
+    default_ssh_opts => [
+       -o => "StrictHostKeyChecking=no",
+       -F => '/home/dpavlin/dell-switch/ssh1-config'
+       ],
+);
 my ($pty ,$pid) = $ssh->open2pty();
 if ( ! $pty ) {
        warn "ERROR: can't connect to $ip, skipping";
@@ -80,7 +99,8 @@ while() {
        } elsif ( $buff =~ m/Password:/ ) {
                send_pty "$passwd\n";
                $buff = '';
-       } elsif ( $buff =~ m/([\w\-\(\)]+)#$/ ) {
+       } elsif ( $buff =~ m/[\n\r\b]([\w\-\(\)\/]+)#\s*$/ ) {
+               # config interface needs / in prompt
                my $hostname = $1;
                if ( $buff ) {
                        save_log $ip, $hostname, $command, $buff;
@@ -97,10 +117,24 @@ while() {
                }
        } elsif ( $buff =~ m/% Unrecognized command/ ) {
                exit 1;
+       } elsif ( $buff =~ m/% Invalid input detected at .* marker/ ) {
+
+               # try to rewrite command differences
+
+               if ( $command =~ m/show lldp neighbors/ ) {
+                       unshift @commands_while, 'show lldp remote-device all';
+                       undef $command; # don't save this command
+                       $buff = '';
+               }
+
+               warn "# commands_while = ",dump( \@commands_while );
+
        } elsif ( $buff =~ s{More: <space>,  Quit: q.*One line: <return>\s*}{} ) {
                send_pty " ";
        } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
                send_pty " ";
+       } elsif ( $buff =~ s{\r\s{18}\r}{} ) {
+               # strip spaces delete after more prompt
        } elsif ( $buff =~ s{\e\[0m\s*\r\s+\r}{} ) {
                # nop
        } elsif ( $buff =~ m/^[\r\n]+[\w\-]+>$/ ) {
@@ -110,6 +144,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;
        }
 }
 
@@ -120,4 +161,3 @@ show vlan
 show running-config
 show bridge address
 show interfaces status
-