in reply to Need Help on Net::SSH2 Module Usage on Windows

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

Replies are listed 'Best First'.
Re^2: Need Help on Net::SSH2 Module Usage on Windows
by tarunmudgal4u (Sexton) on Aug 17, 2012 at 13:07 UTC

    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

        hi zentara.. thanks for your reply.. first of all, I want to make ssh from a windows machine to windows machine. So, here, I think, I can't run my commands in background. I tried one example using shell method you described but it was not working. It got stuck meanwhile executing it. Please see below snippet of code-

        my $ssh2 = Net::SSH2->new(); $ssh2->debug(1); if ($ssh2->connect($host)) { if ($ssh2->auth_password($user,$pass)) { #shell use print "connetced\n"; my $chan = $ssh2->channel() and print "channel created\n"; $chan->shell() and print "shell method called\n"; $chan->blocking(1) and print "blocking set ot 1\n"; $chan->write("dir\n") and print "dir fired\n"; select(undef,undef,undef,0.2) and print "select command fired\n" +; print $buf while ($len = $chan->read($buf,512)) > 0;
        and the output of this program is-
        C:\Compatibility_Automation_Temp>perl SSH2.pl connetced libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type +, window_size, packet_size, ((void *)0) , 0 ) -> 0x1c70aec channel created shell method called blocking set ot 1 dir fired Net::SSH2::Channel::read(size = 512, ext = 0) ##here, this program stuck
        Then I went through Net::SSH2::Channel module, but, there as well I didn't understand anything e.g.-
        sub shell { $_[0]->process('shell') }
        what is this subroutine doing? I didn't understand it as I didn't see any method named "process" specified there. Could you please give me an idea what I'm doing wrong and how can I move ahead with the better understanding of this module.