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

hi perlmonks
iam wroking with Net::SSH::Perl (1.27)
it is not wroking with multiple cmds like this
$ssh->cmd("cd /Net::SSH::Perl"); $ssh->cmd("ls -lrt");
it is not showing the Net::SSH::Perl files it is showing my home directory files.
iam using SSH2 protocol (protocol=>2)
which version of Net::SSH::Perl supports my above requiremnet?
  • Comment on why single channel is not supporting multiple cmds in NET::SSH::PERL
  • Download Code

Replies are listed 'Best First'.
Re: why single channel is not supporting multiple cmds in NET::SSH::PERL
by shmem (Chancellor) on Jul 23, 2006 at 12:33 UTC
    That's because $ssh->cmd is not for interactive use. Each $ssh->cmd is executed over the same channel, but not with the same shell.

    Please read the section $ssh->shell in Net::SSH::Perl.

    Note that in the Win32 extension the shell command is not available according to the Net::SSH::W32Perl documentation.

    To chain commands you have to write them in a chain in shell syntax for $ssh->cmd, i.e.

    $ssh->cmd("cd /Net::SSH::Perl && ls -lrt");

    --shmem

    just another note - in many of your posts, you misspell working as wroking, which is worng ;) say working.

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: why single channel is not supporting multiple cmds in NET::SSH::PERL
by Joost (Canon) on Jul 23, 2006 at 12:28 UTC
    This question again?

    From the docs:

    NOTE: the SSH-1 protocol does not support running multiple commands per connection, unless those commands are chained together so that the remote shell can evaluate them. Because of this, a new socket connection is created each time you call cmd, and disposed of afterwards.

    I suggest you run a single shell script with all your commands instead.

Re: why single channel is not supporting multiple cmds in NET::SSH::PERL
by McDarren (Abbot) on Jul 23, 2006 at 12:27 UTC
    You need to capture the STDOUT, STDERR and return values of $ssh->cmd, so that you can see what is going wrong. Something like:
    my ($out, $err, $exit) = $ssh->cmd("....");

    If you then examine the value of $err, you'll almost certainly see that you've tried to cd to a non-existent directory.

    And the reason why you see the contents of your home directory when you issue the second command is because the first failed.

    Exactly what is it that you are trying to do?