in reply to Executing Linux command remotely from Windows client using SSH2
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Executing Linux command remotely from Windows client using SSH2
by Anonymous Monk on Feb 19, 2013 at 06:18 UTC | |
by salva (Canon) on Feb 19, 2013 at 08:43 UTC | |
by perldev (Initiate) on Feb 21, 2013 at 05:59 UTC | |
by Anonymous Monk on Aug 13, 2013 at 10:50 UTC |