Help scripting dns lookup using awk

Ernie Luzar luzar722 at gmail.com
Fri Sep 15 00:55:04 UTC 2017


The following sh script works, but runs very slow.

   host_in="$1"
   host_out="$2"
   host_error="$3"
   truncate -s 0 $host_out
   truncate -s 0 $host_error

# Make the input file read a line at a time, not a field at a time.
   IFS=$'\n'
   set -f

   for line in `cat $host_in`; do
     domain_name=`echo -n $line | cut -w -f 1`
     host $domain_name > /dev/null

     if [ $? -ne 0 ]; then
       echo "$domain_name" >> $host_error
    else
       echo "$domain_name" >> $host_out
     fi
  done



The follow script uses awk trying to do the same thing.

   host_in="$1"
   host_out="$2"
   host_error="$3"
   truncate -s 0 $host_out
   truncate -s 0 $host_error

   cat $host_in | awk '
     { system(host $1)
      rc_status = system($0)
      if (rc_status != 0)
         print $1 > $host_error
       else
         print $1 > $host_out
     }'


# command line exec command.
 >hosts2void-dns_lookup.awk /tmp/aw.hosts \
/root/good.dns /root/bad.dns

# This is the output.
sh: medrx.sensis.com.au: not found
sh: medrx.sensis.com.au: not found
awk: illegal field $(), name "host_error"
  input record number 1, file
  source line number 5


I see 2 problems with my awk code.

1. The text output of the host command results is going
    the console screen. In the sh version I kill the output
    using > /dev/null  How would I to do something like that in awk.

2. I get that doing  print $1 > $host_error  is not allowed.
    What is the correct way to pass script variables to awk?

Now I am wondering if there is a simpler way to do dns lookup
in awk?


More information about the freebsd-questions mailing list