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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|