简体   繁体   中英

Shell scripting support — multiple nslookups on different URLs

My company hosts 2 datacenters and the traffic is expected to be routed in a round robin fashion. We have a bunch of URLs and traffic is expected to be catered from both the DC. I check the if traffic is hitting both the DCs by doing a continuous nslookup

for i in {1..100}; do nslookup www.xyz.com ; done | grep 'Address:' | grep -v 10 | sort | uniq | wc -l

If the word count is 1, I know traffic is going only to one DC and that is an error however, if the output is 2, I know everything is working as expected.

Currently, I have a bunch of sites and i have them in a file. I wanted to write a script that will "cat" the file and do an nslookup against each of the entires, echo the entry and the word count variable along with it. Hoping the output to look like

www.xyz.com ==> 2 DCs active
www.123.com ==> 1 DC active

I couldn't think of a logic to attain this output. Request your support..

Disclaimer: I'm assuming there is no anycast involved here.

First it would be good to specify the DNS server you are asking.. using a wild nslookup may be giving you cached data..

so assuming they're all being served from the same DNS server (and these are NOT anycast) you can easily lookup the rules (clearly) using dig @DNSSERVER +short <query>

So first make a text file like this.. (first variable line is the domain, after the comma is the DNS server you want to look up against)

domains.txt :

google.com,4.2.2.3
sampledomain.org,8.8.4.4
joeblogs.net,

I've intentionally left joeblogs with no DNS server to lookup against, it will fall back to the default DNS server or cache on your workstation

now I've made a simple script that :

  • loops over the file, line by line
  • summarises the results of the DNS
  • starts all over again every 10 secs

dig.sh

#!/usr/bin/env bash
DEFAULT_DNS_SERVER=4.2.2.3
while true ; do
        while read line
        do
                domain="$(cut -d "," -f 1 <<<"$line")"
                server="$(cut -d "," -f 2 <<<"$line")"
                if [ "X$server" == "X" ]; then
                        export server="$DEFAULT_DNS_SERVER"
                fi
                result="$(dig +short @"$server" "$domain" | wc -l)"
                echo "$domain ==> ${result} DCs active"
        done < domains.txt
sleep 10
done

And run it.. EG:

stark@fourier ~ $ ./dig.sh 
google.com ==> 6 DCs active
sampledomain.org ==> 4 DCs active
joeblogs.net ==> 1 DCs active
google.com ==> 11 DCs active
sampledomain.org ==> 4 DCs active
joeblogs.net ==> 1 DCs active

....etc...

To install "dig" on a modern ubuntu based distro (I use Mint Cinnamon): sudo apt-get install dnsutils

Good luck

You can do something like this:

#!/bin/bash
readarray -t HOSTS < hosts_file
for HOST in "${HOSTS[@]}"; do
    COUNT=$(for i in {1..100}; do nslookup "$HOST"; done | awk '/Address/ && !/10/ && !a[$0]++ { ++count } END { print count }')
    echo "$HOST ==> $COUNT DCs active"
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM