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,
35 ssh_cmd => '/usr/bin/ssh1', # apt-get install openssh-client-ssh1
37 -o => "StrictHostKeyChecking=no",
38 -F => '/home/dpavlin/dell-switch/ssh1-config'
41 -o => "StrictHostKeyChecking=no",
42 -F => '/home/dpavlin/dell-switch/ssh1-config'
45 my ($pty ,$pid) = $ssh->open2pty();
47 warn "ERROR: can't connect to $ip, skipping";
55 sleep 0.05; # we really need to wait for slow PowerConnect 5324
56 foreach (split //, $string) {
57 print STDERR "[$_]" if $debug;
62 sysread $pty, my $echo, 1;
68 mkdir 'log' unless -d 'log';
73 my ($ip, $hostname, $command, $buff) = @_;
75 return unless $command;
76 return if $ENV{NO_LOG};
78 my $file = "${ip}_${hostname}_${command}.log";
79 open my $log, '>', $file;
80 $buff =~ s/\r//gs; # strip CR, leave LF only
83 system 'git', 'add', $file;
84 system 'git', 'commit', '-m', "$ip $hostname", $file;
89 my @commands_while = ( @commands );
93 my $read = sysread($pty, $data, 1);
96 if ( $buff =~ m/User Name:/ ) {
99 } elsif ( $buff =~ m/Password:/ ) {
100 send_pty "$passwd\n";
102 } elsif ( $buff =~ m/[\n\r]([\w\-\(\)]+)#$/ ) {
105 save_log $ip, $hostname, $command, $buff;
108 if ( $command = shift @commands_while ) {
109 $command =~ s/[\n\r]+$//;
110 send_pty "$command\n";
117 } elsif ( $buff =~ m/% Unrecognized command/ ) {
119 } elsif ( $buff =~ s{More: <space>, Quit: q.*One line: <return>\s*}{} ) {
121 } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
123 } elsif ( $buff =~ s{\e\[0m\s*\r\s+\r}{} ) {
125 } elsif ( $buff =~ m/^[\r\n]+[\w\-]+>$/ ) {
127 } elsif ( $buff =~ m{\QOverwrite file [startup-config] ?[Yes/press any key for no]....\E} ) {
130 } elsif ( $buff =~ s{Management access will be blocked for the duration of the transfer.*Are you sure you want to start\? \(y/n\) }{}s ) {
132 } elsif ( $buff =~ s{\QThis command will reset the whole system and disconnect your current session.\E}{}s ) { # reload
133 warn "\nRELOAD detected\n";
136 } elsif ( $buff =~ m{MikroTik RouterOS} ) {
137 warn "\nERROR: don't know how to talk to MicroTik - ABORTING";
148 show interfaces status