From 3a0ee630ed9232063375e74ec791a51304a3a506 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Thu, 11 Aug 2022 15:46:31 +0200 Subject: [PATCH] Make fqdn more resilient and more polite --- dns/GNUmakefile | 2 +- dns/fqdn.pl | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100755 dns/fqdn.pl diff --git a/dns/GNUmakefile b/dns/GNUmakefile index 1ad5eb3..83650a7 100644 --- a/dns/GNUmakefile +++ b/dns/GNUmakefile @@ -22,7 +22,7 @@ cfg/%: gethostbyname: gethostbyname.o hstrerror.o -fqdn: fqdn.o hstrerror.o +fqdn: fqdn.pl hstrerror.o: cfg/have_hstrerror.h diff --git a/dns/fqdn.pl b/dns/fqdn.pl new file mode 100755 index 0000000..c4e4235 --- /dev/null +++ b/dns/fqdn.pl @@ -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); +}