Make fqdn more resilient and more polite
This commit is contained in:
parent
ca9306318b
commit
3a0ee630ed
|
@ -22,7 +22,7 @@ cfg/%:
|
|||
|
||||
gethostbyname: gethostbyname.o hstrerror.o
|
||||
|
||||
fqdn: fqdn.o hstrerror.o
|
||||
fqdn: fqdn.pl
|
||||
|
||||
hstrerror.o: cfg/have_hstrerror.h
|
||||
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/perl
|
||||
use v5.26;
|
||||
|
||||
sub fqdn {
|
||||
my ($h) = @_;
|
||||
|
||||
if ($h =~ /\./) {
|
||||
return $h;
|
||||
}
|
||||
open(my $fh, '-|', '/usr/bin/host', $h);
|
||||
while (<$fh>) {
|
||||
if (/^(\S+\.\S+) has address/) {
|
||||
return $1;
|
||||
}
|
||||
}
|
||||
say STDERR "$0: cannot determine FQDN of $h";
|
||||
return "$h.invalid";
|
||||
}
|
||||
|
||||
if (!@ARGV) {
|
||||
$ARGV[0] = `/bin/hostname -f`;
|
||||
chomp($ARGV[0]);
|
||||
}
|
||||
|
||||
for my $h (@ARGV) {
|
||||
say fqdn($h);
|
||||
}
|
Loading…
Reference in New Issue