[Thamesvalley-pm] using ssh within perl scripts

Greg Matthews gmatt at nerc.ac.uk
Tue Oct 30 10:45:10 PDT 2007

Seems like I'm the only one to ask for advice here...

I'm using ssh to connect to a series of machines like so:

open( SSH, "$ssh_bin $ssh_opts $host $command |") or die...
while (<SSH>) {
close SSH;

I'm opening a new ssh shell for each command.

I want to capture the output of various commands to a set of lists or 
files. For instance, the output of "uname -a" needs to be recorded in a 
file uname.out.

Is it possible to send a series of commands through the same SSH 
session? and capture the output for each separately?

That would save repetitive long timeouts when hosts are down.

So far I have figured out that I can open ssh like this:

open(SSH,"| /usr/bin/ssh $options $host");
print SSH "$command1\n";
print SSH "$command2\n";
close SSH;

but I haven't figured out how to capture the results of each command 

hope this makes sense and someone can point me the right direction


Greg Matthews           01491 692445
Head of UNIX/Linux, iTSS Wallingford

This message (and any attachments) is for the recipient only. NERC
is subject to the Freedom of Information Act 2000 and the contents
of this email and any reply you make may be disclosed by NERC unless
it is exempt from release under the Act. Any material supplied to
NERC may be stored in an electronic records management system.

More information about the Thamesvalley-pm mailing list