mirror of
https://github.com/duncs/clusterssh.git
synced 2025-04-21 09:09:06 +00:00
host realname and givenname functionality
Check the hostname exists in ssh config or via lookup, but only when called/required (i.e. not done implicitly during new)
This commit is contained in:
parent
65a600e706
commit
9730b7f90f
2 changed files with 101 additions and 35 deletions
|
@ -28,28 +28,34 @@ sub new {
|
|||
my $self = $class->SUPER::new( ssh_config => "$ENV{HOME}/.ssh/config", %args );
|
||||
|
||||
# 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 $_ = <$ssh_config_fh> ) {
|
||||
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 $_ = <$ssh_config_fh> ) {
|
||||
chomp $_;
|
||||
next unless (m/^\s*host\s+(.*)/i);
|
||||
|
||||
# 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 );
|
||||
}
|
||||
else {
|
||||
$self->debug( 3, 'Unable to read ', $self->{ssh_config}, ': ', $!, $/ );
|
||||
}
|
||||
}
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub get_givenname {
|
||||
my ($self) = @_;
|
||||
return $self->{givenname};
|
||||
}
|
||||
|
||||
sub get_hostname {
|
||||
my ($self) = @_;
|
||||
return $self->{hostname};
|
||||
|
@ -77,8 +83,30 @@ sub set_port {
|
|||
return $self;
|
||||
}
|
||||
|
||||
sub get_realname {
|
||||
my ($self) = @_;
|
||||
|
||||
if ( !$self->{realname} ) {
|
||||
if ( $self->{type} && $self->{type} eq 'name' ) {
|
||||
if ( $ssh_hostname_for{ $self->{hostname} } ) {
|
||||
$self->{realname} = $self->{hostname};
|
||||
}
|
||||
else {
|
||||
my $gethost_obj = gethostbyname( $self->{hostname} );
|
||||
|
||||
$self->{realname} = defined($gethost_obj) ? $gethost_obj->name() : $self->{hostname};
|
||||
}
|
||||
}
|
||||
else {
|
||||
$self->{realname} = $self->{hostname};
|
||||
}
|
||||
}
|
||||
return $self->{realname};
|
||||
}
|
||||
|
||||
sub parse_host_string {
|
||||
my ( $self, $host_string ) = @_;
|
||||
my $parse_string = $host_string;
|
||||
|
||||
$self->debug( 5, $self->loc( 'host_string=" [_1] "', $host_string ), );
|
||||
|
||||
|
@ -94,10 +122,11 @@ sub parse_host_string {
|
|||
{
|
||||
$self->debug( 5, $self->loc( 'bracketed IPv6: u=[_1] h=[_2] p=[_3]', $1, $2, $3 ), );
|
||||
return __PACKAGE__->new(
|
||||
username => $1,
|
||||
hostname => $2,
|
||||
port => $3,
|
||||
|
||||
parse_string => $parse_string,
|
||||
username => $1,
|
||||
hostname => $2,
|
||||
port => $3,
|
||||
type => 'ipv6',
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -113,10 +142,11 @@ sub parse_host_string {
|
|||
{
|
||||
$self->debug( 5, $self->loc( 'std IPv4: u=[_1] h=[_2] p=[_3]', $1, $2, $3 ), );
|
||||
return __PACKAGE__->new(
|
||||
username => $1,
|
||||
hostname => $2,
|
||||
port => $3,
|
||||
|
||||
parse_string => $parse_string,
|
||||
username => $1,
|
||||
hostname => $2,
|
||||
port => $3,
|
||||
type => 'ipv4',
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -137,10 +167,11 @@ sub parse_host_string {
|
|||
if ( $colon_count == 7 || $host_string eq '::1' ) {
|
||||
$self->debug( 5, $self->loc( 'IPv6: u=[_1] h=[_2] p=[_3]', $username, $host_string, '' ), );
|
||||
return __PACKAGE__->new(
|
||||
username => $username,
|
||||
hostname => $host_string,
|
||||
port => undef,
|
||||
|
||||
parse_string => $parse_string,
|
||||
username => $username,
|
||||
hostname => $host_string,
|
||||
port => undef,
|
||||
type => 'ipv6',
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -148,16 +179,17 @@ sub parse_host_string {
|
|||
&& $colon_count < 8
|
||||
&& $host_string =~ m/:(\d+)$/xsm )
|
||||
{
|
||||
warn 'Ambiguous host string: "', $host_string, '"', $/;
|
||||
warn 'Assuming you meant "[', $host_string, ']"?', $/;
|
||||
warn 'Ambiguous host string: "', $host_string, '"', $/;
|
||||
warn 'Assuming you meant "[', $host_string, ']"?', $/;
|
||||
|
||||
$self->debug( 5, $self->loc( 'Ambiguous IPv6 u=[_1] h=[_2] p=[_3]', $username, $host_string, '' ) );
|
||||
|
||||
return __PACKAGE__->new(
|
||||
username => $username,
|
||||
hostname => $host_string,
|
||||
port => undef,
|
||||
|
||||
parse_string => $parse_string,
|
||||
username => $username,
|
||||
hostname => $host_string,
|
||||
port => undef,
|
||||
type => 'ipv6',
|
||||
);
|
||||
}
|
||||
else {
|
||||
|
@ -171,10 +203,11 @@ sub parse_host_string {
|
|||
$self->debug( 5, $self->loc( 'Default parse u=[_1] h=[_2] p=[_3]', $username, $hostname, $port ) );
|
||||
|
||||
return __PACKAGE__->new(
|
||||
username => $username,
|
||||
hostname => $hostname,
|
||||
port => $port,
|
||||
|
||||
parse_string => $parse_string,
|
||||
username => $username,
|
||||
hostname => $hostname,
|
||||
port => $port,
|
||||
type => 'name',
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -185,11 +218,12 @@ sub parse_host_string {
|
|||
sub check_ssh_hostname {
|
||||
my ( $self, ) = @_;
|
||||
|
||||
$self->debug(4, 'Checking ssh hosts for hostname ', $self->get_hostname);
|
||||
$self->debug( 4, 'Checking ssh hosts for hostname ', $self->get_hostname );
|
||||
|
||||
if($ssh_hostname_for{ $self->get_hostname } ) {
|
||||
if ( $ssh_hostname_for{ $self->get_hostname } ) {
|
||||
return 1;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
32
t/10host.t
32
t/10host.t
|
@ -20,6 +20,7 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'hostname set' );
|
||||
is( $host->get_port, undef, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host->set_port(2323);
|
||||
|
||||
|
@ -27,12 +28,14 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host->set_username('username');
|
||||
|
||||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is undef' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -44,6 +47,7 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'hostname set' );
|
||||
is( $host->get_port, undef, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host->set_port(2323);
|
||||
|
||||
|
@ -51,12 +55,14 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host->set_username('username');
|
||||
|
||||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is undef' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -71,12 +77,14 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'hostname set' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host->set_username('username');
|
||||
|
||||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is undef' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -91,12 +99,14 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'hostname set' );
|
||||
is( $host->get_port, undef, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host->set_port(2323);
|
||||
|
||||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -113,6 +123,7 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -125,6 +136,7 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, undef, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host = App::ClusterSSH::Host->parse_host_string('host%name');
|
||||
isa_ok( $host, "App::ClusterSSH::Host" );
|
||||
|
@ -132,6 +144,7 @@ is( $host, 'host%name', 'stringify works' );
|
|||
is( $host->get_hostname, 'host%name', 'checking set works' );
|
||||
is( $host->get_port, undef, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, 'host%name', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -142,6 +155,7 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host = App::ClusterSSH::Host->parse_host_string('host%name:2323');
|
||||
isa_ok( $host, "App::ClusterSSH::Host" );
|
||||
|
@ -149,6 +163,7 @@ is( $host, 'host%name', 'stringify works' );
|
|||
is( $host->get_hostname, 'host%name', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, 'host%name', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -159,6 +174,7 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host = App::ClusterSSH::Host->parse_host_string('username@host%name:2323');
|
||||
isa_ok( $host, "App::ClusterSSH::Host" );
|
||||
|
@ -166,6 +182,7 @@ is( $host, 'host%name', 'stringify works' );
|
|||
is( $host->get_hostname, 'host%name', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, 'host%name', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -176,6 +193,7 @@ is( $host, 'hostname', 'stringify works' );
|
|||
is( $host->get_hostname, 'hostname', 'checking set works' );
|
||||
is( $host->get_port, undef, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, 'hostname', 'realname set' );
|
||||
|
||||
$host = App::ClusterSSH::Host->parse_host_string('username@host%name');
|
||||
isa_ok( $host, "App::ClusterSSH::Host" );
|
||||
|
@ -183,6 +201,7 @@ is( $host, 'host%name', 'stringify works' );
|
|||
is( $host->get_hostname, 'host%name', 'checking set works' );
|
||||
is( $host->get_port, undef, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, 'host%name', 'realname set' );
|
||||
|
||||
diag('Parsing IPv4 IP address') if ( $ENV{TEST_VERBOSE} );
|
||||
|
||||
|
@ -192,6 +211,7 @@ is( $host, '127.0.0.1', 'stringify works' );
|
|||
is( $host->get_hostname, '127.0.0.1', 'checking set works' );
|
||||
is( $host->get_port, undef, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, '127.0.0.1', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -203,6 +223,7 @@ is( $host, '127.0.0.1', 'stringify works' );
|
|||
is( $host->get_hostname, '127.0.0.1', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, '127.0.0.1', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -213,6 +234,7 @@ is( $host, '127.0.0.1', 'stringify works' );
|
|||
is( $host->get_hostname, '127.0.0.1', 'checking set works' );
|
||||
is( $host->get_port, 2323, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, '127.0.0.1', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -224,6 +246,7 @@ is( $host, '127.0.0.1', 'stringify works' );
|
|||
is( $host->get_hostname, '127.0.0.1', 'checking set works' );
|
||||
is( $host->get_port, undef, 'checking set works' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, '127.0.0.1', 'realname set' );
|
||||
|
||||
diag('Checking IPv6 type addresses') if ( $ENV{TEST_VERBOSE} );
|
||||
|
||||
|
@ -236,6 +259,7 @@ is( $host, '::1', 'stringify works' );
|
|||
is( $host->get_hostname, '::1', 'checking set works' );
|
||||
is( $host->get_port, undef, 'port is undef' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, '::1', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -246,6 +270,7 @@ is( $host, '::1', 'stringify works' );
|
|||
is( $host->get_hostname, '::1', 'checking set works' );
|
||||
is( $host->get_port, undef, 'port is undef' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, '::1', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -256,6 +281,7 @@ is( $host, '::1', 'stringify works' );
|
|||
is( $host->get_hostname, '::1', 'checking set works' );
|
||||
is( $host->get_port, undef, 'port is undef' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, '::1', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -266,6 +292,7 @@ is( $host, '::1', 'stringify works' );
|
|||
is( $host->get_hostname, '::1', 'checking set works' );
|
||||
is( $host->get_port, undef, 'port is undef' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, '::1', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -276,6 +303,7 @@ is( $host, '::1', 'stringify works' );
|
|||
is( $host->get_hostname, '::1', 'checking set works' );
|
||||
is( $host->get_port, 22, 'checking port set' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, '::1', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -286,6 +314,7 @@ is( $host, '::1', 'stringify works' );
|
|||
is( $host->get_hostname, '::1', 'checking set works' );
|
||||
is( $host->get_port, 22, 'checking port set' );
|
||||
is( $host->get_username, 'username', 'username is set' );
|
||||
is( $host->get_realname, '::1', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -296,6 +325,7 @@ is( $host, '2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'stringify w
|
|||
is( $host->get_hostname, '2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'checking set works' );
|
||||
is( $host->get_port, undef, 'port is undef' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, '2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -316,6 +346,7 @@ like( $trap->stderr, qr/Assuming you meant "\[2001:0db8:85a3::8a2e:0370:7334\]"?
|
|||
is( $host->get_hostname, '2001:0db8:85a3::8a2e:0370:7334', 'checking set works' );
|
||||
is( $host->get_port, undef, 'port is undef' );
|
||||
is( $host->get_username, undef, 'username is undef' );
|
||||
is( $host->get_realname, '2001:0db8:85a3::8a2e:0370:7334', 'realname set' );
|
||||
|
||||
$host = undef;
|
||||
is( $host, undef, 'starting afresh' );
|
||||
|
@ -342,6 +373,7 @@ for my $hostname ( 'server1', 'server2', 'server3', 'server4', 'server-5', 'serv
|
|||
isa_ok( $host, "App::ClusterSSH::Host" );
|
||||
is( $host, $hostname, 'stringify works' );
|
||||
is( $host->check_ssh_hostname, 1, 'check_ssh_hostname ok for '. $hostname );
|
||||
is( $host->get_realname, $hostname, 'realname set' );
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue