tweak flush and timeout, better prompt detection
[dell-switch] / dell-switch.pl
index 158c8e4..4d7badb 100755 (executable)
@@ -20,8 +20,12 @@ my @commands = @ARGV;
 @commands = <DATA> unless @commands;
 
 warn "## $ip\n";
-my $ssh = Net::OpenSSH->new('auto@'.$ip);
+my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd);
 my ($pty ,$pid) = $ssh->open2pty();
+if ( ! $pty ) {
+       warn "ERROR: can't connect to $ip, skipping";
+       exit 0;
+}
 
 my $buff;
 
@@ -31,7 +35,9 @@ sub send_pty {
        foreach (split //, $string) {
                print STDERR "[$_]" if $debug;
                syswrite $pty, $_;
-#              $pty->flush;
+               #$pty->flush;
+               sleep 0.05;
+
                sysread $pty, my $echo, 1;
                print STDERR $echo;
                $buff .= $echo;
@@ -70,6 +76,8 @@ while(1) {
        } elsif ( $buff =~ m/Password:/ ) {
                send_pty "$passwd\n";
                $buff = '';
+       } elsif ( $buff =~ m/\b([\w\-]+)>$/ ) {
+               send_pty "enable\n";
        } elsif ( $buff =~ m/([\w\-]+)#$/ ) {
                my $hostname = $1;
                if ( $buff ) {
@@ -88,7 +96,10 @@ while(1) {
        } elsif ( $buff =~ m/% Unrecognized command/ ) {
                exit 1;
        } elsif ( $buff =~ s{More: <space>,  Quit: q.*One line: <return> }{} ) {
+               sleep 0.5;
                send_pty " ";
+       } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
+               send_pty " "
        } elsif ( $buff =~ s{\e\[0m\r\s+\r}{} ) {
        }
 }
@@ -97,4 +108,4 @@ __DATA__
 show arp
 show vlan
 show running-config
-show bridge address-table
+show bridge address