How to get IP address of named host?
- From: Dave Farrance <DaveFarrance@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 24 Feb 2006 15:09:39 GMT
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
.
- Follow-Ups:
- Re: How to get IP address of named host?
- From: Jason Clifford
- Re: How to get IP address of named host?
- From: Jim Howes
- Re: How to get IP address of named host?
- From: paul
- Re: How to get IP address of named host?
- Prev by Date: Re: Standard Troll BS
- Next by Date: Re: Grep
- Previous by thread: Hotplug strangeness
- Next by thread: Re: How to get IP address of named host?
- Index(es):
Relevant Pages
|
|