Make fqdn more resilient and more polite

This commit is contained in:
Peter J. Holzer 2022-08-11 15:46:31 +02:00
parent ca9306318b
commit 3a0ee630ed
2 changed files with 28 additions and 1 deletions

View File

@ -22,7 +22,7 @@ cfg/%:
gethostbyname: gethostbyname.o hstrerror.o gethostbyname: gethostbyname.o hstrerror.o
fqdn: fqdn.o hstrerror.o fqdn: fqdn.pl
hstrerror.o: cfg/have_hstrerror.h hstrerror.o: cfg/have_hstrerror.h

27
dns/fqdn.pl Executable file
View File

@ -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);
}