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

Looking at the Net::OpenSSH documentation, it says "When a new Net::OpenSSH object is created, the OpenSSH ssh client is run in master mode, establishing a permanent (actually, for the lifetime of the object) connection to the server. Then, every time a new operation is requested a new ssh process is started in slave mode, effectively reusing the master SSH connection to send the request to the remote side."

However, consider the following code (simplified from my actual code to make the problem clearer):

use Net::OpenSSH; $Net::OpenSSH::debug = -1; my $ssh = Net::OpenSSH->new("host.domain", (user => "username", passwo +rd => "password", timeout=>30, master_opts => [-o => "UserKnownHostsF +ile=/dev/null"])); while(1) { my($stdout, $stderr) = $ssh->capture2({timeout => 30}, "sleep +5"); }

Looking at /var/adm/wtmp on host.domain, I see updates with my username every 5 seconds when running this script. Shouldn't it only be once every time I run the script???

The real-life problem I'm encountering is that my script runs about 100 commands on host.domain every day. With one entry added in /var/adm/wtmp for each command, this causes /var to fill up in a few weeks, leading to no one being able to log into host.domain remotely until /var/adm/wtmp is cleaned out, which doesn't make people very happy with me. I nor anyone I know has admin authority to this box (though one person, not me, does have enough authority to clear out /var/adm/wtmp). Is there any way for me to modify my script to prevent this from happening?

Additional info (output when in debug mode):

# ctl_path: /root/.libnet-openssh-perl/username-host.domain-19342-2284 +20, ctl_dir: /root/.libnet-openssh-perl/ # _is_secure_path(dir: /root/.libnet-openssh-perl, file mode: 16832, f +ile uid: 0, euid: 0 # _is_secure_path(dir: /root, file mode: 16832, file uid: 0, euid: 0 # _is_secure_path(dir: /root/.libnet-openssh-perl, file mode: 16832, f +ile uid: 0, euid: 0 # _is_secure_path(dir: /root, file mode: 16832, file uid: 0, euid: 0 # set_error(0 - 0) # call args: ['ssh','-o','UserKnownHostsFile=/dev/null','-xMN','-S','/ +root/.libnet-openssh-perl/username-host.domain-19342-228420','-o','Us +er=username','--','host.domain'] Warning: Permanently added 'host.domain,9.5.252.203' (RSA) to the list + of known hosts. # passwd requested (yes username@host.domain's password:) # call args: ['ssh','-O','check','-S','/root/.libnet-openssh-perl/user +name-host.domain-19342-228420','-o','User=username','--','host.domain +'] # open_ex: ['ssh','-O','check','-S','/root/.libnet-openssh-perl/userna +me-host.domain-19342-228420','-o','User=username','--','host.domain'] + # call args: ['ssh','-S','/root/.libnet-openssh-perl/username-host.dom +ain-19342-228420','-o','User=username','--','host.domain','sleep 5'] # open_ex: ['ssh','-S','/root/.libnet-openssh-perl/username-host.domai +n-19342-228420','-o','User=username','--','host.domain','sleep 5'] # call args: ['ssh','-S','/root/.libnet-openssh-perl/username-host.dom +ain-19342-228420','-o','User=username','--','host.domain','sleep 5'] # open_ex: ['ssh','-S','/root/.libnet-openssh-perl/username-host.domai +n-19342-228420','-o','User=username','--','host.domain','sleep 5'] # call args: ['ssh','-S','/root/.libnet-openssh-perl/username-host.dom +ain-19342-228420','-o','User=username','--','host.domain','sleep 5'] # open_ex: ['ssh','-S','/root/.libnet-openssh-perl/username-host.domai +n-19342-228420','-o','User=username','--','host.domain','sleep 5']
...

Replies are listed 'Best First'.
Re: Multiple connections with single Net::OpenSSH object
by salva (Canon) on Sep 29, 2009 at 19:10 UTC
    That issue is probably more related to the server side than to the client but you can try using another SSH client module (Net::SSH2 or Net::SSH::Perl) to see if it makes any difference.

    In any case, the common solution to this kind of problems is to set a proper log rotation police. On my Linux box, wtmp files are reduced to 5% of its original size!

    Which server software and version is running in the remote side? I would also ask the sys-admin to check that there isn't anything estrange on the sshd_config (or equivalent) configuration file.

      Sshd version on remote side is OpenSSH_5.0p1. As I stated earlier, I have no idea who the admin is, and in the odd chance that I'd find him through all the layers of bureaucracy at my business, I'm sure he'd not be willing to make any changes using the excuse that this change could hurt other users.

      I will try using other modules, but I vaguely remember back when originally writing this code that OpenSSH was the only module I could get working for some reason.

        Probably, the module that would solve your problem is Net::SSH::Expect. It launches one shell and then runs the commands talking to it, so there is only one login per connection.
Re: Multiple connections with single Net::OpenSSH object
by Illuminatus (Curate) on Sep 29, 2009 at 19:31 UTC
    Another option, assuming you can get somebody with root to help at least once, would be to set up everything via inetd or xinetd (and any firewall involved would allow a port). Then you would not have to login at all.