How to get IP address of named host?



If I execute commands like "ftp" or "ping" using a hostname rather than
a raw IP address, then that hostname is searched for in the /etc/hosts
file and the DNS.

How do I do that sort of IP lookup for a hostname in a shell script?

"host" is no good because it won't look in /etc/hosts.

Currently, I'm using a script that looks up the hostname by pinging the
host, times out in 1 second, and then parses the output report to
extract the IP address. This is somewhat clumsy so I hope that someone
can suggest a better method than this:

function getaddr () {
hst=$(ping -c 1 -w 1 $1 | grep PING) # Extract line with IP addy
hst=${hst#*\(} # Delete up to opening bracket
hst=${hst%%\)*} # Delete after closing bracket
echo $hst
}

$ getaddr anvil # This name is in /etc/hosts
192.168.11.3

$ getaddr www.bbc.co.uk # This lookup uses the DNS
212.58.224.88

--
Dave Farrance
.



Relevant Pages

  • Passing forced command parameters with secondary key id...HELP!
    ... I'm interested in restricting a user's functionality upon connecting ... to a remote server to only running a shell script that takes ... Here's what my test shell script looks like on the server: ... one paramater to be passed (ip, date, hostname, etc). ...
    (comp.security.ssh)
  • Re: How to get IP address of named host?
    ... file and the DNS. ... How do I do that sort of IP lookup for a hostname in a shell script? ... getent hosts www.ukfsn.org ...
    (uk.comp.os.linux)
  • Re: Sudo for non-root user
    ... argument's sake) to execute my own shell script (located somewhere ... Install sudo ... Where <hostname> is the output of the 'hostname' command on the specific ...
    (comp.unix.solaris)
  • Re: How to get IP address of named host?
    ... How do I do that sort of IP lookup for a hostname in a shell script? ... getent hosts www.ukfsn.org ...
    (uk.comp.os.linux)
  • Re: Redirect IP to hostname
    ... (this is not called redirection). ... In your original post you stated you wanted to redirect an IP to a Hostname. ... This is referred to as reverse dns lookup, ...
    (microsoft.public.win2000.general)