Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

SSH and Scripting

by Anonymous Monk
on May 20, 2014 at 19:36 UTC ( [id://1086849]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks

I am trying to SSH into a remote machine and execute a shell script. So far I can access the machine and I can run basic administrative commands such as, pwd, ls ,df -h, but when I attempt to use sh, perl, or any of the SGE commands (qsub, qstat) my script fails. Ideally I want to use the SGE commands directly, but I would be happy if I can get a shell script to run and thereby accomplish what I need to do (check status of a job and hopefully one day actually submit a job). Any help is greatly appreciated.

Below is my existing script:

use warnings; use strict; use Perl::Critic; use Net::SSH::Perl; use Net::SSH::Perl::Key::RSA; use Class::ErrorHandler; my $server="400.800.400.800"; #not a real IP address my $port="22"; my $username="snoopy"; #my $command="df -h"; #this worked #my $command="ls"; #this worked #my $command="sh status.sh"; #Received error Use of uninitialized valu +e $stdout in string at ssh.pl #my $command="/usr/bin/sh status.sh"; #Received error Use of uninitial +ized value $stdout in string at ssh.pl #my $command="qstat"; #Received error Use of uninitialized value $stdo +ut in string at ssh.pl line my $command="pwd"; #this worked my $ssh = Net::SSH::Perl->new("$server", protocol=> 2, port=>"$port", +identity_files=>["/location/of/my/identity/files/file"]); $ssh->login($username); my($stdout, $stderr, $exit) = $ssh->cmd("$command"); print "$stdout";

Replies are listed 'Best First'.
Re: SSH and Scripting
by Bloodnok (Vicar) on May 21, 2014 at 09:01 UTC
    You are sure that the ssh config allows you to run the failing commands, aren't you ?

    Also, as anony monk has already said, try printing the value of $stderr to get at the error from the ssh session e.g. print $stdout ? $stdout : $stderr;.

    You could also try pre-fixing status.sh with $HOME i.e. $HOME/status.sh since . is not normally on the path for an ssh login because of it's inherent insecurity, ensuring that the command is set up within single quotes in order to ensure that it is expanded at the remote end $command = '/usr/bin/sh $HOME/status.sh';.

    A user level that continues to overstate my experience :-))
Re: SSH and Scripting
by NetWallah (Canon) on May 20, 2014 at 20:05 UTC
    Try printing out $stderr ...

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1086849]
Approved by Jim
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found