Search code examples
bashshellnslookup

Add Address from nslookup to file with domain from separate file


I have a .txt file with a list of domains. I need to get the IP Address of each domain's host, and output both to a file, on a single line per domain, if their host IP Address matches a set of IP addresses.

Example: in domains.txt, I have domain.tld. I want to do an nslookup on domain.tld, and in output.txt, show "domain.tld | $IPADDRESS" if $IPADDRESS is one of three IP Addresses in ipaddress.txt

I may very well be overthinking this, but I'm a bit new to bash, and trying to figure things out. Any help or pointers in the right direction would be appreciated.

Thanks


Solution

  • #!/bin/bash
    while read domain
    do
        ip=$(nslookup "$domain" | grep -m1 "^Address: " | cut -d' ' -f2)
        if grep -q "$ip" ipaddress.txt
        then
            echo "$domain | $ip" >> output.txt
        fi
    done < domains.txt