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 | |
by thanos1983 (Parson) on May 21, 2014 at 14:55 UTC |