Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Net::SSH::Perl and getting User from config

by Don Coyote (Hermit)
on May 02, 2015 at 08:21 UTC ( [id://1125432]=note: print w/replies, xml ) Need Help??


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

Many Meek Monks Monikers Mention Meekness

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
    Thanks all for the replies/pointers...

    The answer was much simpler... the "Host" section in my .ssh/config file had extra whitespace in the name so it didn't match... After I trimmed the whitespace, this is working as expected with no further edits to my script...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1125432]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found