Script or tool to get the IP address of a remote host

4

1

I want to set an environment variable with the IP address of a given remote host. This is the solution I've arrived to:

export IP=`curl http://network-tools.com/default.asp?host=www.example.com | 
       grep -oE '<br>[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | 
       cut -c5-`

It works fine but I have a few questions:

  1. Is there is a simple tool to obtain the same result? I need it to work through a proxy that limits the host names, that's why I use the network-tools page.
  2. How can I make it a function of the host name? someting like getIP(hostname)

Esteban Crespi

Posted 2012-07-17T10:54:07.427

Reputation: 153

#3 is a very different question from the other two, and as it currently stands, extremely broad. I doubt that can even be answered in a good way as it is written now. – a CVn – 2012-07-17T12:51:47.943

Answers

5

Try something like

export IP=`( dig ${HOSTNAME} A +short | tail -n1; \
             dig ${HOSTNAME} AAAA +short | tail -n1 ) \
           | head -n1`

This will give you:

  • For a host with at least one IPv4 address, (one of) the IPv4 address
  • For a host with no IPv4 addresses but with at least one IPv6 address, (one of) the IPv6 address
  • For a host with no (IPv4 or IPv6) address at all in DNS, or if there is a resolver failure, an empty value

If you don't need IPv4 or IPv6 capability (your question seems to indicate you are mostly interested in the IPv4 address), just remove that dig command (A is IPv4, AAAA is IPv6). So, if you are only interested in IPv4 addresses:

export IP=`dig ${HOSTNAME} A +short | tail -n1`

The tail -n1 ensures that you get an IP address (in the case of CNAME, and possibly other record types, dig outputs the referenced canonical name on the first line). The head -n1 ensures that you only get a single address by returning only the first line of the remaining output (unnecessary in the case of only one address family, since tail will return only one address). Since DNS records are normally served in a round-robin fashion, there is no guarantee which exact address will be returned for multiple-address hosts.

I'm not too familiar with writing bash functions, but it should be relatively easy to turn this into a function.

a CVn

Posted 2012-07-17T10:54:07.427

Reputation: 26 553

Most stacks prefer IPv6, if available; consequently, you probably want tail -n 1 at the top level, as well, if you want to mirror the host's resolve order. Then again, this whole approach also doesn't respect NIS, or /etc/hosts, or nsswitch.conf, and yet I use it regularly nonetheless on machines where resolveip -s isn't necessarily available, so perhaps I'm being a bit too pedantic. ;) – BMDan – 2014-07-09T19:00:18.537

@BMDan The snippet in the question doesn't even support IPv6, which is (likely; it's been about two years now) why I went with this particular ordering. – a CVn – 2014-07-10T07:15:22.840

I'm not sure I understand your meaning. The snippet you provided explicitly queries both A and AAAA, so it's IPv6-aware. I agree insofar as your assertion that you share functionality with the original, but I think that the goal is to improve on what was in the question, not merely to parrot its shortcomings. – BMDan – 2014-07-28T16:23:46.823

2

Try the following:

  • host www.example.com
  • nslookup www.example.com
  • dig www.example.com

Depending on how much additional information you want, chose the appropriate candidate.

Izzy

Posted 2012-07-17T10:54:07.427

Reputation: 3 187

Thanks for your answer, I've tryed all the three but they doen't seem to work (in my context). For example host returns the message host www.example.com not found: 3(NXDOMAIN). I have also created the file etc/resolv.conf both with the ip of dns server obtained with the command ipconfig /all and with the ip of googles's public dns 8.8.8.8 without success. I fear the problem comes from some kind of limitation placed by my network's security. – Esteban Crespi – 2012-07-17T14:29:31.690

That looks like a problem with DNS resolution on your machine. The dig command may come in handy here, permitting to specify the nameserver to use, e.g. dig @ns.example.com www.example.com would use ns.example.com as name server (you could also specify an IP here). nslookup also permits this (please check with man nslookup for details). host would have been especially nice for you as it returns only the IP (without additional information to filter out) -- but it requires DNS resolution to work on the host directly. – Izzy – 2012-07-17T15:03:38.140