Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Sep 2017 20:55:00 -0400
From:      Ernie Luzar <luzar722@gmail.com>
To:        "freebsd-questions@freebsd.org" <freebsd-questions@freebsd.org>
Subject:   Help scripting dns lookup using awk 
Message-ID:  <59BB24E4.6060908@gmail.com>

Next in thread | Raw E-Mail | Index | Archive | Help
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?



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?59BB24E4.6060908>