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);
my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n";
$ip = $1 if `host $ip` =~ m/has address (\S+)/;
my @commands = @ARGV;
-@commands = <DATA> unless @commands;
+if ( ! @commands && ! -t STDIN && -p STDIN ) { # we are being piped into
+ while(<>) {
+ push @commands, $_;
+ }
+} else {
+ @commands = <DATA> unless @commands;
+}
warn "\n## ssh $ip\n";
-my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd, default_ssh_opts => [-o => "StrictHostKeyChecking=no"]);
+my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd,
+ ssh_cmd => '/usr/bin/ssh1', # apt-get install openssh-client-ssh1
+ master_opts => [
+ -o => "StrictHostKeyChecking=no",
+ -F => '/home/dpavlin/dell-switch/ssh1-config'
+ ],
+ default_ssh_opts => [
+ -o => "StrictHostKeyChecking=no",
+ -F => '/home/dpavlin/dell-switch/ssh1-config'
+ ],
+);
my ($pty ,$pid) = $ssh->open2pty();
if ( ! $pty ) {
warn "ERROR: can't connect to $ip, skipping";
$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;