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

Hello everyone!

Greetings!

Recently, I am using Net::OpenSSH for my work. And I run to this problem...

I created an $ssh object and then fork a lot children processes which use this object.

When the children number is bigger than "MaxSessions" sshd configuration, some children process prompt user for confirming password when no more channels could be created by sshd.

I reviewed the src of Net::OpenSSH _connect method, and did found NumberOfPasswordPrompts parameter, but I don't know if this could affect the multiplexing session of ssh.

What could I do to avoid the password prompt from forked child or am I using this module wrong?

The following lines are my test code.

Any comments is appreciated, thanks for read along this line...

:)

#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; sub link_ssh { my $ssh = shift; $ssh->error and die "Couldn't establish SSH connection: " . $ssh->error . "\n"; my @lines = $ssh->capture(qq{ps}); print @lines; } sub main { my $host = "morya:morya\@10.137.73.214"; my $ssh = Net::OpenSSH->new( $host ); my @childs = (); # most hosts use MaxSessions=10 as default for ( 1 .. 12 ) { my $kid = 1; $kid = fork; push( @childs, $kid ) if ($kid); if ( !$kid ) { # child process eval { link_ssh $ssh, $_; }; if ($@) { print "child $_ ERR $@\n"; } exit; } } print "main $$ waiting childs\n"; waitpid( $_, 0 ) foreach @childs; return 0; } exit(main);

Replies are listed 'Best First'.
Re: fork more than MaxSessions times in Net::OpenSSH
by salva (Canon) on Sep 02, 2011 at 07:37 UTC
    It seems that once the MaxSessions limit is reached, the SSH client tries to establish a new connection.

    As a workaround, the following hack seems to work, just making the command fail:

    my $ssh = Net::OpenSSH->new( $host ); push @{$ssh->{_ssh_opts}}, '-oConnectionAttempts=0'; ...
    After the capture method call, in case of too many session, $ssh->error will be OSSH_SLAVE_CMD_FAILED and $? == 255.

    I will add some option to the constructor to allow for setting slave ssh arguments without fiddling with the object interiors.

    Anyway, instead of forking and unlimited number of processes, you could also use Parallel::ForkManager or Proc::Queue to get it under control.

    update: I have just released version 1.53_04 that allows to set default slave ssh options as follows:

    my $ssh = Net::OpenSSH->new($host, default_ssh_opts => [-o => 'ConnectionAtte +mpts=0'], ...);

      Never thought of getting rely from the author~</>

      Yeah~

      Thanks a lot for such a quick answer!

      Going to check out Parallel::ForkManager and Proc::Queue ~