Following the many examples out there, I came up with the following basic code which works 90% of the time. The only time I have a problem is with long-running processes. For example, restarting the syslog service on RHEL 5 doesn't complete before the script closes the channel and exits.
#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH2;
use Term::ReadKey;
my $host = '192.168.123.5';
my $user = 'root';
my $pass = getpass();
my $ssh = Net::SSH2->new();
$ssh->connect($host) or die $!;
$ssh->auth_password($user,$pass) or die $!;
my $chan = $ssh->channel();
$chan->shell();
print $chan "/sbin/service syslog restart";
print "$_" while <$chan>;
$chan->close;
$ssh->disconnect;
I tried setting "blocking" to "0" as suggested in several locations. This didn't work. I set "poll(10000)" and it appears to have ignored the timeout wait. I set debug on ssh and here is what I get (note no output):
$ ./ssh.pl
Password:
libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type
+, window_size, packet_size, ((void *)0) , 0 ) -> 0x1b10be0
Net::SSH2::poll: timeout = 10000, array[0]
Net::SSH2::poll: timeout = 250, array[1]
- [0] = channel
- [0] events 1
- libssh2_poll returned 0
- [0] revents 0
Net::SSH2::Channel::DESTROY
Net::SSH2::DESTROY object 0x1a055a0
Seeing as I will ultimately be using keys, might it be best if I just drop Net::SSH2 in favor of ssh via system?