-1

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
tripleee
  • 175,061
  • 34
  • 275
  • 318
Abby
  • 67
  • 1
  • 11
  • Does your `cat` command break into lines properly? What is the output if you replace all the inner loop logic with just `echo $address`? – Aaron Meese Aug 18 '22 at 11:15
  • yes it does. It returns something like this Server: 1111:1111:1111:1111::1 Address: 1111:1111:1111:1111::1#53 Non-authoritative answer: Name: google.com Name: google.com Address: 155.55.66.55 – Abby Aug 18 '22 at 11:31
  • Sorry I meant with more than one line, what is the output then? Does it still work? – Aaron Meese Aug 18 '22 at 11:33
  • hello @EdMorton I only want to get the nslookup response that the address does not contain 155.55.66.55, 155.55.66.54 into the txt file. The nslookup will always return responses like this 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 Apologies if you aren't able to understand me still. I appreciate the help – Abby Aug 18 '22 at 12:24

1 Answers1

0

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
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Sorry about that, forgot to remove that line. – tripleee Aug 18 '22 at 11:23
  • Its unable to run. In the linux.hosts file, It contains line A B C { D google.com } A B C { D facebook.com } A B C { D twitter.com } hence why i am trying to use a substring on each line to get the one that contains ".com" – Abby Aug 18 '22 at 11:28
  • 1
    That's why we ask you to provide a [mre] – tripleee Aug 18 '22 at 12:47