Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Executing Linux command remotely from Windows client using SSH2

by perldev (Initiate)
on Feb 18, 2013 at 08:17 UTC ( #1019293=perlquestion: print w/replies, xml ) Need Help??
perldev has asked for the wisdom of the Perl Monks concerning the following question:


I would like to develop a Perl script/module that can be used for executing commands remotely on a Linux box from Windows client. I need to connect to Linux box, authenticate and then execute several commands one after another and capture the output of each command. I want to use SSH2 for communication with Linux Box.

I know that it can be done using Net::SSH2, Perl sockets or Net::SSH::Perl etc. However I want to minimize the dependency on other modules (libssh2, server should present on Linux Box etc.).

Please note that the Perl script will be executed from the Windows client. Also I want to avoid deploying additional modules apart from the one available with ActiveState Perl distribution 5.14.

One option is to use utility plink I want to use this utility and execute several commands one after another with single connection.

Any help in this regards is very much appreciated.

  • Comment on Executing Linux command remotely from Windows client using SSH2

Replies are listed 'Best First'.
Re: Executing Linux command remotely from Windows client using SSH2
by salva (Abbot) on Feb 18, 2013 at 08:46 UTC
    In order to run several commands over one plink connection you would have to run a shell on the remote side, sending commands and processing the output, looking for prompts in order to discover when some command is done, etc... a quite effective way to waste your time!

    Net::SSH::Perl is and old and unmaintained module, with lots of bugs and almost impossible to install on Windows.

    Net::SSH2 would make your life slightly better. It allows to run several commands over the same connection, but its API is too low level, making its operation unnecessarily difficult. You should be able to find versions of Net::SSH2 prepackaged for Windows.

    Finally, I have been working for some time on Net::SSH::Any, a module that runs on top of Net::SSH2 or Net::OpenSSH providing a unified high level API. It is still a work in progress but simple operations as capturing the output of remote commands, already work flawlessly and it is far easier to use than the alternatives.

      Thank you salva for your valuable comments and guidance. I will surely look out for the alternative as mentioned by you.
Re: Executing Linux command remotely from Windows client using SSH2
by GrandFather (Sage) on Feb 19, 2013 at 01:33 UTC

    I use the following code in a script that executes several remote commands to deploy a system:

    $self->{ssh} = Net::SSH2->new(); $self->{ssh}->connect($self->{host}) or die <<ERR; Can't open an ssh connection to $self->{host}: $! ERR $self->{ssh}->auth_password($opts{user}, $opts{pass}) or die <<ERR +; Can't login using ssh to $self->{host}: $! ERR ... sub sshSendCommand { my ($self, $cmd, $pollTime) = @_; my $chan = $self->{ssh}->channel(); my $log = ''; my $timeDelta = sprintf "%4d:", time() - $self->{startTime}; $pollTime ||= 500; print "$timeDelta ssh: <$cmd> (poll time: $pollTime)\n"; $chan->exec($cmd); my @poll = {handle => $chan, events => ['in']}; while ($self->{ssh}->poll($pollTime, \@poll)) { if ($poll[0]{revents}{channel_closed}) { print "$timeDelta channel closed\n"; last; } next if !$poll[0]{revents}{in}; my $buff; $log .= $buff while $chan->read($buff, 1024); next; } print "$log\n$timeDelta ssh command complete\n"; return $log; }
    True laziness is hard work
      Thanks for the code snippet. However this one is making use of Net::SSH2. As mentioned above, I will be executing scripts from Windows client and want to avoid deploying additional Perl modules.
        So, what reply do you expect?

        You can write your own solution from scratch on top of plink which will cost you lots of effort or you can use any of the available modules from CPAN. It is your choice.

        Nobody is going to write the code for you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019293]
Approved by Old_Gray_Bear
NodeReaper polishes bones to Britney

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2018-03-22 14:38 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (276 votes). Check out past polls.