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

Hi Guys,

I'm very new to perl and this is my first perl program...

I'm trying to write a script for lanching xterm thru LSF on one of the servers we have and Code I'm using is

#! /usr/local/bin/perl -w use Net::SSH::Perl; use strict; #my $host="192.168.10.32"; my $host="hostname"; my $usr="venkatas"; my $pass="password"; my $ssh = Net::SSH::Perl->new($host,debug=> 1, protocol=> '2,1'); $ssh->login($usr,$pass); my $job1 = $ssh->cmd("source /home/venkatas/.cshrc") ; my $job2 = $ssh->cmd('setenv DISPLAY linux37:0'); my $job3 = $ssh->cmd("bsub -q linux64_idc_1g xterm & " ); when I'm executing this script I'm getting following problem linux37: Reading configuration data /home/venkatas/.ssh/config linux37: Reading configuration data /etc/ssh_config linux37: Connecting to netli01, port 22. linux37: Remote protocol version 1.99, remote software version OpenSSH +_3.9p1 linux37: Net::SSH::Perl Version 1.34, protocol version 2.0. linux37: No compat match: OpenSSH_3.9p1. linux37: Connection established. linux37: Sent key-exchange init (KEXINIT), wait response. linux37: Algorithms, c->s: 3des-cbc hmac-sha1 none linux37: Algorithms, s->c: 3des-cbc hmac-sha1 none linux37: Entering Diffie-Hellman Group 1 key exchange. linux37: Sent DH public key, waiting for reply. linux37: Received host key, type 'ssh-dss'. linux37: Host 'netli01' is known and matches the host key. linux37: Computing shared secret key. linux37: Verifying server signature. linux37: Waiting for NEWKEYS message. linux37: Send NEWKEYS. linux37: Enabling encryption/MAC/compression. linux37: Sending request for user-authentication service. linux37: Service accepted: ssh-userauth. linux37: Trying empty user-authentication request. linux37: Authentication methods that can continue: publickey,gssapi-wi +th-mic,password. linux37: Next method to try is publickey. linux37: Next method to try is password. linux37: Trying password authentication. linux37: Login completed, opening dummy shell channel. linux37: channel 0: new [client-session] linux37: Requesting channel_open for channel 0. linux37: channel 0: open confirm rwindow 0 rmax 32768 linux37: Got channel open confirmation, requesting shell. linux37: Requesting service shell on channel 0. linux37: channel 1: new [client-session] linux37: Requesting channel_open for channel 1. linux37: Entering interactive session. linux37: Sending command: setenv DISPLAY linux37:0 linux37: Sending command: setenv DISPLAY linux37:0 linux37: Requesting service exec on channel 1. linux37: channel 1: open confirm rwindow 0 rmax 32768 ** Standard Error - I received this: LD_LIBRARY_PATH: Undefined variable. linux37: channel 1: rcvd eof linux37: channel 1: output open -> drain linux37: channel 1: obuf empty linux37: channel 1: output drain -> closed linux37: channel 1: close_write linux37: input_channel_request: rtype exit-status reply 0 linux37: channel 1: rcvd close linux37: channel 1: input open -> closed linux37: channel 1: close_read linux37: channel 1: send close linux37: channel 1: full closed linux37: channel 2: new [client-session] linux37: Requesting channel_open for channel 2. linux37: Entering interactive session. linux37: Sending command: bsub -q linux64_idc_1g xterm & linux37: Sending command: bsub -q linux64_idc_1g xterm & linux37: Requesting service exec on channel 2. linux37: channel 2: open confirm rwindow 0 rmax 32768 ** Standard Error - I received this: LD_LIBRARY_PATH: Undefined variable. ** Standard Error - I received this: bsub: Command not found. linux37: channel 2: rcvd eof linux37: channel 2: output open -> drain linux37: input_channel_request: rtype exit-status reply 0 linux37: channel 2: rcvd close linux37: channel 2: input open -> closed linux37: channel 2: close_read ** Standard Out - I received this: [1] 28668 [1] Exit 1 bsub -q linux64_idc_1g xterm linux37: channel 2: obuf empty linux37: channel 2: output drain -> closed linux37: channel 2: close_write linux37: channel 2: send close linux37: channel 2: full closed

Can anybody help me out in this

--Venkata

Replies are listed 'Best First'.
Re: Problem while using Net::SSH::Perl module
by almut (Canon) on Oct 09, 2009 at 12:43 UTC
    bsub: Command not found.

    Maybe try absolute path to bsub command...

Re: Problem while using Net::SSH::Perl module
by salva (Canon) on Oct 09, 2009 at 13:14 UTC
    Your script does not work because SSH launches a new shell for every command. You have to put all the commands in the same cmd invocation using the semicolon (;) or the and operator (&&) to join them:
    my $job = $ssh->cmd(<<SHELL); source /home/venkatas/.cshrc; setenv DISPLAY linux37:0; bsub -q linux64_idc_1g xterm & SHELL
    Though, this will not work either, because the cmd method would not return control until the xterm is closed, even if it is run in the background. You will have to use the non-blocking features of Net::SSH::Perl for that.
      Though, this will not work either, because the cmd method would not return control until the xterm is closed

      I think the idea here is to submit the xterm command to the linux64_idc_1g LSF queue, so LSF will then run the command (when it gets around to it).  In other words, the bsub command should return more or less immediately.  (Also, the bsub command should "capture" the current environment (such as DISPLAY) and LSF should reinstate it when the queued command is being run. So, it could in theory work, provided the X server that DISPLAY points to allows access from the (possibly remote) LSF cluster machine...)

        oh, I see, but then, the ampersand is useless!

      Thanks for suggesting me to use SHELL in my code but I'm unable to source my .cshrc on remote host the way you mentioned.

      Can you help me in this too.

      thanks in advance.

        it works for me, so, unless you give us some more details about how it fails for you, we will be unable to help you further!
Re: Problem while using Net::SSH::Perl module
by mickep76 (Beadle) on Oct 09, 2009 at 12:30 UTC

    Looks like a job more suited for BASH. And for password use Public Keys and ssh-agent.

    #!/bin/bash script=/var/tmp/run.sh runScript() { local host=$1 local file=$2 scp $file root@$host:$script ssh root@$host "bash $script" ssh root@$host "rm -f $script" } host=192.168.10.32 cat <<%%SCRIPT%% #!/bin/bash source /home/venkatas/.cshrc setenv DISPLAY linux37:0 bsub -q linux64_idc_1g xterm & %%SCRIPT%% >$script runScript $host $script
    Just because you know how to use a hammer doesn't mean it's the best tool to make a hole.