X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=dell-switch.pl;h=efff4ba71000d9322e275cf03609461aec259fda;hb=f9fe79479e3a8845f472e0804c6b467c3431df59;hp=5b9109cdd26381f677a3ec5b9328429578b9f795;hpb=7d2404688f803123733f43689ebbcb9ab393b055;p=dell-switch diff --git a/dell-switch.pl b/dell-switch.pl index 5b9109c..efff4ba 100755 --- a/dell-switch.pl +++ b/dell-switch.pl @@ -11,17 +11,22 @@ our $login; our $passwd; our $debug = $ENV{DEBUG} || 0; +use lib '.'; 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 = 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 +36,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 +51,9 @@ chdir 'log'; sub save_log { my ($ip, $hostname, $command, $buff) = @_; + + return unless $command; + return if $ENV{NO_LOG}; my $file = "${ip}_${hostname}_${command}.log"; open my $log, '>', $file; @@ -56,8 +66,9 @@ sub save_log { } my $command; +my @commands_while = ( @commands ); -while(1) { +while() { my $data; my $read = sysread($pty, $data, 1); print STDERR $data; @@ -68,13 +79,13 @@ while(1) { } elsif ( $buff =~ m/Password:/ ) { send_pty "$passwd\n"; $buff = ''; - } elsif ( $buff =~ m/([\w\-]+)#$/ ) { + } elsif ( $buff =~ m/([\w\-\(\)]+)#$/ ) { my $hostname = $1; if ( $buff ) { save_log $ip, $hostname, $command, $buff; $buff = ''; } - if ( $command = shift @commands ) { + if ( $command = shift @commands_while ) { $command =~ s/[\n\r]+$//; send_pty "$command\n"; $buff = ''; @@ -85,14 +96,27 @@ while(1) { } } elsif ( $buff =~ m/% Unrecognized command/ ) { exit 1; - } elsif ( $buff =~ s{More: , Quit: q, One line: }{} ) { + } elsif ( $buff =~ s{More: , Quit: q.*One line: \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"; + } elsif ( $buff =~ m{\QOverwrite file [startup-config] ?[Yes/press any key for no]....\E} ) { + send_pty "y"; + $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'; } } __DATA__ +show system show arp show vlan show running-config -show bridge address-table +show bridge address +show interfaces status +