April 12th, 2021

BSD

Query a List of Hostnames to Create /etc/hosts File

for x in `cat hostnames`;
do echo $(nslookup $x | grep -e Address | sed -n 2p | sed -r 's/.{9}//') $x ;
done >> hostfile

Or, add SHORTNAME and ALIAS:

for x in `cat hostnames`;
do echo $(nslookup $x | grep -e Address | sed -n 2p | sed -r 's/.{9}//') $x $(echo $x | sed "s/\..*//") $(echo $x | sed "s/\..*//").alt.fqdn;
done >> hostfile