Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Net::SSH2 related query

by nithins (Sexton)
on Nov 19, 2013 at 12:30 UTC ( #1063318=perlquestion: print w/ replies, xml ) Need Help??
nithins has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks

i am using windows locally , i wanted to ssh to linux server & run some command and get the output back.For this i wrote a script but it doesn't seems to be working as i expected.It authenticates that i logged in to server but how to get the output back. i googled a lot but found no success

#!/usr/bin/perl use strict; use warnings; use Net::SSH2; use File::Remote; my $user = ''; my $pass =""; #my $cmd = "ls"; my %host; $host{'portaltest'} = ""; $host{'opatest'} = ""; eval{ foreach my $key (keys(%host)){ my $ssh = Net::SSH2->new(); $ssh->connect($host{$key}); $ssh->auth_password($user, $pass) or die "not able to login\n"; print "password authenticated\n" if ($ssh->auth_ok()); my $chan = $ssh->channel(); $chan->blocking(0); $chan->shell(); $chan->write("ls"); print "ls command executed\n"; select(undef,undef,undef,0.2); my $buf; print $buf while defined (my $len = $chan->read($buf,512)); # print "ls printing\n"; $chan->close(); } }; warn $@ if $@;

Comment on Net::SSH2 related query
Download Code
Re: Net::SSH2 related query
by lune (Monk) on Nov 19, 2013 at 12:48 UTC
Re: Net::SSH2 related query
by nithins (Sexton) on Nov 19, 2013 at 13:20 UTC

    Tried that($chan->write("ls\n");), but it didn't work

      Strange, just tried it for logging in from some ubuntu VM to another linux box which didn't work without newline and worked with the change. Could you add some additional error handling for all channel operations and show me the error ($!)?
Re: Net::SSH2 related query
by salva (Monsignor) on Nov 19, 2013 at 13:47 UTC
    Net::SSH::Any builds on top of Net::SSH2 and is quite easier to use:
    my $ssh = Net::SSH::Any->new($host, user => $user, password => $pass, +backends => 'Net::SSH2'); my $output = $ssh->capture("ls"); print "output:\n$output";
Re: Net::SSH2 related query
by keszler (Priest) on Nov 19, 2013 at 14:21 UTC
    ... print $chan "ls\n"; my @response = (readline $chan);

      I used your code , but the print statement didn't print results of "ls command". however when i run the script in debugg mode i found "@respose" having results of "ls"

        In print $chan "ls\n";, $chan is like a filehandle (open FH, '>', 'file.txt'; print FH "something\n";). The print command sends the characters over the SSH connection to the shell on the other end.
        Just tried again to reproduce your error. First, I am getting the same result (nothing read), if I set the timeout in the select-function too low, say
        #select(undef,undef,undef,0.2); select(undef,undef,undef,0.001);
        Maybe You should try a higher one?

        Second, it seems, that readline (or <>) is always able to read from the channel (no select necessary). The below code works for me. For you too?

        my $chan = $ssh->channel() or die "create channel:$!\n"; $chan->shell() or die("open shell: $!\n"); $chan->blocking(0); $chan->write("ls\n") or die("write to channel: $!\n"); while ( <$chan> + ) { print $_; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-07-31 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (252 votes), past polls