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

I updated my perl version 5.8.8 I instlled Net::SSH2 through ppm install http://members.optusnet.com.au/~sisyphus1/ppms/Net-SSH2.ppd
Below is my code, but it is not giving any output.
could anyone help me on this....
use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect("$hostname") or die "Unable to connect Host $@ \n"; $ssh2->auth_password('ann134','pass') or die "Unable to login $@ \n"; my $chan2 = $ssh2->channel(); $chan2->shell(); print $chan2 "ls -lrt\n"; print "LINE : $_" while <$chan2>; $chan2->close;

Replies are listed 'Best First'.
Re: SSH2 for windows
by zentara (Cardinal) on Nov 27, 2006 at 13:41 UTC
    I don't have windows to try it, but it looks like it should work. Does non-shell use work?
    $chan2->exec('ls -lrt'); while (<$chan2>){ print }
    Maybe on windows, shell use requires "\r\n" instead of "\n", to signal an enter?

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: SSH2 for windows
by chargrill (Parson) on Nov 27, 2006 at 16:38 UTC

    This is probably a silly question, but have you installed libssh2?

    I can't imagine that your code wouldn't bail immediately if you hadn't, but I suppose it's a reasonable troubleshooting first step.

    If installing libssh2 proves difficult, you may wish to look into using Net::SSH::Perl, which IIRC (if I'm reading correctly) has no external dependencies.

    Alternatively, you could look into using Net::SSH, which acts as a wrapper around an ssh binary on your system, which would hopefully work (on windows) without issues given a C:\Progra~1\Putty\putty.exe as the command => $command init setup.



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
      This is probably a silly question, but have you installed libssh2?

      It's not a silly question ... but the ppm package the op installed was built against a static libssh2 library, so installation of libssh2 is not necessary :-)

      Cheers,
      Rob
        The code is completely working, I am able to connect and execute chain of commands in the remote host (through shell). But only thing is, I don't get any output (i.e. Not getting the return value after execution of the command).
Re: SSH2 for windows
by BrowserUk (Patriarch) on Nov 27, 2006 at 15:08 UTC

    Is the machine you are connectiong to a *nix machine or a windows machine?

    I ask, because I am wondering whether you are expecting ls -lrt to work on a windows shell?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: SSH2 for windows
by fenLisesi (Priest) on Nov 27, 2006 at 13:47 UTC
    The $pass is in single quotes, $hostname is in brackets, i don't get this.
Re: SSH2 for windows
by Anonymous Monk on Nov 28, 2006 at 12:15 UTC
    Turn on debugging.
Re: SSH2 for windows
by syphilis (Archbishop) on Dec 15, 2006 at 00:48 UTC
    Does the code you provided work for you on linux ? It doesn't for me. Seems to me that as soon as you '$chan->shell();' you lose the capability to get the output. There's simply nothing there to be read. To get the output, I find I have to '$chan->exec("command");'. Here's a demo that works for me on Windows:
    use Net::SSH2; use warnings; use strict; my $buflen = 100; my $buf = '0' x $buflen; my $ssh2 = Net::SSH2->new(); $ssh2->connect('192.168.0.3') or die; $ssh2->auth_password('username','password') or die "Unable to login $! \n"; my $chan1 = $ssh2->channel(); $chan1->blocking(1); $chan1->exec('echo $PATH'); $chan1->read($buf, $buflen); print "BUF: ", $buf, "\n"; $chan1->close; my $chan2 = $ssh2->channel(); $chan2->blocking(1); $chan2->exec('echo $HOME'); $chan2->read($buf, $buflen); print "BUF: ", $buf, "\n"; $chan2->close;
    It outputs:
    BUF: /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin BUF: /home/rob
    If someone can provide me with a *nix script that both uses the shell() method and captures the output, then I'll take a look at replicating that on Windows.

    Cheers,
    Rob