6 # example usage as pipe:
7 # ./ips | sed 's/^/ping /' | NO_LOG=1 ./dell-switch.pl sw-dpc
10 use Data::Dump qw(dump);
11 use Time::HiRes qw(sleep);
15 our $debug = $ENV{DEBUG} || 0;
20 #$Net::OpenSSH::debug = ~0;
22 my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n";
23 $ip = $1 if `host $ip` =~ m/has address (\S+)/;
25 if ( ! @commands && ! -t STDIN && -p STDIN ) { # we are being piped into
30 @commands = <DATA> unless @commands;
33 warn "\n## ssh $ip\n";
34 my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd, default_ssh_opts => [-o => "StrictHostKeyChecking=no"]);
35 my ($pty ,$pid) = $ssh->open2pty();
37 warn "ERROR: can't connect to $ip, skipping";
45 sleep 0.05; # we really need to wait for slow PowerConnect 5324
46 foreach (split //, $string) {
47 print STDERR "[$_]" if $debug;
52 sysread $pty, my $echo, 1;
58 mkdir 'log' unless -d 'log';
63 my ($ip, $hostname, $command, $buff) = @_;
65 return unless $command;
66 return if $ENV{NO_LOG};
68 my $file = "${ip}_${hostname}_${command}.log";
69 open my $log, '>', $file;
70 $buff =~ s/\r//gs; # strip CR, leave LF only
73 system 'git', 'add', $file;
74 system 'git', 'commit', '-m', "$ip $hostname", $file;
79 my @commands_while = ( @commands );
83 my $read = sysread($pty, $data, 1);
86 if ( $buff =~ m/User Name:/ ) {
89 } elsif ( $buff =~ m/Password:/ ) {
92 } elsif ( $buff =~ m/([\w\-\(\)]+)#$/ ) {
95 save_log $ip, $hostname, $command, $buff;
98 if ( $command = shift @commands_while ) {
99 $command =~ s/[\n\r]+$//;
100 send_pty "$command\n";
107 } elsif ( $buff =~ m/% Unrecognized command/ ) {
109 } elsif ( $buff =~ s{More: <space>, Quit: q.*One line: <return>\s*}{} ) {
111 } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
113 } elsif ( $buff =~ s{\e\[0m\s*\r\s+\r}{} ) {
115 } elsif ( $buff =~ m/^[\r\n]+[\w\-]+>$/ ) {
117 } elsif ( $buff =~ m{\QOverwrite file [startup-config] ?[Yes/press any key for no]....\E} ) {
120 } elsif ( $buff =~ s{Management access will be blocked for the duration of the transfer.*Are you sure you want to start\? \(y/n\) }{}s ) {
122 } elsif ( $buff =~ s{\QThis command will reset the whole system and disconnect your current session.\E}{}s ) { # reload
123 warn "\nRELOAD detected\n";
126 } elsif ( $buff =~ m{MikroTik RouterOS} ) {
127 warn "\nERROR: don't know how to talk to MicroTik - ABORTING";
138 show interfaces status