added two more commands for ip only invocation
[dell-switch] / dell-switch.pl
index 5b9109c..22d0ddd 100755 (executable)
@@ -15,13 +15,17 @@ require 'config.pl';
 
 #$Net::OpenSSH::debug = ~0;
 
-my $ip = shift @ARGV || '10.20.0.2';
+my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n";
 my @commands = @ARGV;
 @commands = <DATA> unless @commands;
 
-warn "## $ip\n";
-my $ssh = Net::OpenSSH->new('auto@'.$ip);
+warn "\n## ssh $ip\n";
+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;
@@ -44,6 +50,8 @@ chdir 'log';
 
 sub save_log {
        my ($ip, $hostname, $command, $buff) = @_;
+
+       return unless $command;
        
        my $file = "${ip}_${hostname}_${command}.log";
        open my $log, '>', $file;
@@ -85,14 +93,22 @@ while(1) {
                }
        } elsif ( $buff =~ m/% Unrecognized command/ ) {
                exit 1;
-       } elsif ( $buff =~ s{More: <space>,  Quit: q, One line: <return> }{} ) {
+       } elsif ( $buff =~ s{More: <space>,  Quit: q.*One line: <return>\s*}{} ) {
                send_pty " ";
-       } elsif ( $buff =~ s{\e\[0m\r\s+\r}{} ) {
+       } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
+               send_pty " ";
+       } elsif ( $buff =~ s{\e\[0m\s*\r\s+\r}{} ) {
+               # nop
+       } elsif ( $buff =~ m/^[\r\n]+[\w\-]+>$/ ) {
+               send_pty "enable\n";
        }
 }
 
 __DATA__
+show system
 show arp
 show vlan
 show running-config
-show bridge address-table
+show bridge address
+show interfaces status
+