Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

In a script I want to be able to write an IP address to somewhere easily, so I thought using dig (or a similar command) with back-ticks.

However the simplest output I've been able to come up to wrt dig parameters is

> dig -t A +noall +answer     300 IN  A     300 IN  A

Any way (extra arg, different tool instead of dig?) to get rid of the junk apart from the IP address?? (And please don't tell me to use sed.)


share|improve this question

3 Answers 3

up vote 7 down vote accepted

On the CentOS box I have to hand

dig +short

If you only want one address then

dig +short | head -1


dig +short | tail -1
share|improve this answer

sed is a viable option and outright rejecting it is misguided at best.

Anyway, try

dig -t A +noall +answer | cut -f 6 | tail -1
share|improve this answer

For scripts (particularly if doing reverse-lookup in reports), unless you need to be testing specific DNS functionality, then you would be better to use getent hosts ... That way, if you're running a local DNS caching daemon (eg. nscd or a local caching DNS server such as dnsmasqd) then you get a performance and caching gain, and don't bombard the DNS server with a lot more traffic than necessary.

I've written a description with example in AWK. Hope it helps people avoid some of the pain I'm working with.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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