I realize this is an old post, but since there was not a solution here, I figured I would post mine.
I was having the same issue- Net::Appliance::Session (actually, Net::Appliance::Session::Transport::SSH) spawns a child proc for the ssh session, but even after I do $s->close() AND $s goes out of scope, I still see that child proc running when I do a ps. In my case, the ssh connection did not even succeed. Also, my program redirected SIG DIE to my own handler, which I thought might have been messing up Net::Appliance::Session's own signal handling (though this does not seem to be the case).
Anyway, it turns out that Net::Appliance::Session keeps track of its own children. You can use the method childpid() to get the child's pid. Then, you can check if it is still running and kill it yourself if need be. I have pasted a snippet of my own code below, which seems to work. Note, this only handles the case when the the connection/login fails- you would need some similar code elsewhere in your script to handle the lingering process if you log in successfully and close out the session later.
sub login_ssh {
my $ip = shift;
my $uname = shift;
my $pword = shift;
# I remapped the SIG handler for DIE elsewhere in my code.
# If the SSH connection fails, it will be caught by
# that handler, which I do not want. So remap it temporarily.
my $old_sigdie = $SIG{__DIE__};
$SIG{__DIE__} = sub {
mylog(MSG_NOTICE, "Caught SIG to DIE: $_[0]. Ignoring");
};
my $conn = Net::Appliance::Session->new(
Host => $ip,
Transport => 'SSH'
);
eval {
# eval {} probably not necessary in this particular case,
# since we intercept SIG DIE
$conn->connect(Name => $uname, Password => $pword);
};
# The ssh procs are sticking around even after we fail to log in.
# Get the child pid spawned by
# Net::Appliance::Session::Transport::SSH so we can kill it explic
+itly
my $sshpid = $conn->childpid();
if ($conn->logged_in()) {
# return an open Net::Appliance::Session session
return $conn;
} else {
if ($@) {
process_error($@);
}
$SIG{__DIE__} = $old_sigdie;
$conn->close();
if (kill(0, $sshpid) > 0) {
mylog("Child SSH proc $sshpid is still running. Killing i
+t");
my $rc = kill TERM => $sshpid;
}
if (kill(0, $sshpid) > 0) {
mylog("Child SSH proc $sshpid is still running. Kill -9'i
+ng it");
my $rc = kill 9 => $sshpid;
}
return;
}
}
|