7 use Data::Dump qw(dump);
8 use Time::HiRes qw(sleep);
12 our $debug = $ENV{DEBUG} || 0;
17 #$Net::OpenSSH::debug = ~0;
19 my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n";
21 @commands = <DATA> unless @commands;
23 warn "\n## ssh $ip\n";
24 my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd);
25 my ($pty ,$pid) = $ssh->open2pty();
27 warn "ERROR: can't connect to $ip, skipping";
35 sleep 0.1; # we really need to wait for slow PowerConnect 5324
36 foreach (split //, $string) {
37 print STDERR "[$_]" if $debug;
42 sysread $pty, my $echo, 1;
48 mkdir 'log' unless -d 'log';
53 my ($ip, $hostname, $command, $buff) = @_;
55 return unless $command;
56 return if $ENV{NO_LOG};
58 my $file = "${ip}_${hostname}_${command}.log";
59 open my $log, '>', $file;
60 $buff =~ s/\r//gs; # strip CR, leave LF only
63 system 'git', 'add', $file;
64 system 'git', 'commit', '-m', "$ip $hostname", $file;
69 my @commands_while = ( @commands );
73 my $read = sysread($pty, $data, 1);
76 if ( $buff =~ m/User Name:/ ) {
79 } elsif ( $buff =~ m/Password:/ ) {
82 } elsif ( $buff =~ m/([\w\-\(\)]+)#$/ ) {
85 save_log $ip, $hostname, $command, $buff;
88 if ( $command = shift @commands_while ) {
89 $command =~ s/[\n\r]+$//;
90 send_pty "$command\n";
97 } elsif ( $buff =~ m/% Unrecognized command/ ) {
99 } elsif ( $buff =~ s{More: <space>, Quit: q.*One line: <return>\s*}{} ) {
101 } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
103 } elsif ( $buff =~ s{\e\[0m\s*\r\s+\r}{} ) {
105 } elsif ( $buff =~ m/^[\r\n]+[\w\-]+>$/ ) {
107 } elsif ( $buff =~ m{\QOverwrite file [startup-config] ?[Yes/press any key for no]....\E} ) {
110 } elsif ( $buff =~ s{Management access will be blocked for the duration of the transfer.*Are you sure you want to start\? \(y/n\) }{}s ) {
121 show interfaces status