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";
20 $ip = $1 if `host $ip` =~ m/has address (\S+)/;
22 @commands = <DATA> unless @commands;
24 warn "\n## ssh $ip\n";
25 my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd, default_ssh_opts => [-o => "StrictHostKeyChecking=no"]);
26 my ($pty ,$pid) = $ssh->open2pty();
28 warn "ERROR: can't connect to $ip, skipping";
36 sleep 0.05; # we really need to wait for slow PowerConnect 5324
37 foreach (split //, $string) {
38 print STDERR "[$_]" if $debug;
43 sysread $pty, my $echo, 1;
49 mkdir 'log' unless -d 'log';
54 my ($ip, $hostname, $command, $buff) = @_;
56 return unless $command;
57 return if $ENV{NO_LOG};
59 my $file = "${ip}_${hostname}_${command}.log";
60 open my $log, '>', $file;
61 $buff =~ s/\r//gs; # strip CR, leave LF only
64 system 'git', 'add', $file;
65 system 'git', 'commit', '-m', "$ip $hostname", $file;
70 my @commands_while = ( @commands );
74 my $read = sysread($pty, $data, 1);
77 if ( $buff =~ m/User Name:/ ) {
80 } elsif ( $buff =~ m/Password:/ ) {
83 } elsif ( $buff =~ m/([\w\-\(\)]+)#$/ ) {
86 save_log $ip, $hostname, $command, $buff;
89 if ( $command = shift @commands_while ) {
90 $command =~ s/[\n\r]+$//;
91 send_pty "$command\n";
98 } elsif ( $buff =~ m/% Unrecognized command/ ) {
100 } elsif ( $buff =~ s{More: <space>, Quit: q.*One line: <return>\s*}{} ) {
102 } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
104 } elsif ( $buff =~ s{\e\[0m\s*\r\s+\r}{} ) {
106 } elsif ( $buff =~ m/^[\r\n]+[\w\-]+>$/ ) {
108 } elsif ( $buff =~ m{\QOverwrite file [startup-config] ?[Yes/press any key for no]....\E} ) {
111 } elsif ( $buff =~ s{Management access will be blocked for the duration of the transfer.*Are you sure you want to start\? \(y/n\) }{}s ) {
113 } elsif ( $buff =~ s{\QThis command will reset the whole system and disconnect your current session.\E}{}s ) { # reload
114 warn "\nRELOAD detected\n";
117 } elsif ( $buff =~ m{MikroTik RouterOS} ) {
118 warn "\nERROR: don't know how to talk to MicroTik - ABORTING";
129 show interfaces status