rastoboy has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Brothers!

I'm using Net::OpenSSH to connect to a series of machines and doing stuff. However, it's causing a lot of ssh connections to be used on the box I am sharing with other technicians.

Is there a way to explicitly (or implicitly!) kill a connection with that library? It doesn't mention it in the docs. I did grep around and find a private method for killing it in the perl module, but most of the source is frankly over my head to analyze when that gets called.

What am I missing? Any input would be greatly appreciated!

Update: By simply setting the scalar reference variable for the Net::OpenSSH object to '' it does seem to have done this. Just seems a little squirrely to me.

ugh but this leaves behind defunct ssh processes!

  • Comment on Explicitly kill a Net::OpenSSH connection?

Replies are listed 'Best First'.
Re: Explicitly kill a Net::OpenSSH connection?
by zentara (Cardinal) on Aug 02, 2011 at 20:03 UTC
    Hi, I never used it myself, but the docs for the module show you can get a $pid for the connection.
    # various ways of starting a connection returning a $pid my ($in, $out ,$pid) = $ssh->open2("foo"); my ($pty, $pid) = $ssh->open2pty("foo"); my ($in, $out, $err, $pid) = $ssh->open3("foo"); my ($pty, $err, $pid) = $ssh->open3pty("login");
    I would say you could either issue
    kill 9, $pid;
    #or in case some shells are hiding in there ( which may cause defunct process)
    use Proc::Killfam; killfam 9, $pid

    See Stopping subprocesses for a discussion of this. As a last resort, you can always search the process table for your process name, get the pid and kill it. Changing $0 of your script would make it easier to find in the process table, like

    $0 = 'rastoboy-ssh'; # :-)

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Nice. Thank you Zentara :-)
Re: Explicitly kill a Net::OpenSSH connection?
by salva (Canon) on Aug 02, 2011 at 20:33 UTC
    undef $ssh; That should not leave ssh zombies behind, if it does, it's a bug on the module.
      Gotcha Salva. I wonder--is there a difference between setting the variable to '', as opposed to using undef?

      I did try using the private method _kill_master and it seems to work...but I know it's marked private for a reason.

        _kill_master is called from the DESTROY method when the object goes out of scope, so there is usually no need to call it explicitly.

        In any case, I don't fully understand your message, did undef $ssh solve your problem at all?

        If the answer is no, activate the module debugging and post here the output:

        $Net::OpenSSH::debug = -1;