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

Hello monks,

I'm considering to use Net::SSH::Any for a small script which will connect to several hosts with short-lived connections and I was looking for a way to explicit close the connections to the them.

For my surprise, didn't find any method for it in the documentation, neither found respective code after a quickly check.

Is there any method for it? Should I worry at all? I assume that the connection will be closed automatically when the object goes out of scope, but I prefer explicit than implicit behavior. :-)

Thanks,

Alceu Rodrigues de Freitas Junior
---------------------------------
"You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill
  • Comment on Explicit closing connections with Net::SSH::Any

Replies are listed 'Best First'.
Re: Explicit closing connections with Net::SSH::Any
by thanos1983 (Parson) on Jan 04, 2018 at 15:42 UTC

    Hello glasswalk3r,

    Why not use Net::OpenSSH or Net::OpenSSH::Parallel where you can set on the options close_slave_pty => 0 and then close the slave your self like: $slave = $pty->slave; close($slave).

    From Net::OpenSSH documentation:

    close_slave_pty => 0 When a pseudo pty is used for the stdin stream, the slave side is auto +matically closed on the parent process after forking the ssh command. This option disables that feature, so that the slave pty can be access +ed on the parent process as $pty->slave. It will have to be explicitl +y closed (see IO::Pty).

    The question is why you want to explicitly close the connection? As you said (I assume that the connection will be closed automatically when the object goes out of scope, but I prefer explicit than implicit behavior. :-)) well by reading this question Is it really required to explicitly close the SSH connection, channel or the telnet connection in Perl? the author of the module salva states:

    If you don't close channels/connections, they will be automatically cl +osed for you from the objects destructors when they go out of scope. +The advantage of doing it explicitly is that you can catch errors.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Explicit closing connections with Net::SSH::Any
by salva (Canon) on Jan 08, 2018 at 08:53 UTC
    Net::SSH::Any does not provide any kind of disconnect method. In order to close the connection you can, as you said, let the object go out of scope or undefine all the variables pointing to it.