Awk script to run a command based on condition using 'if'

0

I have a piece of code here that basically takes a file called 'srcip' (which is just a list of IP addresses) and compares it to my '~/db' file which is just a tab delimited IP HOSTNAME database of servers we have in our environment. The objective is to take the 'srcip' file and match it with what's in the ~/db file to get the hostname. It works fine but there are a lot of IP addresses that are intentionally not in the ~/db file because of DHCP issues, so we need a real host lookup using the host command. What I'd like is to basically put an if statement in this awk code that will check if the IP does not match in the db, then run the host command on that ip and give me the output. How can I do that here?

awk 'FNR==NR {a[$1]=$2; next} $1 in a && a[$1]!=$2 {print $1, a[$1], $2}' OFS="\t" srcip ~/db

I am aware that awk can use if statements such as this below to check if any system users have a /bin/bash shell:

awk -F':' '{ if ($3 < '500' && $7 != "/bin/bash" ) print $0 }' /etc/passwd

As stated, I need to use something that parses this data fast such as awk or sed and not rely on any loops. Please keep in mind I can already do this with a while loop but given the nature of a while loop, it is parsing it line by line and slow for our use since we have nearly 100,000 IPs to parse at a time if not more. For 2000 lines, I can run this piece of code but again this is not what I want to do:

#!/bin/bash

while read this_IP_address
do
    db_host=$(grep "^$this_IP_address" ~/db | awk -F ' ' '{print $2}')
    if [ "$db_host" = "" ]
    then
        host $this_IP_address | head -n 1 | awk -F ' ' '{print $5}'
    else
    echo $db_host
    fi
done

unixpipe

Posted 2014-10-17T18:25:25.930

Reputation: 1

Answers

0

It sounds like you need something like this:

awk '
    NR == FNR {ip[$1]=1; next}
    $1 in ip {print; delete ip[$1]}
    END {
        for (i in ip) {
            system("host " i)
        }
    }
' srcip ~/db

glenn jackman

Posted 2014-10-17T18:25:25.930

Reputation: 18 546

It works but it must stay in order of the IP addresses. It cannot END and begin the for loop afterwards. Any ideas on how to re-structure this awk code? – unixpipe – 2014-10-17T20:21:50.010