Hello. The problem is not in the connecting logic, but in the extraction of the users from the config file. To suggest solution to this we need to see your config extraction code.

As an exercise, I have assumed the config file is not extracted automatically from an ssh method.

Untested

#/usr/bin/perl -T use strict; use warnings; my( $index_of_host, %HOSTLIST ); OLOOP: while(<DATA>){ s/^\s*// , s/\s*$//; # eradicate outer whitespace next OLOOP until m/^Host\s+(\w+)$/; # untaint $1 $index_of_host = $1; ILOOP: while(defined $index_of_host && my $line = <DATA>){ $line =~ s/^\s*//; # eradicate inner outer whitespace $line =~ s/\s*$//; # assume an empty line between 'Host' lists in config file undef $index_of_host && last ILOOP unless $line; if( $line =~ m/^hostname\s+(\w+)$/ ){ # untaint $1; $hostname = $1 && next ILOOP; } if( $line =~ m/^User\s+(\w+)$/ ){ # untaint $1; push @{$HOSTLIST{$index_of_host}->{$hostname}}, $1; } } } __DATA__ #hosts Host host1 hostname host1.mydomain User user1 Host host2 hostname host2.mydomain User user1

Now your users are inside an anonymous array, keyed by the hostname, in an anonymous hash, keyed by the index of hosts. To retrieve:

foreach my $indhost( keys %HOSTLIST ){ foreach my $hname( keys %$indhost ){ foreach my $user ( @{ $hname } ){ #host print $hname #user print $user } } }

Using a similar approach, you can optimise down the config file and potentially remove the 'index_of_hosts' level from the %HOSTLISTS hash This would make the extraction logic simpler.

# optimised config file host1.mydomain user1 user2 #hostundef #user1 #user2 host2.mydomain user1 user2

Many Meek Monks Monikers Mention Meekness

In reply to Re: Net::SSH::Perl and getting User from config by Don Coyote
in thread Net::SSH::Perl and getting User from config by dayton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.