in reply to safely passing args through ssh
Let's see: ssh does not touch the STDIN, STDOUT, STDERR streams. But it invokes the remote standard shell to handle arguments. So, get rid of the shell parsing.
Perl can read a program from STDIN, it stops reading at EOF or when it finds the magic "__END__" marker on a line. So, feed the remote Perl a program that implements some kind of read-eval-print loop (you may also call that a RPC server, if you wish), then send commands over STDIN, read results back from STDOUT.
Working (stupid) example:
#!/usr/bin/perl -w use strict; use IPC::Open2; # or IPC::Open3 my ($in,$out); $|=1; my $pid=open2($out,$in,'/usr/bin/ssh','foken@10.50.20.82','/usr/bin/pe +rl'); print $in <<'__end_of_perl__'; $|=1; use strict; use warnings; print "ok, running with PID $$\n"; while (<STDIN>) { chomp; print "File $_ has a size of ",-s($_)," bytes\n"; } # the next line is important: __END__ __end_of_perl__ print "Loaded\n"; print "Reading boot message: "; my $info=<$out>; print $info; for my $file ('/etc/passwd','/etc/motd','/etc/issue') { print "Requesting $file\n"; print $in "$file\n"; print "Reading: "; $info=<$out>; print $info; }
Output:
Loaded Reading boot message: ok, running with PID 13498 Requesting /etc/passwd Reading: File /etc/passwd has a size of 1468 bytes Requesting /etc/motd Reading: File /etc/motd has a size of 20 bytes Requesting /etc/issue Reading: File /etc/issue has a size of 24 bytes
To do:
Alexander
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: safely passing args through ssh
by perl5ever (Pilgrim) on Jul 29, 2011 at 21:37 UTC |