Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 | 
       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)
share|improve this question
#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. – Michael Kjörling Jul 17 '12 at 12:51
You are right, thanks. I've edited it out. – Esteban Crespi Jul 17 '12 at 14:58

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.

share|improve this answer
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 Jul 9 '14 at 19:00
@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. – Michael Kjörling Jul 10 '14 at 7:15
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 Jul 28 '14 at 16:23

Try the following:

  • host
  • nslookup
  • dig

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

share|improve this answer
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 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 without success. I fear the problem comes from some kind of limitation placed by my network's security. – Esteban Crespi Jul 17 '12 at 14:29
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 would use 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 Jul 17 '12 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .