in reply to Net::SSH::Perl and getting User from config
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Net::SSH::Perl and getting User from config
by dayton (Acolyte) on May 06, 2015 at 15:16 UTC |