jon75 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks. I'm trying to use this module to connect to a remote machine, tar up a directory and then scp_get the file back across the firewall. Is it possible to run a series of system calls within a single $ssh->system() as you would with a regular Perl system call? I've tried a couple of variations but neither work. e.g. cd, tar, mv Thanks.

Replies are listed 'Best First'.
Re: Net::OpenSSH mutliple commands
by salva (Canon) on Jul 27, 2010 at 12:02 UTC
    Net::OpenSSH (and actually also Net::SSH2 and Net::SSH::Perl) starts a new session for every command run, so the effects of operations as changing the working directory or setting environment variables do not persist between commands.

    One way to overcome that is to join all the commands together:

    $ssh->system("cd $dir && tar czf /tmp/file.tgz .");

    In your case, you can also eliminate the scp_get step transferring the archive as it gets generated:

    $ssh->system({stdout_file => '/tmp/file.tgz'}, "cd $dir && tar czf - . +");
Re: Net::OpenSSH mutliple commands
by jethro (Monsignor) on Jul 27, 2010 at 11:43 UTC
Re: Net::OpenSSH mutliple commands
by Anonymous Monk on Sep 06, 2010 at 20:31 UTC
    If the box you are joining is running a classical unix, it is possible to execute easily multiline commands, even conditional or loop.
    $prog=<<'EOF' for i in `ls` do echo $i done EOF ; print $ssh->system( {stdin_data=>$prog},"bash");
    Easy, isn't it ?