if (defined($s2)) { $rcode = send2report($s2, $dbid, $delta); if ($rcode == 0) { addlog("error: no ssh2 connection, trying again..."); #sleep(5); $s2 = ssh2tunnel($dest, $port, $user, $pass); next if (! defined($s2)); addlog("info: ssh2 re-established"); $rcode = send2report($s2, $dbid, $delta); } } else { addlog("info: waiting for ssh2..."); #sleep(10); $s2 = ssh2tunnel($dest, $port, $user, $pass); addlog("info: ssh2 re-established") if (defined($s2)); } sub ssh2tunnel { my ($dest, $port, $user, $pass) = @_; my $ssh2 = Net::SSH2->new(); #$ssh2->poll(1000); #$ssh2->debug(1); if( $ssh2->connect($dest, $port) ) { if ($pass eq "") { # authentication using private key my $pubkey = "$pkey" . ".pub"; eval { $ssh2->auth_publickey($user, $pubkey, $pkey); 1 } or do { warn "error: ssh2 authkey failed $@ \n", $ssh2->error; }; } else { # authentication using private key eval { $ssh2->auth(username => "$user", password => "$pass"); 1 } or do { warn "error: ssh2 auth failed $@ \n", $ssh2->error; }; } } else { addlog("error: ssh2 connect failed $@"); return; } return $ssh2; } sub send2report { my ($s, $id, $payload) = @_; my $c = $s->channel(); if($c) { $c->exec("echo $payload | /opt/sdr/report/bin/hostadm -I $id:$host\n"); #$c->shell(); } else { return 0; } # print $c "echo $payload | /opt/sdr/report/bin/hostadm -I $id:$host\n"; my ( $code, $error_name, $error_string ) = $s->error(); if ( $code ) { addlog("error send2report($id): code:$code:$error_name: $error_string"); $c->close; return 0; } $c->close; return 1; }