in reply to SSH and expect without module
my goal is to connect in SSH to several material and without the result in STDOUT, pass in console some command.
It is even more simple than what you tried. Sample transcript:
qwurx [shmem] ~> perl -e 'open my $fh,"|-","ssh localhost"; while(<STD +IN>){print $fh $_}' Pseudo-terminal will not be allocated because stdin is not a terminal. The programs included with the Debian GNU/Linux system are free softwa +re; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. You have new mail. touch blorflydick <--- input at terminal touch gobbledygook <--- input at terminal ^D <--- input at terminal (Ctrl-D or eof) qwurx [shmem] ~> ls -lrt | tail -2 <--- after perl terminated -rw-r--r-- 1 shmem shmem 0 Mar 15 18:30 blorflydick -rw-r--r-- 1 shmem shmem 0 Mar 15 18:30 gobbledygook
No modules required. If you just want to pass some commands and don't care about STDOUT/STDERR, all you have to do is a plain piped open. If you want to multiplex the commands to several hosts, store the filehandles in an array and print your input to the remote shells in a loop (untested):
my @fh; for my $ip (@ips) { open my $fh,"|-","ssh $ip" or die "Can't ssh to $ip: $!\n"; push @fh, $fh; } while (<STDIN>) { for my $fh (@fh) { print $fh $_; } }
You might, since you are not interested in STDOUT/STDERR, redirect all output within the piped open to avoid funny blocking errors and/or spamming your terminal:
open my $fh,"|-","ssh $ip >/dev/null 2>&1" or die "Can't ssh to $i +p: $!\n";
and/or set $|=1 (see perlvar) and/or call $fh->autoflush on every handle created. Finding out the difference between these measures is left as an excercise to the reader.
update: the ssh connection in this example was established using ssh keys and a running ssh-agent. If you have to enter (different) credentials you want to set $fh->autoflush and use sshpass to pass the passwords (for each connection).
|
|---|