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.


In reply to Net::SSH::Perl (Use of uninitialized value $stdout) by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.