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

Is there a way to ssh2 to a server, run a shell script and wait for it to end for the return code?

The shell script takes about 4 minutes to run

code snippet:

sub run_load { push @LOG, "Executing Shell script: > $rfile <\n"; my $chan = $ssh2->channel(); #$ssh2->debug(1); $chan->shell(); print $chan "$path/../{some_shell_script.sh}\n"; select(undef,undef,undef,20.2); my ($len,$buf); while ($len = $chan->read($buf,512) > 0) { push @LOG, "EXEC_LINE : $buf"; print "EXEC_LINE : $buf" if $dbug; } $chan->close; }

Thanks,
Mark

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: Net::SSH2::Channel question
by talexb (Chancellor) on Nov 06, 2007 at 18:18 UTC

    You can probably use Net::SSH::Expect to do that.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      Sorry... I failed to mention that I am running Perl on winders and I don't think that Net::SSH::Expect was built for the windows platform.
Re: Net::SSH2::Channel question
by zentara (Cardinal) on Nov 07, 2007 at 13:40 UTC
    I havn't tested this, but the perldoc for Net::SSH2::Channel has a method for wait_closed and exit_status.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Is there any good examples on how to use these? The doc doesn't really have a good explanation or example. Thanks
        The best place to ask is the maillist at

        ssh maillist

        Your best bet is to search the archives there for those key words, and if that fails, ask on the list.


        I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Net::SSH2::Channel question
by cmv (Chaplain) on Nov 07, 2007 at 01:57 UTC
    drfunk2548-

    Unless I'm missing something here, I think you simply want to use Net::SSH::Perl.
    The example in the docs does a great job of what you're asking:

    use Net::SSH::Perl; my $ssh = Net::SSH::Perl->new($host); $ssh->login($user, $pass); my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
    Hope that helps.

    -Craig

      I agree that this would be a simple solution. However, maybe I am missing something b/c when I run the simple example the code hangs <forever> ... see below

      bash$ ./test-ssh.pl
      MYWIN: Reading configuration data /.ssh/config
      MYWIN: Reading configuration data /etc/ssh_config
      MYWIN: Connecting to {host}, port 22.
      MYWIN: Remote protocol version 2.0, remote software version OpenSSH_3.9p1
      MYWIN: Net::SSH::Perl Version 1.28, protocol version 2.0.
      MYWIN: No compat match: OpenSSH_3.9p1.
      MYWIN: Connection established.
      MYWIN: Sent key-exchange init (KEXINIT), wait response.
      MYWIN: Algorithms, c->s: 3des-cbc hmac-sha1 none
      MYWIN: Algorithms, s->c: 3des-cbc hmac-sha1 none
      MYWIN: Entering Diffie-Hellman Group 1 key exchange.
      MYWIN: Sent DH public key, waiting for reply.
      MYWIN: Received host key, type 'ssh-dss'.
      MYWIN: Host 'xx.xx.xx.xx' is known and matches the host key.
      MYWIN: Computing shared secret key.
      MYWIN: Verifying server signature.
      MYWIN: Waiting for NEWKEYS message.
      MYWIN: Enabling incoming encryption/MAC/compression.
      MYWIN: Send NEWKEYS, enable outgoing encryption/MAC/compression.
      MYWIN: Sending request for user-authentication service.
      MYWIN: Service accepted: ssh-userauth.
      MYWIN: Trying empty user-authentication request.
      MYWIN: Authentication methods that can continue: publickey,password,keyboard-interactive.
      MYWIN: Next method to try is publickey.
      MYWIN: Next method to try is password.
      MYWIN: Trying password authentication.
      MYWIN: Login completed, opening dummy shell channel.
      MYWIN: channel 0: new client-session
      MYWIN: Requesting channel_open for channel 0.
      MYWIN: channel 0: open confirm rwindow 0 rmax 32768
      MYWIN: Got channel open confirmation, requesting shell.
      MYWIN: Requesting service shell on channel 0.
      MYWIN: channel 1: new client-session
      MYWIN: Requesting channel_open for channel 1.
      MYWIN: Entering interactive session.
      MYWIN: Sending command: /usr/WebSphere/CommerceServer/bin/dataload/data/../test1.sh
      MYWIN: Requesting service exec on channel 1.
      MYWIN: channel 1: open confirm rwindow 0 rmax 32768

        Can you try running a simple command like /usr/bin/date and see how and what it returns?

        I'm guessing that your shell script isn't working because of environment differences. Remember, when you remotely run a command via ssh, you don't get the same environment as you get after logging in normally.

        One thing I like to do is remotely run the set command under ksh. This will dump the environment of your remote commands so you can see what you have v.s. what you need.

        -Craig