add support for .ssh/config include directive

This commit is contained in:
azenet 2017-03-14 13:43:12 +01:00
parent a3e8e221e7
commit f9c6120477

View file

@ -37,30 +37,44 @@ sub new {
# load in ssh hostname for later use
if ( !%ssh_hostname_for || !$ssh_configs_read{ $self->{ssh_config} } ) {
$ssh_configs_read{ $self->{ssh_config} } = 1;
if ( open( my $ssh_config_fh, '<', $self->{ssh_config} ) ) {
while ( my $line = <$ssh_config_fh> ) {
chomp $line;
next unless ( $line =~ m/^\s*host\s+(.*)/i );
read_ssh_file($self, $self->{ssh_config});
# account for multiple declarations of hosts
$ssh_hostname_for{$_} = 1 foreach ( split( /\s+/, $1 ) );
}
close($ssh_config_fh);
$self->debug( 5, 'Have the following ssh hostnames' );
$self->debug( 5, ' "', $_, '"' )
foreach ( sort keys %ssh_hostname_for );
}
else {
$self->debug( 3, 'Unable to read ',
$self->{ssh_config}, ': ', $!, $/ );
}
$self->debug( 5, 'Have the following ssh hostnames' );
$self->debug( 5, ' "', $_, '"' ) foreach ( sort keys %ssh_hostname_for );
}
return $self;
}
sub read_ssh_file($$) {
my ( $self ) = shift;
my ( $filename ) = glob(shift);
$self->debug(3, 'Reading SSH file: ', $filename);
$ssh_configs_read{ $filename } = 1;
if ( open( my $ssh_config_fh, '<', $filename ) ) {
while ( my $line = <$ssh_config_fh> ) {
chomp $line;
if ( $line =~ /^\s*include\s+(.+)/i ) {
$self->read_ssh_file($1);
next;
}
next unless ( $line =~ m/^\s*host\s+(.*)/i );
# account for multiple declarations of hosts
$ssh_hostname_for{$_} = 1 foreach ( split( /\s+/, $1 ) );
}
close($ssh_config_fh);
}
else {
$self->debug( 3, 'Unable to read ',
$filename, ': ', $!, $/ );
}
}
sub get_hostname {
my ($self) = @_;
return $self->{hostname};