Running NSLOOKUP against some urls to print out url that is not equals to some IPS i have

71 Views Asked by At

I have some Web URLs I am trying to do a nslookup against. All it does is check against the URL and print the ones not equals to some certain IP Address into a file. I am able to do it for one IP Address but i tried adding one more, and I am unable to get it to work.

SUB='.com'
for address in `cat linux.hosts`; do
  if [[ "$address" == *"$SUB"* ]]; then
        echo "Got [$address]"
        nslookup $address \
        | awk '!/155.55.66.55/ || !/155.55.66.54/' >> com.txt
  fi
done

The linux.hosts file contains info like this

A B B { D E google.com }
A B B { D E twitter.com }
A B B { D E microsoft.com }
A B B { D E facebook.com }

I only want to get the string that has ".com" in it and do a nslookup that doesn't contain a certain IP Address.

The $nslookup address returns

Got [google.com]
Server:     BBBB:BBBB:BBBB:BBBB::1
Address:    BBBB:BBBB:BBBB:BBBB::1#53

Non-authoritative answer:
Name:   google.com
Address: 155.55.66.55
Name:   google.com
Address: 155.55.66.54

The | awk '!/155.55.66.55/ || ' >> com.txt works for some of the address that only contains 155.55.66.55 but if contains 155.55.66.54 it does not work, hence i am trying to add another check.

I only want to print the domains with address that doesn't contain 155.55.66.54 and 155.55.66.55.

The $nslookup address should only return

Got [twitter.com]
Server:     BBBB:BBBB:BBBB:BBBB::1
Address:    BBBB:BBBB:BBBB:BBBB::1#53

Non-authoritative answer:
Name:   google.com
Address: 198.168.101.1
Name:   google.com
Address: 198.168.101.2
1

There are 1 best solutions below

3
tripleee On

Don't read lines with for.

Also, probably redirect only once, after the loop, to avoid having the shell opening the file and seeking to the end repeatedly inside the loop.

As a minor optimization, use grep to find the matching lines in the file before the main loop.

grep -F '.com' linux.hosts |
while read -r _ _ _ _ _ _ address _; do
    nslookup "$address" |
    awk '!/155.55.66.55/ || !/155.55.66.54/'
done >>com.txt  # or maybe > com.txt if you want to overwrite