Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Need Help on Net::SSH2 Module Usage on Windows

by tarunmudgal4u (Sexton)
on Aug 16, 2012 at 19:35 UTC ( #987862=perlquestion: print w/ replies, xml ) Need Help??
tarunmudgal4u has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I want to use Net::SSH2 module to establish SSH connection on remote Windows server. Then, once SSH connection is established, I want to fire some CMD system commands on this remote server and capture the output/status.

I've gone through CPAN Net::SSH2 documentation, but, it's not very much elaborative. Can anybody give me pointers where I can get full detail of all the methods available in this module? Also, if there is a sample code to begin with this task, please let me know.

Furthermore, while going through CPAN doc for Net::SSH2 there are some questions came into my mind- 1) when we call channel method, what does it do? 2)what does blocking method do? 3) What are the difference between exec and shell methods?

Please sort out these queries. It would help me to move further.

Comment on Need Help on Net::SSH2 Module Usage on Windows
Re: Need Help on Net::SSH2 Module Usage on Windows
by Khen1950fx (Canon) on Aug 17, 2012 at 02:57 UTC
    First, make sure that you're correctly set up. Net::SSH2 requires libssh2 for Windows, and it has a long list of dependencies:
    #!/usr/bin/perl use strict; use autodie; use warnings; use CPAN; CPAN::Shell->install( qw( IO::Scalar ExtUtils::Constant Encode ExtUtils::ParseXS Pod::Usage Getopt::Long Text::ParseWords Module::CoreList Module::Load Module::Load::Conditional Params::Check Locale::Maketext::Simple IPC::Cmd ExtUtils::CBuilder ExtUtils::Manifest Perl::OSType CPAN::Meta::Requirements CPAN::Meta::YAML JSON::PP Parse::CPAN::Meta CPAN::Meta ExtUtils::Install File::Temp version IO::File Module::Metadata Module::Build constant Text::Wrap Pod::Escapes Pod::Simple Pod::Man Test Exporter Carp File::Path Test::Harness Test::More Scalar::Util File::Spec ExtUtils::MakeMaker Net::SSH2));

    Net::SSH2::Channel goes into more detail about what the channel is and does. Read that thoroughly. It'll help.

    For more details about "exec", you can do perl -f exec for more info.

    See: libssh2.org for more info.

      Hi, I looked into Net::SSH2::Channel documentation and it's also just one liner description of all the methods used there.However, I've read about exec.

Re: Need Help on Net::SSH2 Module Usage on Windows
by zentara (Archbishop) on Aug 17, 2012 at 10:43 UTC
    if there is a sample code to begin with

    See A little demo for Net::SSH2.

    As far as blocking and channels go, it's not simple, read perldoc Net::SSH2::Channel

    If you want to exec something on the server, use this:

    #!/usr/bin/perl use warnings; use strict; use Net::SSH2; my $pass = 'hahahaha'; my $ssh2 = Net::SSH2->new(); $ssh2->connect('localhost') or die "Unable to connect Host $@ \n"; #plain password login #$ssh2->auth_password('z','ztester') or die "Unable to login $@ \n"; # works when run from z's homedir because you need # permission to read the keys $ssh2->auth_publickey('zentara', '/home/zentara/.ssh/id_dsa.pub', '/home/zentara/.ssh/id_dsa', $pass ); my $chan = $ssh2->channel(); $chan->blocking(1); $chan->exec("nohup /home/zentara/perlplay/net/zzsleep > foo.out 2> foo +.err < /dev/null &"); $chan->send_eof; exit;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Hi, I want to fire multiple system commands. Also, second system command would depend on the first system command output and so on. therefore, I guess, we cannot use exec here as exec overlays the parent process. We can use shell method then. If it is then can you please help me out to find out details about this shell method?

        See Salva's advice in channel SSH2. You probably are better off running a background process on the server itself. You exec with a nohup and detach the pid back into the background. You can log all output. Why do you need the logic of command processing done on the remote machine thru an unreliable network? Launch your control script right on the server, and come back later and get the log of it running.

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-07-26 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls