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

I am using Net::SSH::Perl to ssh and run command. I can not get print stdout or stderr. Uninitialized error is reported. Looks like module is initializing and somewhere it's not reporting. Any thoughts will will helpful.

script output START

Creating ssh object... done Logging into server... done Running command (df)... done Use of uninitialized value $stdout in concatenation (.) or string at p +erlscript.pl line 28.

script output END

script START

#!/usr/bin/env perl -w use strict; use warnings; use Net::SSH::Perl; ###################################################################### +# # LOGIN DETAILS ###################################################################### +# my $server = "10.255.255.10"; my $username = "user"; my $password = "password"; # Log into server print "Creating ssh object... "; my $ssh = Net::SSH::Perl->new($server); # Error check this print "done\n"; print "Logging into server... "; $ssh->login($username, $password); # Error check this print "done\n"; # Check df my $command = "df"; print "Running command ($command)... "; my ($stdout, $stderr, $exit) = $ssh->cmd($command); # Check output print "done\n"; print "$stdout\n"; $ssh->cmd("exit");

script END

Debug output START

Use of uninitialized value $stdout in concatenation (.) or string at p +erlscript.pl line 28. server-01: channel 2: new [client-session] server-01: Requesting channel_open for channel 2. server-01: Entering interactive session. server-01: Requesting service exec on channel 2. server-01: channel 2: open confirm rwindow 0 rmax 32768 server-01: input_channel_request: rtype exit-status reply 0 server-01: input_channel_request: rtype eow@openssh.com reply 0 server-01: channel 2: rcvd eof server-01: channel 2: output open -> drain server-01: channel 2: rcvd close server-01: channel 2: input open -> closed server-01: channel 2: close_read server-01: channel 2: obuf empty server-01: channel 2: output drain -> closed server-01: channel 2: close_write server-01: channel 2: send close server-01: channel 2: full closed [root@server-01 Name]#

Debug output END

Replies are listed 'Best First'.
Re: Uninitalized error for Stdout and Stderr from Net::SSH::Perl
by QM (Parson) on May 11, 2015 at 10:44 UTC
    Not seeing a reply, I'll dive in here. Perhaps you didn't check $stderr and $exit? $stdout may be undefined if there is an error, but your code doesn't check any of those conditions.

    Have you tried this from the command line? Perhaps df needs the full pathname?

    I can't make any progress on your debug output.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      Updated code to check $stderr and $exit. $stdout and $stderr have undefined. Do I need to initialize $STDOUT to $stdout

      Output from the updated code

      #!/usr/bin/env perl -w use strict; use warnings; use Data::Dumper; use Net::SSH::Perl; ###################################################################### +# # LOGIN DETAILS ###################################################################### +# my $server = "10.255.255.10"; my $username = "username"; my $password = "password"; # Log into server print "Creating ssh object... "; my $ssh = Net::SSH::Perl->new($server); # Error check this print "done\n"; print "Logging into server... "; $ssh->login($username, $password); # Error check this print "done\n"; # Check df my $command = 'df'; print "Running command ($command)... "; my ($stdout, $stderr, $exit) = $ssh->cmd($command); # Check output print "done\n"; print "$stdout\n"; print "$stderr\n"; print "$exit\n"; $ssh->cmd("exit");
      [root@server-01 New_script]#perl New_script.pl Creating ssh object... done Logging into server... done Running command (df)... done Use of uninitialized value $stdout in concatenation (.) or string at w +lgw2.pl line 29. Use of uninitialized value $stderr in concatenation (.) or string at w +lgw2.pl line 30. 0
        As the login seems to be successful, it appears that the command is not returning any output, and is exiting "normally".

        My own foray into this kind of script just calls ssh through a backtick or qx//, like so:

        my @result = qx/$ssh ${user}\@${host} '$cmd'/;

        which isn't as easy to separate the various results. I also have pre-shared key login, so there's no password prompt. And that doesn't help you.

        I'll see if I can attract more attention to this.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of