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

Dear Monks,

My initial goal is to connect to a remote host though SSH apply one command or several (through bash script) catch the output and exit.

It seems and it is extremely easy but some unknown reason I can not get the output.

The message that I am getting is:

testOS: Use of uninitialized value $stdout in print at ssh.pl line 32.

With debug mode on (debug => 1), part of the output:

testOS: Use of uninitialized value $stdout in print at ssh.pl line 32. testOS: channel 2: new [client-session] testOS: Requesting channel_open for channel 2. testOS: Entering interactive session. testOS: Requesting service exec on channel 2. testOS: channel 2: open confirm rwindow 0 rmax 32768 testOS: input_channel_request: rtype exit-status reply 0 testOS: channel 2: rcvd eof testOS: channel 2: output open -> drain testOS: channel 2: rcvd close testOS: channel 2: input open -> closed testOS: channel 2: close_read testOS: channel 2: obuf empty testOS: channel 2: output drain -> closed testOS: channel 2: close_write testOS: channel 2: send close testOS: channel 2: full closed

Since this is my first Perl/ssh script, I am kind of lost. I run out of options, all the examples online point that it should be working.

Secondary question, in case that I want to execute a bash script through Perl/ssh is this syntax correct:

$cmd = `/bin/bash test.sh`;

And with script:

system("/bin/bash test.sh") == 0 or die "Bash Script failed";

A working example:

#!/usr/bin/perl use Expect; use strict; use warnings; use Data::Dumper; use Net::SSH::Perl; # SSH connection through Perl $| = 1; my $host = xxxx; my $port = xxxx; my $user = xxxx; my $password = xxxx; my $cmd = `ls -l`; # my $cmd = `/bin/bash test.sh`; #system("/bin/bash test.sh") == 0 # or die "Bash Script failed"; my $ssh = Net::SSH::Perl->new( "".$host."" , port => "".$port."" , protocol => 2 , interactive => 1 , batchMode => 1 , RhostsAuthentication => 1, debug => 1 ); $ssh->login("".$user."","".$password.""); my($stdout, $stderr, $exit) = $ssh->cmd($cmd); #$ssh->shell; print $stdout; $ssh->cmd("exit");

If I remove the bash (#) from the $ssh->shell, I can even enter the remote terminal. But I just want to apply a script and exit remotely.

Thanks in advance for the time and effort.

Replies are listed 'Best First'.
Re: Net::SSH::Perl (Use of uninitialized value $stdout)
by boftx (Deacon) on May 21, 2014 at 14:31 UTC

    Unless I am not fully awake yet, it appears that you are using backtics "`" as opposed to single quotes "'" to assign a value:

    my $cmd = `ls -l`;
    The end effect of this is that you are telling perl to execute the command inside the backtics, and place any output from STDOUT in the variable $cmd. I presume that what you want is to assign the string consisting of 'ls -l' to $cmd which is then executed on the remote host.

    EDIT: Simplistic demo of above:

    $ cat backtictest.pl #!/usr/bin/perl use strict; use warnings; my $cmd_sq = 'ls -l'; print "Using single quotes: $cmd_sq\n\n"; my $cmd_bt = `ls -l`; print "Using backtics: $cmd_bt\n"; exit; __END__ Output: Macintosh-45:stuff user$ ./backtictest.pl Using single quotes: ls -l Using backtics: -rw-r--r-- 1 user group 260 May 6 17:58 Class1.pm -rw-r--r-- 1 user group 260 May 6 17:58 Class2.pm -rw-r--r-- 1 user group 331 May 6 17:58 MyExceptions.pm -rwx------ 1 user group 182 May 21 07:38 backtictest.pl

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

      To: boftx

      OMG, it was simple and I am spending so much time for something so simple. I guess when my brain get's stack to something keeps thinking that is correct.

      Thanks a lot for something so small I could end up spending the rest of the day just trying to figure out what was wrong.