Take the 2-minute tour ×
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 www.google.com
www.google.com.     300 IN  A   173.194.66.106
www.google.com.     300 IN  A   173.194.66.104

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.)

Thanks

share|improve this question

3 Answers 3

up vote 7 down vote accepted

On the CentOS box I have to hand

dig +short   www.google.com
74.125.132.147
74.125.132.99
74.125.132.103
74.125.132.104
74.125.132.105
74.125.132.106

If you only want one address then

dig +short   www.google.com | head -1

or

dig +short   www.google.com | 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 www.google.com | 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

 
discard

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.