how to extract just the IP address from a DNS query

I believe dig +short outputs two lines for you because the domain you query, smtp.mydomain.net is a CNAME for smtp.ggs.mydomain.net, and dig prints the intermediate resolution step.

You can probably rely on the last line from dig's output being the IP you want, though, and therefore the following should do:

dig +short smtp.mydomain.net | tail -n1

@dhag's answer sounds good; if you do not want to “rely on the last line from dig's output being the IP” you can use grep to extract just the numerical IP address:

dig +short smtp.mydomain.net | grep '^[.0-9]*$'

Tags:

Dns

Bash

Dig