Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

SSH::W32Perl and STDOUT

by spanko (Initiate)
on Feb 21, 2007 at 14:33 UTC ( [id://601341]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I'm a new Monk here - nice to meet you.

I have a great script here that is based on Net::Telnet - it telnets to a list of hosts, gathers the required info and dumps it into a local log file. Nice.

But....we are changing our network to using only SSH and therefore I need to adapt my "wonder script". I'm running ActivePerl 5.8.7 build 813 and have started off by creating a simple script using net-ssh-w32perl to run on a Windows XP SP2 platform.
I want to be able to run my SSH script against a list of servers, run a command on the remote server, and allow me to see / grab / log the output.
I have researched everywhere and see no clear answer. People recommend changing to Expect... I don't really want to do this.
Is it possible to grab the STDOUT when using the Net::SSH:W32Perl module?
I suspect there is an issue with stdout for the following reason.

(1) I can run the following code without any problem: => as long as any stdout gets redirected (... > /tmp/foo.out...)

================================================ my($host, $username, $new_password, $old_password); $host = 'xxx.xxx.xx.xxx' $username = 'blah'; $password = 'secret'; use strict; use Net::SSH::W32Perl; ## Create a Net::SSH::Perl object and login to the remote host. my %args; $args{debug} = 1; $args{protocol} = 2; my $ssh = new Net::SSH::W32Perl($host, %args); $ssh->login($username, $password); my ($stdout, $stderr, $exit) = $ssh->cmd('ls > /tmp/foo.out',"\n"); =========================================================

(2) As soon as I change the last line to this (without the redirect), it hangs:

my ($stdout, $stderr, $exit) = $ssh->cmd('ls',"\n");

(3) This is the output I receive:

Reading configuration data C:\Documents and Settings\myname/.ssh/con Reading configuration data /etc/ssh_config Connecting to 9.0.6.141, port 22. Socket created, turning on blocking... Remote protocol version 2.0, remote software version OpenSSH_3.4p1 Net::SSH::Perl Version 1.27, protocol version 2.0. No compat match: OpenSSH_3.4p1. Connection established. Sent key-exchange init (KEXINIT), wait response. Algorithms, c->s: 3des-cbc hmac-sha1 none Algorithms, s->c: 3des-cbc hmac-sha1 none Entering Diffie-Hellman Group 1 key exchange. Sent DH public key, waiting for reply. Received host key, type 'ssh-dss'. Host 'x.xx.xxx.xxx' is known and matches the host key. Computing shared secret key. Verifying server signature. Waiting for NEWKEYS message. Enabling incoming encryption/MAC/compression. Send NEWKEYS, enable outgoing encryption/MAC/compression. Sending request for user-authentication service. Service accepted: ssh-userauth. Trying empty user-authentication request. Authentication methods that can continue: publickey,password. Next method to try is publickey. Trying pubkey authentication with key file 'C:\Documents and Settings Login completed, opening dummy shell channel. channel 0: new [client-session] Requesting channel_open for channel 0. channel 0: open confirm rwindow 0 rmax 32768 Got channel open confirmation, requesting shell. Requesting service shell on channel 0. channel 1: new [client-session] Requesting channel_open for channel 1. Entering interactive session. Sending command: ls Requesting service exec on channel 1. channel 1: send eof channel 1: open confirm rwindow 131071 rmax 32768 =======> Hangs Here.

When it runs OK (with the stdout redirect) I get this:

Sending command: ls Requesting service exec on channel 1. channel 1: send eof channel 1: open confirm rwindow 131071 rmax 32768 channel 1: rcvd eof channel 1: output open -> drain channel 1: obuf empty channel 1: output drain -> closed channel 1: close_write channel 1: send close input_channel_request: rtype exit-status reply 0 channel 1: rcvd close channel 1: full closed =======> script exits normally
Does anyone have any ideas on how I can grab the command output to a file no my local machine? Currently I am thinking I will have to run the script in its current format, outputting a bunch of commands to a file on the remote server, and then FTP the file back to my machine once completed.
Thanks

Replies are listed 'Best First'.
Re: SSH::W32Perl and STDOUT
by quester (Vicar) on Feb 22, 2007 at 20:45 UTC
    Oh, sorry, I forgot to mention: Expect.pm works in Perl under Cygwin http://cygwin.com/. If you can use native (meaning TCL-based rather than Perl-based) Expect, it is available either under Cygwin or as part of ActiveTCL http://www.activestate.com/products/ActiveTcl/.

    In theory, it ought to be possible to run Net::SSH::Perl under cygwin, but when I tried to install it from CPAN, test t/01-dh of the prerequisite Crypt-DH package hung.

    -----

    I used to use Windows without Cygwin, but I have repented. Life is too short to be a craftsman with an empty toolbag.

      ...when I tried to install it from CPAN, test t/01-dh of the prerequisite Crypt-DH package hung.

      yeah, when I had built Crypt::DH, I thought so too... so I aborted the test after a couple of minutes. But looking at the test source, I then figured it might just take a little longer. So, I reran the tests, and they did in fact complete. You have to be patient though :) The last group of tests (#16-18) use a somewhat larger prime (4096 bits). On my (pretty decent) machine, this took over half an hour:

      ~/.cpan/build/Crypt-DH-0.06 $ make test PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_h +arness(0, 'inc', 'blib/lib', 'blib/arch')" t/00-compile.t t/01-dh.t t/00-compile....ok + t/01-dh.........ok + All tests successful. Files=2, Tests=19, 2100 wallclock secs (2099.05 cusr + 0.70 csys = 20 +99.75 CPU) ^^^^ ~/.cpan/build/Crypt-DH-0.06 $ dmesg | grep Athlon AMD Athlon(tm) 64 X2 Dual Core Processor 4600+ stepping 01
        Hey Monks, I'm in the same position as the OP. Hangs on me unless i redirect to a file. Is there a work around for this? OP have you worked out how to sent the information back to the screen? Thanks.
Re: SSH::W32Perl and STDOUT
by quester (Vicar) on Feb 22, 2007 at 08:10 UTC
    I have two suggestions:

    1. This will work for SSH2, but if you are going to wind up supporting a mixture of Telnet, SSH1, and SSH2 clients, you will have a lot fewer headaches using Expect.pm from CPAN.

    2. I'm not on a Windows box, so I can't check this, but... in the command $ssh->cmd('ls',"\n"); the "\n" is an optional stdin parameter. In this case, it is fed into ls, which doesn't read its standard input.

    It may be that the Net::SSH::W32Perl problem described in its CPAN page "the shell() interface is not supported due to MSWin32's lack of support for select() on non-socket filehandles" also applies to the case where stdin and stdout are active at the same time.

    I would try removing the "\n" parameter and see if that helps. If you need to pass some input to other commands, you could use echo commands to create a file and then redirect stdin to the file.

      Thanks for taking the time to respond.

      Concerning point 1 - we will be using only SSH clients, so I hope this mixed environment scenario does not apply.

      I removed the "\n" but again, the script simply hangs. I think you could be right, that I am trying to implement a feature that is simply not supported on MSWin32. Excuse my ignorance, but does this mean I could run my script from a Solaris box for example, and then it *could* work...?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-25 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found