I need to run a dig on domains and these domains aliases, so the result will be:
IF domain (+ doesn't have an alias) and is using my Name Servers -> then print out
IF both domain and alias are using my Name Servers -> then print out
Everything else no (including when domain is using my Name Servers and alias don't). I have domains stored in MySQL and there's the script I am working on:

for domain in `echo "$QUERY1" | mysql -N -s -u $USER -p$PASS $DBNAME -h$HOST | awk '{print $1}'; do
    lookup=$(dig $domain ns +short)
    if [[ "$lookup" =~ 'XXX' ]]; then

In this QUERY I am listing everything (both domains and aliases). I can of course create another query to list only aliases or only domains. It would seem that I would need to group together all the aliases of the domain and loop over the domain name + aliases list. If either one not using NS, I would have to set variable that dictates printing the domain. Let me know, what are your ideas and please share the solution, if possible.


Consider the following alternative to the script you're working on:

mysql --silent --skip-column-names \
    --user="$USER" --password="$PASSWORD" \
    --host="$HOST" --database="$DBNAME" \
    --execute="$QUERY1" |
awk '{ printf("%s ns +short\n", $1) }' |
if dig -f - | grep -q "XXX"; then

I prefer using the long options in scripts since they are self-documenting.

The query in $QUERY1 can be crafted in such a way that the awk isn't necessary:

SELECT CONCAT(column, " ns +short") FROM ...

I've made dig read its queries from standard input out of the pipe coming from awk and the grep helps us finding XXX in the output.

No messy looping required.

Obviously, it doesn't do what you want to do at the end, only what you do at the moment, but since I don't know too much about DNS lookups I'll leave it at that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.