X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=dell-switch.pl;h=c48184f8e26377e19c1d4d1760308327ac422ca6;hb=d72fcd70c44a4bd2e3dbe35027a52f649ffae491;hp=954656111bce4ecc2a977b9d09c673a6e4db23a3;hpb=5dff5aec91f6bd0e84637211d0a75d912633e9f7;p=dell-switch diff --git a/dell-switch.pl b/dell-switch.pl index 9546561..c48184f 100755 --- a/dell-switch.pl +++ b/dell-switch.pl @@ -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); @@ -11,16 +14,24 @@ our $login; our $passwd; our $debug = $ENV{DEBUG} || 0; +use lib '.'; require 'config.pl'; #$Net::OpenSSH::debug = ~0; my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n"; +$ip = $1 if `host $ip` =~ m/has address (\S+)/; my @commands = @ARGV; -@commands = unless @commands; +if ( ! @commands && ! -t STDIN && -p STDIN ) { # we are being piped into + while(<>) { + push @commands, $_; + } +} else { + @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, default_ssh_opts => [-o => "StrictHostKeyChecking=no"]); my ($pty ,$pid) = $ssh->open2pty(); if ( ! $pty ) { warn "ERROR: can't connect to $ip, skipping"; @@ -31,11 +42,13 @@ my $buff; sub send_pty { my $string = shift; - sleep 0.1; # we really need to wait for slow PowerConnect 5324 + sleep 0.05; # we really need to wait for slow PowerConnect 5324 foreach (split //, $string) { print STDERR "[$_]" if $debug; syswrite $pty, $_; -# $pty->flush; + #$pty->flush; + sleep 0.01; + sysread $pty, my $echo, 1; print STDERR $echo; $buff .= $echo; @@ -50,6 +63,7 @@ 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; @@ -62,8 +76,9 @@ sub save_log { } my $command; +my @commands_while = ( @commands ); -while(1) { +while() { my $data; my $read = sysread($pty, $data, 1); print STDERR $data; @@ -74,13 +89,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 = ''; @@ -91,14 +106,34 @@ 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'; + } 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; } } __DATA__ +show system show arp show vlan show running-config -show bridge address-table +show bridge address +show interfaces status +