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

Is it possible to run the shell script on remote machine in ssh->cmd option Actually my requirement is login through ssh using perl Net::SSH::Perl and run the shell script on remote machine
  • Comment on Passing the shell script in ssh->cmd option

Replies are listed 'Best First'.
Re: Passing the shell script in ssh->cmd option
by kennethk (Abbot) on Dec 24, 2008 at 22:40 UTC
    A little more context for you request would be helpful. If the shell script is housed on the remote server, you should be able to just call it. If it is local, a shell script is just a series of shell commands so there should be nothing preventing you from executing it remotely line-by-line. Have you attempted to implement this yet?
      Local server is A and Remote Server is B Shell Script is housed on the local server A. for example we have a script called test.sh in local server A It is possible to pass this test.sh script to ssh->cmd option ssh->cmd(test.sh) whether ssh->cmd option will accept shell script as argument syntax -> ssh RemoteServer LocalScript example -> ssh RemoteB test.sh I want to run above sequence using Net::SSH and ssh->cmd option

        You cannot just pass a file name to cmd and expect it to read the file, parse it, transmit it and execute it. See Net::SSH::Perl. In particular, note that each string passed to cmd is executed in an independent session. The easiest solution to your proposal is to copy the script to the remote server, so it can be invoked with a single command. Something along these lines could also function for you:

        open(FILEHANDLE, '<test.sh'); my @file = <FILEHANDLE>; close FILEHANDLE; ssh->cmd(join ';', @file);

Re: Passing the shell script in ssh->cmd option
by cmv (Chaplain) on Dec 26, 2008 at 18:42 UTC
    saravanansh-

    No problem! I do this all of the time with Net::SSH::Perl. You must make sure you have your quotes and escapes correct (which usually takes a bit of fiddling), but I usually build the scripts up command-by-command while testing them out. Also remember that you usually get /bin/sh as your shell unless you specify otherwise.

    I usually end up with things as shown below. This particular ksh shell script will log the event in a logfile, then pre-process what I send on STDIN via Net::SSH::Perl, and provide it to a local command on the destination machine. The results ends up in $stdout.

    my $cmd = '/usr/bin/ksh -c \' LOGDIR=/log/predecode LOGFILE=$LOGDIR/usage`date \'+%m\'` if [ -w $LOGFILE ] ; then echo "$LOGNAME; `date \"+%D %T\"`;`uname -a`;${DISPLAY:-SS +H:$SSH_CONNECTION}" >>$LOGFILE fi while read LINE do if [ "$LINE" == "" ] then continue; fi PRE="${LINE%%\|*}" POST="${LINE#[0-9]*\|}" if [ "${POST%%[A-Z]*}" == "_" ] then print "$PRE|$POST" continue; fi ( print -n "$PRE|"; echo $POST | ' . /usr/bin/farendcommand . ' print "\n" ) done\''; my ($stdout, $stderr, $exit) = $ssh->cmd($cmd, $inputForFarEnd);
      Is it possible to redirect the ssh->cmd output command output to some scalar variable $cmd="ls"; ssh->cmd($cmd); my requirement is to store ls output to one scalar variable. as per the above example ls output should be stored in the some scalar variable $test.