How can I resolve a hostname to an IP address in a Bash script?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
332
down vote

favorite
84












What's the most concise way to resolve a hostname to an IP address in a Bash script? I'm using Arch Linux.










share|improve this question



















  • 14




    Shame that the getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> answer is somewhere down there near the bottom. It's the simplest, requires no extra packages and is easier to parse from a Bash script, too.
    – 0xC0000022L
    Apr 29 '15 at 22:15







  • 1




    @0xC0000022L: The new shame is that that answer suggests getent hosts somehost, when running this while on somehost will produce an IPv6 address, which is different from how most other tools (ping, ssh at least) resolve names, and breaks some things. Use the ahosts instead of hosts.
    – j_random_hacker
    Jun 29 at 12:46










  • @j_random_hacker: who keeps you from requesting specifically IPv4 (ahostsv4) or IPv6 (ahostsv6) addresses? Personally I find nothing wrong with the unspecific request returning IPv6. Your code should be prepared. IPv6 has been out there for more than 20 years now.
    – 0xC0000022L
    Jun 29 at 14:15










  • @0xC0000022L: Nobody "keeps me" from doing that, but the answer specifically suggests hosts, and so far 4 people have upvoted vinc17's comment expressing the pain caused by "suddenly IPv6". Being prepared for IPv6 is not always the issue: many programs need a way to determine whether two names/addresses refer to the same host. They can either use simple string matching, or they must know a lot about the network to find the "true" answer. The latter is a minefield, so many 3rd-party programs and systems -- that I have no control over -- use the former.
    – j_random_hacker
    Jun 29 at 15:08















up vote
332
down vote

favorite
84












What's the most concise way to resolve a hostname to an IP address in a Bash script? I'm using Arch Linux.










share|improve this question



















  • 14




    Shame that the getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> answer is somewhere down there near the bottom. It's the simplest, requires no extra packages and is easier to parse from a Bash script, too.
    – 0xC0000022L
    Apr 29 '15 at 22:15







  • 1




    @0xC0000022L: The new shame is that that answer suggests getent hosts somehost, when running this while on somehost will produce an IPv6 address, which is different from how most other tools (ping, ssh at least) resolve names, and breaks some things. Use the ahosts instead of hosts.
    – j_random_hacker
    Jun 29 at 12:46










  • @j_random_hacker: who keeps you from requesting specifically IPv4 (ahostsv4) or IPv6 (ahostsv6) addresses? Personally I find nothing wrong with the unspecific request returning IPv6. Your code should be prepared. IPv6 has been out there for more than 20 years now.
    – 0xC0000022L
    Jun 29 at 14:15










  • @0xC0000022L: Nobody "keeps me" from doing that, but the answer specifically suggests hosts, and so far 4 people have upvoted vinc17's comment expressing the pain caused by "suddenly IPv6". Being prepared for IPv6 is not always the issue: many programs need a way to determine whether two names/addresses refer to the same host. They can either use simple string matching, or they must know a lot about the network to find the "true" answer. The latter is a minefield, so many 3rd-party programs and systems -- that I have no control over -- use the former.
    – j_random_hacker
    Jun 29 at 15:08













up vote
332
down vote

favorite
84









up vote
332
down vote

favorite
84






84





What's the most concise way to resolve a hostname to an IP address in a Bash script? I'm using Arch Linux.










share|improve this question















What's the most concise way to resolve a hostname to an IP address in a Bash script? I'm using Arch Linux.







linux bash networking dns






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 13 '15 at 12:14









DhiaTN

1487




1487










asked Sep 15 '11 at 14:43









Eugene Yarmash

4,50682844




4,50682844







  • 14




    Shame that the getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> answer is somewhere down there near the bottom. It's the simplest, requires no extra packages and is easier to parse from a Bash script, too.
    – 0xC0000022L
    Apr 29 '15 at 22:15







  • 1




    @0xC0000022L: The new shame is that that answer suggests getent hosts somehost, when running this while on somehost will produce an IPv6 address, which is different from how most other tools (ping, ssh at least) resolve names, and breaks some things. Use the ahosts instead of hosts.
    – j_random_hacker
    Jun 29 at 12:46










  • @j_random_hacker: who keeps you from requesting specifically IPv4 (ahostsv4) or IPv6 (ahostsv6) addresses? Personally I find nothing wrong with the unspecific request returning IPv6. Your code should be prepared. IPv6 has been out there for more than 20 years now.
    – 0xC0000022L
    Jun 29 at 14:15










  • @0xC0000022L: Nobody "keeps me" from doing that, but the answer specifically suggests hosts, and so far 4 people have upvoted vinc17's comment expressing the pain caused by "suddenly IPv6". Being prepared for IPv6 is not always the issue: many programs need a way to determine whether two names/addresses refer to the same host. They can either use simple string matching, or they must know a lot about the network to find the "true" answer. The latter is a minefield, so many 3rd-party programs and systems -- that I have no control over -- use the former.
    – j_random_hacker
    Jun 29 at 15:08













  • 14




    Shame that the getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> answer is somewhere down there near the bottom. It's the simplest, requires no extra packages and is easier to parse from a Bash script, too.
    – 0xC0000022L
    Apr 29 '15 at 22:15







  • 1




    @0xC0000022L: The new shame is that that answer suggests getent hosts somehost, when running this while on somehost will produce an IPv6 address, which is different from how most other tools (ping, ssh at least) resolve names, and breaks some things. Use the ahosts instead of hosts.
    – j_random_hacker
    Jun 29 at 12:46










  • @j_random_hacker: who keeps you from requesting specifically IPv4 (ahostsv4) or IPv6 (ahostsv6) addresses? Personally I find nothing wrong with the unspecific request returning IPv6. Your code should be prepared. IPv6 has been out there for more than 20 years now.
    – 0xC0000022L
    Jun 29 at 14:15










  • @0xC0000022L: Nobody "keeps me" from doing that, but the answer specifically suggests hosts, and so far 4 people have upvoted vinc17's comment expressing the pain caused by "suddenly IPv6". Being prepared for IPv6 is not always the issue: many programs need a way to determine whether two names/addresses refer to the same host. They can either use simple string matching, or they must know a lot about the network to find the "true" answer. The latter is a minefield, so many 3rd-party programs and systems -- that I have no control over -- use the former.
    – j_random_hacker
    Jun 29 at 15:08








14




14




Shame that the getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> answer is somewhere down there near the bottom. It's the simplest, requires no extra packages and is easier to parse from a Bash script, too.
– 0xC0000022L
Apr 29 '15 at 22:15





Shame that the getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> answer is somewhere down there near the bottom. It's the simplest, requires no extra packages and is easier to parse from a Bash script, too.
– 0xC0000022L
Apr 29 '15 at 22:15





1




1




@0xC0000022L: The new shame is that that answer suggests getent hosts somehost, when running this while on somehost will produce an IPv6 address, which is different from how most other tools (ping, ssh at least) resolve names, and breaks some things. Use the ahosts instead of hosts.
– j_random_hacker
Jun 29 at 12:46




@0xC0000022L: The new shame is that that answer suggests getent hosts somehost, when running this while on somehost will produce an IPv6 address, which is different from how most other tools (ping, ssh at least) resolve names, and breaks some things. Use the ahosts instead of hosts.
– j_random_hacker
Jun 29 at 12:46












@j_random_hacker: who keeps you from requesting specifically IPv4 (ahostsv4) or IPv6 (ahostsv6) addresses? Personally I find nothing wrong with the unspecific request returning IPv6. Your code should be prepared. IPv6 has been out there for more than 20 years now.
– 0xC0000022L
Jun 29 at 14:15




@j_random_hacker: who keeps you from requesting specifically IPv4 (ahostsv4) or IPv6 (ahostsv6) addresses? Personally I find nothing wrong with the unspecific request returning IPv6. Your code should be prepared. IPv6 has been out there for more than 20 years now.
– 0xC0000022L
Jun 29 at 14:15












@0xC0000022L: Nobody "keeps me" from doing that, but the answer specifically suggests hosts, and so far 4 people have upvoted vinc17's comment expressing the pain caused by "suddenly IPv6". Being prepared for IPv6 is not always the issue: many programs need a way to determine whether two names/addresses refer to the same host. They can either use simple string matching, or they must know a lot about the network to find the "true" answer. The latter is a minefield, so many 3rd-party programs and systems -- that I have no control over -- use the former.
– j_random_hacker
Jun 29 at 15:08





@0xC0000022L: Nobody "keeps me" from doing that, but the answer specifically suggests hosts, and so far 4 people have upvoted vinc17's comment expressing the pain caused by "suddenly IPv6". Being prepared for IPv6 is not always the issue: many programs need a way to determine whether two names/addresses refer to the same host. They can either use simple string matching, or they must know a lot about the network to find the "true" answer. The latter is a minefield, so many 3rd-party programs and systems -- that I have no control over -- use the former.
– j_random_hacker
Jun 29 at 15:08











23 Answers
23






active

oldest

votes

















up vote
425
down vote



accepted










You can use getent, which comes with glibc (so you almost certainly have it on Linux). This resolves using gethostbyaddr/gethostbyname2, and so also will check /etc/hosts/NIS/etc:



getent hosts unix.stackexchange.com | awk ' print $1 '


Or, as Heinzi said below, you can use dig with the +short argument (queries DNS servers directly, does not look at /etc/hosts/NSS/etc) :



dig +short unix.stackexchange.com


If dig +short is unavailable, any one of the following should work. All of these query DNS directly and ignore other means of resolution:



host unix.stackexchange.com | awk '/has address/ print $4 '
nslookup unix.stackexchange.com | awk '/^Address: / print $2 '
dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ getline ; print $5 '


If you want to only print one IP, then add the exit command to awk's workflow.



dig +short unix.stackexchange.com | awk ' print ; exit '
getent hosts unix.stackexchange.com | awk ' print $1 ; exit '
host unix.stackexchange.com | awk '/has address/ print $4 ; exit '
nslookup unix.stackexchange.com | awk '/^Address: / print $2 ; exit '
dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ getline ; print $5 ; exit '





share|improve this answer


















  • 2




    By default, using dig only works with ipv4, where host gives both ipv4 and ipv6 answers. This might be unexpected. You can try host www.google.com, dig +short www.google.com, host ipv6.google.com, dig +short ipv6.google.com, host www.facebook.com, dig +short www.facebook.com.
    – jfg956
    Sep 21 '11 at 15:21






  • 4




    DIG does not work, if is a CNAME it will not return the IP.
    – sorin
    Mar 14 '14 at 14:23






  • 3




    Sometimes, host can be timed out and returns nothing. For some domains, dig +short may return domain alias in the first line. So, to ensure the output is an IPv4 address, use dig +short example.com | grep -Eo '[0-9.]7,15' | head -1.
    – caiguanhao
    Jun 7 '14 at 15:25






  • 5




    Using getent hosts <host> is incorrect, as for instance it may give an IPv6 address while IPv6 doesn't work. The correct solution is to use getent ahosts <host> to try both IPv6 and IPv4 if needed.
    – vinc17
    Oct 3 '16 at 15:02






  • 3




    Worth mentioning: host, dig and nslookup seems to directly talk to the servers listed in resolv.conf, whereas "getent hosts" respect both the local hosts file and library-level caching (such as nscd) if enabled.
    – Saustrup
    Jun 12 '17 at 11:59

















up vote
125
down vote













With host from the dnsutils package:



$ host unix.stackexchange.com
unix.stackexchange.com has address 64.34.119.12


(Corrected package name according to the comments. As a note other distributions have host is different packages: Ubuntu bind9-host, openSUSE bind-utils, Frugalware bind.)






share|improve this answer


















  • 4




    Did you mean dnsutils? Anyway, host worked nice, thanks
    – Eugene Yarmash
    Sep 15 '11 at 14:55











  • You're probably right. I have no Arch here to check. (Intended to append a comment later mentioning this, but the answer was already upvoted so I supposed I nailed it...)
    – manatwork
    Sep 15 '11 at 15:06






  • 1




    See the resolveip entry below if you need to resolve something not in DNS (e.g. /etc/hosts)
    – Gavin Brock
    Jul 2 '12 at 8:56






  • 2




    Be aware that host sometimes returns multi-line output (in the case of redirects), you'll want host unix.stackexchange.com | tail -n1 if you just want the line with the IP address.
    – Edward Coffey
    Jan 23 '13 at 5:04






  • 3




    This answer deserves a serious downvote. host is a DNS tool (similar to nslookup) so it only looks up hosts in DNS, not in e.g. /etc/hosts. So it is NOT an answer to OP's question.
    – peterh
    Sep 11 '14 at 9:49

















up vote
47
down vote













I have a tool on my machine that seems to do the job. The man page shows it seems to come with mysql... Here is how you could use it:



resolveip -s unix.stackexchange.com
64.34.119.12


The return value of this tool is different from 0 if the hostname cannot be resolved :



resolveip -s unix.stackexchange.coma
resolveip: Unable to find hostid for 'unix.stackexchange.coma': host not found
exit 2


UPDATE
On fedora, it comes with mysql-server :



yum provides "*/resolveip"
mysql-server-5.5.10-2.fc15.x86_64 : The MySQL server and related files
Dépôt  : fedora
Correspondance depuis :
Nom de fichier  : /usr/bin/resolveip


I guess it would create a strange dependency for your script...






share|improve this answer
















  • 5




    This seems to be the only solution on here that uses the OS's build in resolver - so works for /etc/hosts as well as DNS.
    – Gavin Brock
    Jul 2 '12 at 8:57







  • 7




    getent, as detailed in the other answer, also looks at /etc/hosts, and comes with glibc, so has no dependencies on a Linux system.
    – Asfand Qazi
    Apr 16 '16 at 13:32

















up vote
38
down vote













The following command using dig allows you to read the result directly without any sed/awk/etc. magic:



$ dig +short unix.stackexchange.com
64.34.119.12


dig is also included in the dnsutils package.




Note: dig has a return value of 0, even if the name could not be resolved. Thus, you'd need to check if the output is empty instead of checking the return value:



hostname=unix.stackexchange.com

ip=`dig +short $hostname`

if [ -n "$ip" ]; then
echo IP: $ip
else
echo Could not resolve hostname.
fi



Note 2: If a hostname has multiple IP addresses (try debian.org, for example), all of them will be returned. This "problem" affects all of the tools mentioned in this question so far:






share|improve this answer






















  • Note that if a domain has a CNAME entry its domain may be printed in the first line instead of an IP address.
    – pcworld
    Apr 27 at 23:27

















up vote
32
down vote













getent hosts unix.stackexchange.com | cut -d' ' -f1





share|improve this answer
















  • 4




    Also consider ahosts, ahostsv4, ahostsv6 with getent.
    – 0xC0000022L
    Apr 29 '15 at 22:12










  • cut will not for getent's, which use t to separate columns. This is the case on Solaris.
    – ceving
    Mar 18 '16 at 9:51






  • 1




    @ceving: On Solaris you might have to run cut without -d (defaults to t as delimiter ). On Linux it's spaces, thus the line above works.
    – sborsky
    Apr 7 '16 at 22:24

















up vote
25
down vote













The solutions given so far mostly work in the simpler case: the hostname directly resolves to a single IPv4 address. This might be the only case where you need to resolve hostnames, but if not, below is a discussion on some cases that you might need to handle.



Chris Down and Heinzi briefly discussed the case where the hostname resolves to more than one IP addresses. In this case (and others below), basic scripting under the assumption that a hostname directly resolves to a single IP address may break. Below, an example with a hostname resolving to more than a single IP address:



$ host www.l.google.com
www.l.google.com has address 209.85.148.147
www.l.google.com has address 209.85.148.103
www.l.google.com has address 209.85.148.99
www.l.google.com has address 209.85.148.106
www.l.google.com has address 209.85.148.105
www.l.google.com has address 209.85.148.104


But what is www.l.google.com? This is where the alias case needs to be introduced. Let's check the example below:



$ host www.google.com
www.google.com is an alias for www.l.google.com.
www.l.google.com has address 74.125.39.103
www.l.google.com has address 74.125.39.147
www.l.google.com has address 74.125.39.105
www.l.google.com has address 74.125.39.99
www.l.google.com has address 74.125.39.106
www.l.google.com has address 74.125.39.104


So www.google.com does not directly resolve to IP addresses, but to an alias that itself resolves to multiple IP addresses. For more information on aliases, check here. Of course, the case where an alias has a single IP address is possible, as shown below:



$ host g.www.ms.akadns.net
g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
lb1.www.ms.akadns.net has address 207.46.19.190


But can aliases be chained? The answer is yes:



$ host www.microsoft.com
www.microsoft.com is an alias for toggle.www.ms.akadns.net.
toggle.www.ms.akadns.net is an alias for g.www.ms.akadns.net.
g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
lb1.www.ms.akadns.net has address 207.46.19.254

$ host www.google.fr
www.google.fr is an alias for www.google.com.
www.google.com is an alias for www.l.google.com.
www.l.google.com has address 74.125.39.147
www.l.google.com has address 74.125.39.103
www.l.google.com has address 74.125.39.99
www.l.google.com has address 74.125.39.106
www.l.google.com has address 74.125.39.104
www.l.google.com has address 74.125.39.105


I did not find any example where a hostname resolves to an alias that does not resolve to an IP address, but I think the case might occur.



More than multiple IP addresses and aliases, is there some other special cases... what about IPv6? You could try:



$ host ipv6.google.com
ipv6.google.com is an alias for ipv6.l.google.com.
ipv6.l.google.com has IPv6 address 2a00:1450:8007::68


Where the hostname ipv6.google.com is an IPv6-only hostname. What about dual-stack hostnames:



$ host www.facebook.com
www.facebook.com has address 66.220.153.15
www.facebook.com has IPv6 address 2620:0:1c08:4000:face:b00c::


Again about IPv6, if your host is IPv4 only, you can still resolve IPv6 addresses (tested on a IPv4 only WinXP and with ipv6.google.com, you could try it on Linux). In this case, the resolution succeeds, but a ping fails with an unknown host error message. This might be a case where your scripting fails.



I hope those remarks were useful.






share|improve this answer


















  • 2




    What a great complement to the accepted answer, showing all the edge cases that one might want to deal with in scripting. My version host does not even state "has address" for my boxes.
    – Mihai Danila
    Feb 15 '13 at 23:23

















up vote
17
down vote













To avoid the problem with aliases and always get a single IP address ready for use:



python -c 'import socket; print socket.gethostbyname("www.example.com")'





share|improve this answer




















  • I made a bash function to stick in my bashrc from this answer: gist.github.com/brycepg/ba117a37de53906dc8fcc312bd7d5fee
    – Bryce Guinta
    Aug 11 '17 at 16:58


















up vote
15
down vote













ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/).*//" | sed -e "s/.*(//"


works without dependencies on other systems (and for hosts specified in /etc/hosts)






share|improve this answer


















  • 2




    The use of ping is what I needed as I need the value from the hosts file but the sed pattern parsing correctly but this worked ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/^[^(]*[(]//" | sed -e "s/[)].*$//"
    – ManiacZX
    Jan 18 '13 at 19:23







  • 1




    To resolve something on my home network like myhostname.local this works so for me this is the best answer.
    – Matt Friedman
    Jan 24 '15 at 2:59






  • 1




    May I suggest this also: ping -q -c 1 -t 1 bahface.local | grep -m 1 PING | cut -d "(" -f2 | cut -d ")" -f1
    – Matt Friedman
    Jan 24 '15 at 3:08










  • getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> works for declarations inside /etc/hosts, too ... and it's the go-to-tool for all kinds of system databases (passwd, group, aliases, services).
    – 0xC0000022L
    Apr 30 '15 at 7:53

















up vote
14
down vote













Simple but usefull:



  1. getent ahostsv4 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

  2. getent ahostsv6 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

  3. getent hosts google.de | head -n 1 | cut -d ' ' -f 1

All commands will resolve an IP address if host still exist. If host points to CNAME it will also get the IP in that case.



The first command returns the resolved IPv4 address



The second command returns the resolved IPv6 address



The third command will return the owners preferred address what may IPv4 or IPv6 address.






share|improve this answer




















  • By far the most simple one. And its avilable by default. Not like host that require install of the bindutils
    – michaelbn
    Oct 21 '15 at 6:45


















up vote
5
down vote













Here is a slight variation of the ping approach that takes "unknown host" into account (by piping through stderr) and uses tr to avoid the use of sed regexps:



ping -c1 -t1 -W0 www.example.com 2>&1 | tr -d '():' | awk '/^PING/print $3'


In case it's important to capture the exit value, then the following will work (although less elegant):



ping -c1 -t1 -W0 www.example.com &>/dev/null && ping -c1 -t1 -W0 www.example.com 2>&1 | tr -d '():' | awk '/^PING/print $3'





share|improve this answer




















  • I love this solution, because it works without any extra tools.
    – Radon8472
    Aug 9 at 8:14

















up vote
4
down vote













You could use host:



hostname=example.org

# strips the IP
IP=$( host $hostname | sed -e "s/.* //" )

# checks for errors
if [ $? -ne 0 ] ; then
echo "Error: cannot resolve $hostname" 1>&2
exit 1;
fi





share|improve this answer





























    up vote
    4
    down vote













    To complete Chris Down's answer, and address jfgagne comments about (possibly chained) aliases, here is a solution that :



    • takes into account multiple IPs

    • takes into account one or more aliases (CNAME)

    • does not query /etc/hosts file (in my case I didn't want it); to query it, dbernt's python solution is perfect)


    • does not use awk/sed



      dig +short www.alias.com | grep -v ".$" | head -n 1


    Always returns the first IP address, or empty tring if not resolved. with version of dig :



     $ dig -v
    DiG 9.8.1-P1





    share|improve this answer




















    • Thanks, other answers assume "dig +short" always returns a single ip address. They weren't accounting for CNAMEs.
      – jamshid
      May 10 '16 at 0:46

















    up vote
    3
    down vote













     php -r "echo gethostbyname('unix.stackexchange.com');"





    share|improve this answer




















    • this works, but it requires that php is installed on your terminal
      – Radon8472
      Aug 9 at 8:29






    • 1




      can be useful on a typical php docker container where "host", "dig" etc. are not available
      – Fabian Schmengler
      Sep 5 at 19:42

















    up vote
    3
    down vote













    nmap -sP 192.168.178.0/24|grep YOUR_HOSTNAME|sed -n 's/.*[(]([0-9.]*)[)].*/1/p'


    was the solution I found without DNS server






    share|improve this answer



























      up vote
      3
      down vote













      I would have liked to add this as a comment to Andrew McGregor Re: ping. However it wouldn't let me, so I need to add this as another answer. (If somebody can move it into a comment, feel free to.)



      This is another variant, only using ping and grep:



      ping -q -c1 -t1 your_host_here | grep -Eo "([0-9]+.?)4"


      grep -E for extended regular expression and
      grep -o to return only the matching part.
      the regexp itself looks for one or multiple digits ([0-9]+) and optionally a dot (.?) four times (4)






      share|improve this answer



























        up vote
        2
        down vote













        here's a Bash recipe I cooked up using other folk's answers — first tries /etc/hosts, then falls back to nslookup:



        resolveip() awk 'print $1' )
        if [ -z "$ip" ]
        then
        ip=$( dig +short "$host" )
        if [ -z "$ip" ]
        then
        echo "unable to resolve '$host'" >&2
        return 1
        else
        echo "$ip"
        return 0
        fi
        else
        echo "$ip"
        return 0
        fi
        fi






        share|improve this answer






















        • To be clear, getent hosts isn't just a lookup in /etc/hosts - it's a full-on DNS-resolving call to gethostbyaddr(3), and it's very unlikely to fail in a case where dig will succeed. See the man page for getent.
          – Stuart P. Bentley
          Aug 3 '14 at 11:42










        • @Stuart is right — i've learned a great deal since writing that and oversimplified a powerful command. getent remains my favorite, although i also like dig +short
          – RubyTuesdayDONO
          Aug 3 '14 at 18:26

















        up vote
        2
        down vote













        Maybe not the most concise, but it seems to be robust and efficient:



        # $(get_host_dns_short "google.com")
        #
        # Outputs the IPv4 IP Address of a hostname, resolved by DNS. Returns 0 if DNS
        # responded successfully; 1 otherwise. Will mask error output.
        function get_host_dns_short()
        return 1



        This will output a single IPv4 IP, as well as return 1 in the event of failure, while masking stderr output.



        You can use it like this:



        GOOGLE_IP="$(get_host_dns_short "google.com")"
        if [[ $? -eq 0 ]]; then
        echo "Google's IP is $GOOGLE_IP."
        else
        echo "Failed to resolve Google's IP."
        fi



        Google's IP is 216.58.192.46.




        If you want an IPv6 address instead, just replace -4 with -6.






        share|improve this answer



























          up vote
          2
          down vote













          dig +noall +answer +nocomments example.com | awk 'printf "%-36st%sn", $1, $5 '






          share|improve this answer


















          • 1




            Some context in what way that answer improves over the already existing ones would be great. Also, please indent commands by 4 spaces (cf. the markdown syntax).
            – maxschlepzig
            Mar 17 '16 at 21:14

















          up vote
          1
          down vote













          1 line resolve a list of hostname



          for LINE in `cat ~/Desktop/mylist`; do a=$(nslookup $LINE | awk '/^Address: / print $1 '); echo $a >> ~/Desktop/ip; done





          share|improve this answer



























            up vote
            1
            down vote













            I am doing this all the time on my Mac which does not have getent. ping seems like a hack. I would like to take /etc/hosts into account as well.



            So, I wrote a stupid wrapper for dns.lookup for you who have Node.js installed to provide a CLI:



            $ npm install -g lookup-hostname
            $ lookup google.com
            62.243.192.89





            share|improve this answer



























              up vote
              1
              down vote













              dig is too slow, nslookup is much faster



              nslookup google.com | grep -Po 'Address:s*[0-9.]+' | tail -1 | sed -e 's/Address:s*//g'





              share|improve this answer



























                up vote
                1
                down vote













                I don't know the easiest way for a bash-script but if you want to resolve a hostname and see if the host is up, use ping!



                ping -a hostname -c 1


                Will ping the host one time and resolve the hostname to IP-address.



                $ ping -a www.google.com -c 1
                PING www.google.com (216.58.211.132) 56(84) bytes of data.
                64 bytes from arn09s10-in-f4.1e100.net (216.58.211.132): icmp_seq=1 ttl=54 time=1.51 ms





                share|improve this answer






















                • using ping is good, because everybody has it, but you need to to filter the IP-Part from the outputs, if you like to use it in a script.
                  – Radon8472
                  Aug 9 at 8:27

















                up vote
                -1
                down vote













                host -t a cisco.com


                this command will show ip address ( will reslove domain to IP )






                share|improve this answer






















                  Your Answer







                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "106"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: false,
                  noModals: false,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f20784%2fhow-can-i-resolve-a-hostname-to-an-ip-address-in-a-bash-script%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  23 Answers
                  23






                  active

                  oldest

                  votes








                  23 Answers
                  23






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  425
                  down vote



                  accepted










                  You can use getent, which comes with glibc (so you almost certainly have it on Linux). This resolves using gethostbyaddr/gethostbyname2, and so also will check /etc/hosts/NIS/etc:



                  getent hosts unix.stackexchange.com | awk ' print $1 '


                  Or, as Heinzi said below, you can use dig with the +short argument (queries DNS servers directly, does not look at /etc/hosts/NSS/etc) :



                  dig +short unix.stackexchange.com


                  If dig +short is unavailable, any one of the following should work. All of these query DNS directly and ignore other means of resolution:



                  host unix.stackexchange.com | awk '/has address/ print $4 '
                  nslookup unix.stackexchange.com | awk '/^Address: / print $2 '
                  dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ getline ; print $5 '


                  If you want to only print one IP, then add the exit command to awk's workflow.



                  dig +short unix.stackexchange.com | awk ' print ; exit '
                  getent hosts unix.stackexchange.com | awk ' print $1 ; exit '
                  host unix.stackexchange.com | awk '/has address/ print $4 ; exit '
                  nslookup unix.stackexchange.com | awk '/^Address: / print $2 ; exit '
                  dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ getline ; print $5 ; exit '





                  share|improve this answer


















                  • 2




                    By default, using dig only works with ipv4, where host gives both ipv4 and ipv6 answers. This might be unexpected. You can try host www.google.com, dig +short www.google.com, host ipv6.google.com, dig +short ipv6.google.com, host www.facebook.com, dig +short www.facebook.com.
                    – jfg956
                    Sep 21 '11 at 15:21






                  • 4




                    DIG does not work, if is a CNAME it will not return the IP.
                    – sorin
                    Mar 14 '14 at 14:23






                  • 3




                    Sometimes, host can be timed out and returns nothing. For some domains, dig +short may return domain alias in the first line. So, to ensure the output is an IPv4 address, use dig +short example.com | grep -Eo '[0-9.]7,15' | head -1.
                    – caiguanhao
                    Jun 7 '14 at 15:25






                  • 5




                    Using getent hosts <host> is incorrect, as for instance it may give an IPv6 address while IPv6 doesn't work. The correct solution is to use getent ahosts <host> to try both IPv6 and IPv4 if needed.
                    – vinc17
                    Oct 3 '16 at 15:02






                  • 3




                    Worth mentioning: host, dig and nslookup seems to directly talk to the servers listed in resolv.conf, whereas "getent hosts" respect both the local hosts file and library-level caching (such as nscd) if enabled.
                    – Saustrup
                    Jun 12 '17 at 11:59














                  up vote
                  425
                  down vote



                  accepted










                  You can use getent, which comes with glibc (so you almost certainly have it on Linux). This resolves using gethostbyaddr/gethostbyname2, and so also will check /etc/hosts/NIS/etc:



                  getent hosts unix.stackexchange.com | awk ' print $1 '


                  Or, as Heinzi said below, you can use dig with the +short argument (queries DNS servers directly, does not look at /etc/hosts/NSS/etc) :



                  dig +short unix.stackexchange.com


                  If dig +short is unavailable, any one of the following should work. All of these query DNS directly and ignore other means of resolution:



                  host unix.stackexchange.com | awk '/has address/ print $4 '
                  nslookup unix.stackexchange.com | awk '/^Address: / print $2 '
                  dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ getline ; print $5 '


                  If you want to only print one IP, then add the exit command to awk's workflow.



                  dig +short unix.stackexchange.com | awk ' print ; exit '
                  getent hosts unix.stackexchange.com | awk ' print $1 ; exit '
                  host unix.stackexchange.com | awk '/has address/ print $4 ; exit '
                  nslookup unix.stackexchange.com | awk '/^Address: / print $2 ; exit '
                  dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ getline ; print $5 ; exit '





                  share|improve this answer


















                  • 2




                    By default, using dig only works with ipv4, where host gives both ipv4 and ipv6 answers. This might be unexpected. You can try host www.google.com, dig +short www.google.com, host ipv6.google.com, dig +short ipv6.google.com, host www.facebook.com, dig +short www.facebook.com.
                    – jfg956
                    Sep 21 '11 at 15:21






                  • 4




                    DIG does not work, if is a CNAME it will not return the IP.
                    – sorin
                    Mar 14 '14 at 14:23






                  • 3




                    Sometimes, host can be timed out and returns nothing. For some domains, dig +short may return domain alias in the first line. So, to ensure the output is an IPv4 address, use dig +short example.com | grep -Eo '[0-9.]7,15' | head -1.
                    – caiguanhao
                    Jun 7 '14 at 15:25






                  • 5




                    Using getent hosts <host> is incorrect, as for instance it may give an IPv6 address while IPv6 doesn't work. The correct solution is to use getent ahosts <host> to try both IPv6 and IPv4 if needed.
                    – vinc17
                    Oct 3 '16 at 15:02






                  • 3




                    Worth mentioning: host, dig and nslookup seems to directly talk to the servers listed in resolv.conf, whereas "getent hosts" respect both the local hosts file and library-level caching (such as nscd) if enabled.
                    – Saustrup
                    Jun 12 '17 at 11:59












                  up vote
                  425
                  down vote



                  accepted







                  up vote
                  425
                  down vote



                  accepted






                  You can use getent, which comes with glibc (so you almost certainly have it on Linux). This resolves using gethostbyaddr/gethostbyname2, and so also will check /etc/hosts/NIS/etc:



                  getent hosts unix.stackexchange.com | awk ' print $1 '


                  Or, as Heinzi said below, you can use dig with the +short argument (queries DNS servers directly, does not look at /etc/hosts/NSS/etc) :



                  dig +short unix.stackexchange.com


                  If dig +short is unavailable, any one of the following should work. All of these query DNS directly and ignore other means of resolution:



                  host unix.stackexchange.com | awk '/has address/ print $4 '
                  nslookup unix.stackexchange.com | awk '/^Address: / print $2 '
                  dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ getline ; print $5 '


                  If you want to only print one IP, then add the exit command to awk's workflow.



                  dig +short unix.stackexchange.com | awk ' print ; exit '
                  getent hosts unix.stackexchange.com | awk ' print $1 ; exit '
                  host unix.stackexchange.com | awk '/has address/ print $4 ; exit '
                  nslookup unix.stackexchange.com | awk '/^Address: / print $2 ; exit '
                  dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ getline ; print $5 ; exit '





                  share|improve this answer














                  You can use getent, which comes with glibc (so you almost certainly have it on Linux). This resolves using gethostbyaddr/gethostbyname2, and so also will check /etc/hosts/NIS/etc:



                  getent hosts unix.stackexchange.com | awk ' print $1 '


                  Or, as Heinzi said below, you can use dig with the +short argument (queries DNS servers directly, does not look at /etc/hosts/NSS/etc) :



                  dig +short unix.stackexchange.com


                  If dig +short is unavailable, any one of the following should work. All of these query DNS directly and ignore other means of resolution:



                  host unix.stackexchange.com | awk '/has address/ print $4 '
                  nslookup unix.stackexchange.com | awk '/^Address: / print $2 '
                  dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ getline ; print $5 '


                  If you want to only print one IP, then add the exit command to awk's workflow.



                  dig +short unix.stackexchange.com | awk ' print ; exit '
                  getent hosts unix.stackexchange.com | awk ' print $1 ; exit '
                  host unix.stackexchange.com | awk '/has address/ print $4 ; exit '
                  nslookup unix.stackexchange.com | awk '/^Address: / print $2 ; exit '
                  dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ getline ; print $5 ; exit '






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 3 '16 at 15:13

























                  answered Sep 15 '11 at 16:29









                  Chris Down

                  76.5k12180196




                  76.5k12180196







                  • 2




                    By default, using dig only works with ipv4, where host gives both ipv4 and ipv6 answers. This might be unexpected. You can try host www.google.com, dig +short www.google.com, host ipv6.google.com, dig +short ipv6.google.com, host www.facebook.com, dig +short www.facebook.com.
                    – jfg956
                    Sep 21 '11 at 15:21






                  • 4




                    DIG does not work, if is a CNAME it will not return the IP.
                    – sorin
                    Mar 14 '14 at 14:23






                  • 3




                    Sometimes, host can be timed out and returns nothing. For some domains, dig +short may return domain alias in the first line. So, to ensure the output is an IPv4 address, use dig +short example.com | grep -Eo '[0-9.]7,15' | head -1.
                    – caiguanhao
                    Jun 7 '14 at 15:25






                  • 5




                    Using getent hosts <host> is incorrect, as for instance it may give an IPv6 address while IPv6 doesn't work. The correct solution is to use getent ahosts <host> to try both IPv6 and IPv4 if needed.
                    – vinc17
                    Oct 3 '16 at 15:02






                  • 3




                    Worth mentioning: host, dig and nslookup seems to directly talk to the servers listed in resolv.conf, whereas "getent hosts" respect both the local hosts file and library-level caching (such as nscd) if enabled.
                    – Saustrup
                    Jun 12 '17 at 11:59












                  • 2




                    By default, using dig only works with ipv4, where host gives both ipv4 and ipv6 answers. This might be unexpected. You can try host www.google.com, dig +short www.google.com, host ipv6.google.com, dig +short ipv6.google.com, host www.facebook.com, dig +short www.facebook.com.
                    – jfg956
                    Sep 21 '11 at 15:21






                  • 4




                    DIG does not work, if is a CNAME it will not return the IP.
                    – sorin
                    Mar 14 '14 at 14:23






                  • 3




                    Sometimes, host can be timed out and returns nothing. For some domains, dig +short may return domain alias in the first line. So, to ensure the output is an IPv4 address, use dig +short example.com | grep -Eo '[0-9.]7,15' | head -1.
                    – caiguanhao
                    Jun 7 '14 at 15:25






                  • 5




                    Using getent hosts <host> is incorrect, as for instance it may give an IPv6 address while IPv6 doesn't work. The correct solution is to use getent ahosts <host> to try both IPv6 and IPv4 if needed.
                    – vinc17
                    Oct 3 '16 at 15:02






                  • 3




                    Worth mentioning: host, dig and nslookup seems to directly talk to the servers listed in resolv.conf, whereas "getent hosts" respect both the local hosts file and library-level caching (such as nscd) if enabled.
                    – Saustrup
                    Jun 12 '17 at 11:59







                  2




                  2




                  By default, using dig only works with ipv4, where host gives both ipv4 and ipv6 answers. This might be unexpected. You can try host www.google.com, dig +short www.google.com, host ipv6.google.com, dig +short ipv6.google.com, host www.facebook.com, dig +short www.facebook.com.
                  – jfg956
                  Sep 21 '11 at 15:21




                  By default, using dig only works with ipv4, where host gives both ipv4 and ipv6 answers. This might be unexpected. You can try host www.google.com, dig +short www.google.com, host ipv6.google.com, dig +short ipv6.google.com, host www.facebook.com, dig +short www.facebook.com.
                  – jfg956
                  Sep 21 '11 at 15:21




                  4




                  4




                  DIG does not work, if is a CNAME it will not return the IP.
                  – sorin
                  Mar 14 '14 at 14:23




                  DIG does not work, if is a CNAME it will not return the IP.
                  – sorin
                  Mar 14 '14 at 14:23




                  3




                  3




                  Sometimes, host can be timed out and returns nothing. For some domains, dig +short may return domain alias in the first line. So, to ensure the output is an IPv4 address, use dig +short example.com | grep -Eo '[0-9.]7,15' | head -1.
                  – caiguanhao
                  Jun 7 '14 at 15:25




                  Sometimes, host can be timed out and returns nothing. For some domains, dig +short may return domain alias in the first line. So, to ensure the output is an IPv4 address, use dig +short example.com | grep -Eo '[0-9.]7,15' | head -1.
                  – caiguanhao
                  Jun 7 '14 at 15:25




                  5




                  5




                  Using getent hosts <host> is incorrect, as for instance it may give an IPv6 address while IPv6 doesn't work. The correct solution is to use getent ahosts <host> to try both IPv6 and IPv4 if needed.
                  – vinc17
                  Oct 3 '16 at 15:02




                  Using getent hosts <host> is incorrect, as for instance it may give an IPv6 address while IPv6 doesn't work. The correct solution is to use getent ahosts <host> to try both IPv6 and IPv4 if needed.
                  – vinc17
                  Oct 3 '16 at 15:02




                  3




                  3




                  Worth mentioning: host, dig and nslookup seems to directly talk to the servers listed in resolv.conf, whereas "getent hosts" respect both the local hosts file and library-level caching (such as nscd) if enabled.
                  – Saustrup
                  Jun 12 '17 at 11:59




                  Worth mentioning: host, dig and nslookup seems to directly talk to the servers listed in resolv.conf, whereas "getent hosts" respect both the local hosts file and library-level caching (such as nscd) if enabled.
                  – Saustrup
                  Jun 12 '17 at 11:59












                  up vote
                  125
                  down vote













                  With host from the dnsutils package:



                  $ host unix.stackexchange.com
                  unix.stackexchange.com has address 64.34.119.12


                  (Corrected package name according to the comments. As a note other distributions have host is different packages: Ubuntu bind9-host, openSUSE bind-utils, Frugalware bind.)






                  share|improve this answer


















                  • 4




                    Did you mean dnsutils? Anyway, host worked nice, thanks
                    – Eugene Yarmash
                    Sep 15 '11 at 14:55











                  • You're probably right. I have no Arch here to check. (Intended to append a comment later mentioning this, but the answer was already upvoted so I supposed I nailed it...)
                    – manatwork
                    Sep 15 '11 at 15:06






                  • 1




                    See the resolveip entry below if you need to resolve something not in DNS (e.g. /etc/hosts)
                    – Gavin Brock
                    Jul 2 '12 at 8:56






                  • 2




                    Be aware that host sometimes returns multi-line output (in the case of redirects), you'll want host unix.stackexchange.com | tail -n1 if you just want the line with the IP address.
                    – Edward Coffey
                    Jan 23 '13 at 5:04






                  • 3




                    This answer deserves a serious downvote. host is a DNS tool (similar to nslookup) so it only looks up hosts in DNS, not in e.g. /etc/hosts. So it is NOT an answer to OP's question.
                    – peterh
                    Sep 11 '14 at 9:49














                  up vote
                  125
                  down vote













                  With host from the dnsutils package:



                  $ host unix.stackexchange.com
                  unix.stackexchange.com has address 64.34.119.12


                  (Corrected package name according to the comments. As a note other distributions have host is different packages: Ubuntu bind9-host, openSUSE bind-utils, Frugalware bind.)






                  share|improve this answer


















                  • 4




                    Did you mean dnsutils? Anyway, host worked nice, thanks
                    – Eugene Yarmash
                    Sep 15 '11 at 14:55











                  • You're probably right. I have no Arch here to check. (Intended to append a comment later mentioning this, but the answer was already upvoted so I supposed I nailed it...)
                    – manatwork
                    Sep 15 '11 at 15:06






                  • 1




                    See the resolveip entry below if you need to resolve something not in DNS (e.g. /etc/hosts)
                    – Gavin Brock
                    Jul 2 '12 at 8:56






                  • 2




                    Be aware that host sometimes returns multi-line output (in the case of redirects), you'll want host unix.stackexchange.com | tail -n1 if you just want the line with the IP address.
                    – Edward Coffey
                    Jan 23 '13 at 5:04






                  • 3




                    This answer deserves a serious downvote. host is a DNS tool (similar to nslookup) so it only looks up hosts in DNS, not in e.g. /etc/hosts. So it is NOT an answer to OP's question.
                    – peterh
                    Sep 11 '14 at 9:49












                  up vote
                  125
                  down vote










                  up vote
                  125
                  down vote









                  With host from the dnsutils package:



                  $ host unix.stackexchange.com
                  unix.stackexchange.com has address 64.34.119.12


                  (Corrected package name according to the comments. As a note other distributions have host is different packages: Ubuntu bind9-host, openSUSE bind-utils, Frugalware bind.)






                  share|improve this answer














                  With host from the dnsutils package:



                  $ host unix.stackexchange.com
                  unix.stackexchange.com has address 64.34.119.12


                  (Corrected package name according to the comments. As a note other distributions have host is different packages: Ubuntu bind9-host, openSUSE bind-utils, Frugalware bind.)







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 15 '11 at 16:35

























                  answered Sep 15 '11 at 14:50









                  manatwork

                  21.1k38184




                  21.1k38184







                  • 4




                    Did you mean dnsutils? Anyway, host worked nice, thanks
                    – Eugene Yarmash
                    Sep 15 '11 at 14:55











                  • You're probably right. I have no Arch here to check. (Intended to append a comment later mentioning this, but the answer was already upvoted so I supposed I nailed it...)
                    – manatwork
                    Sep 15 '11 at 15:06






                  • 1




                    See the resolveip entry below if you need to resolve something not in DNS (e.g. /etc/hosts)
                    – Gavin Brock
                    Jul 2 '12 at 8:56






                  • 2




                    Be aware that host sometimes returns multi-line output (in the case of redirects), you'll want host unix.stackexchange.com | tail -n1 if you just want the line with the IP address.
                    – Edward Coffey
                    Jan 23 '13 at 5:04






                  • 3




                    This answer deserves a serious downvote. host is a DNS tool (similar to nslookup) so it only looks up hosts in DNS, not in e.g. /etc/hosts. So it is NOT an answer to OP's question.
                    – peterh
                    Sep 11 '14 at 9:49












                  • 4




                    Did you mean dnsutils? Anyway, host worked nice, thanks
                    – Eugene Yarmash
                    Sep 15 '11 at 14:55











                  • You're probably right. I have no Arch here to check. (Intended to append a comment later mentioning this, but the answer was already upvoted so I supposed I nailed it...)
                    – manatwork
                    Sep 15 '11 at 15:06






                  • 1




                    See the resolveip entry below if you need to resolve something not in DNS (e.g. /etc/hosts)
                    – Gavin Brock
                    Jul 2 '12 at 8:56






                  • 2




                    Be aware that host sometimes returns multi-line output (in the case of redirects), you'll want host unix.stackexchange.com | tail -n1 if you just want the line with the IP address.
                    – Edward Coffey
                    Jan 23 '13 at 5:04






                  • 3




                    This answer deserves a serious downvote. host is a DNS tool (similar to nslookup) so it only looks up hosts in DNS, not in e.g. /etc/hosts. So it is NOT an answer to OP's question.
                    – peterh
                    Sep 11 '14 at 9:49







                  4




                  4




                  Did you mean dnsutils? Anyway, host worked nice, thanks
                  – Eugene Yarmash
                  Sep 15 '11 at 14:55





                  Did you mean dnsutils? Anyway, host worked nice, thanks
                  – Eugene Yarmash
                  Sep 15 '11 at 14:55













                  You're probably right. I have no Arch here to check. (Intended to append a comment later mentioning this, but the answer was already upvoted so I supposed I nailed it...)
                  – manatwork
                  Sep 15 '11 at 15:06




                  You're probably right. I have no Arch here to check. (Intended to append a comment later mentioning this, but the answer was already upvoted so I supposed I nailed it...)
                  – manatwork
                  Sep 15 '11 at 15:06




                  1




                  1




                  See the resolveip entry below if you need to resolve something not in DNS (e.g. /etc/hosts)
                  – Gavin Brock
                  Jul 2 '12 at 8:56




                  See the resolveip entry below if you need to resolve something not in DNS (e.g. /etc/hosts)
                  – Gavin Brock
                  Jul 2 '12 at 8:56




                  2




                  2




                  Be aware that host sometimes returns multi-line output (in the case of redirects), you'll want host unix.stackexchange.com | tail -n1 if you just want the line with the IP address.
                  – Edward Coffey
                  Jan 23 '13 at 5:04




                  Be aware that host sometimes returns multi-line output (in the case of redirects), you'll want host unix.stackexchange.com | tail -n1 if you just want the line with the IP address.
                  – Edward Coffey
                  Jan 23 '13 at 5:04




                  3




                  3




                  This answer deserves a serious downvote. host is a DNS tool (similar to nslookup) so it only looks up hosts in DNS, not in e.g. /etc/hosts. So it is NOT an answer to OP's question.
                  – peterh
                  Sep 11 '14 at 9:49




                  This answer deserves a serious downvote. host is a DNS tool (similar to nslookup) so it only looks up hosts in DNS, not in e.g. /etc/hosts. So it is NOT an answer to OP's question.
                  – peterh
                  Sep 11 '14 at 9:49










                  up vote
                  47
                  down vote













                  I have a tool on my machine that seems to do the job. The man page shows it seems to come with mysql... Here is how you could use it:



                  resolveip -s unix.stackexchange.com
                  64.34.119.12


                  The return value of this tool is different from 0 if the hostname cannot be resolved :



                  resolveip -s unix.stackexchange.coma
                  resolveip: Unable to find hostid for 'unix.stackexchange.coma': host not found
                  exit 2


                  UPDATE
                  On fedora, it comes with mysql-server :



                  yum provides "*/resolveip"
                  mysql-server-5.5.10-2.fc15.x86_64 : The MySQL server and related files
                  Dépôt  : fedora
                  Correspondance depuis :
                  Nom de fichier  : /usr/bin/resolveip


                  I guess it would create a strange dependency for your script...






                  share|improve this answer
















                  • 5




                    This seems to be the only solution on here that uses the OS's build in resolver - so works for /etc/hosts as well as DNS.
                    – Gavin Brock
                    Jul 2 '12 at 8:57







                  • 7




                    getent, as detailed in the other answer, also looks at /etc/hosts, and comes with glibc, so has no dependencies on a Linux system.
                    – Asfand Qazi
                    Apr 16 '16 at 13:32














                  up vote
                  47
                  down vote













                  I have a tool on my machine that seems to do the job. The man page shows it seems to come with mysql... Here is how you could use it:



                  resolveip -s unix.stackexchange.com
                  64.34.119.12


                  The return value of this tool is different from 0 if the hostname cannot be resolved :



                  resolveip -s unix.stackexchange.coma
                  resolveip: Unable to find hostid for 'unix.stackexchange.coma': host not found
                  exit 2


                  UPDATE
                  On fedora, it comes with mysql-server :



                  yum provides "*/resolveip"
                  mysql-server-5.5.10-2.fc15.x86_64 : The MySQL server and related files
                  Dépôt  : fedora
                  Correspondance depuis :
                  Nom de fichier  : /usr/bin/resolveip


                  I guess it would create a strange dependency for your script...






                  share|improve this answer
















                  • 5




                    This seems to be the only solution on here that uses the OS's build in resolver - so works for /etc/hosts as well as DNS.
                    – Gavin Brock
                    Jul 2 '12 at 8:57







                  • 7




                    getent, as detailed in the other answer, also looks at /etc/hosts, and comes with glibc, so has no dependencies on a Linux system.
                    – Asfand Qazi
                    Apr 16 '16 at 13:32












                  up vote
                  47
                  down vote










                  up vote
                  47
                  down vote









                  I have a tool on my machine that seems to do the job. The man page shows it seems to come with mysql... Here is how you could use it:



                  resolveip -s unix.stackexchange.com
                  64.34.119.12


                  The return value of this tool is different from 0 if the hostname cannot be resolved :



                  resolveip -s unix.stackexchange.coma
                  resolveip: Unable to find hostid for 'unix.stackexchange.coma': host not found
                  exit 2


                  UPDATE
                  On fedora, it comes with mysql-server :



                  yum provides "*/resolveip"
                  mysql-server-5.5.10-2.fc15.x86_64 : The MySQL server and related files
                  Dépôt  : fedora
                  Correspondance depuis :
                  Nom de fichier  : /usr/bin/resolveip


                  I guess it would create a strange dependency for your script...






                  share|improve this answer












                  I have a tool on my machine that seems to do the job. The man page shows it seems to come with mysql... Here is how you could use it:



                  resolveip -s unix.stackexchange.com
                  64.34.119.12


                  The return value of this tool is different from 0 if the hostname cannot be resolved :



                  resolveip -s unix.stackexchange.coma
                  resolveip: Unable to find hostid for 'unix.stackexchange.coma': host not found
                  exit 2


                  UPDATE
                  On fedora, it comes with mysql-server :



                  yum provides "*/resolveip"
                  mysql-server-5.5.10-2.fc15.x86_64 : The MySQL server and related files
                  Dépôt  : fedora
                  Correspondance depuis :
                  Nom de fichier  : /usr/bin/resolveip


                  I guess it would create a strange dependency for your script...







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 15 '11 at 21:50









                  greg0ire

                  1,16611232




                  1,16611232







                  • 5




                    This seems to be the only solution on here that uses the OS's build in resolver - so works for /etc/hosts as well as DNS.
                    – Gavin Brock
                    Jul 2 '12 at 8:57







                  • 7




                    getent, as detailed in the other answer, also looks at /etc/hosts, and comes with glibc, so has no dependencies on a Linux system.
                    – Asfand Qazi
                    Apr 16 '16 at 13:32












                  • 5




                    This seems to be the only solution on here that uses the OS's build in resolver - so works for /etc/hosts as well as DNS.
                    – Gavin Brock
                    Jul 2 '12 at 8:57







                  • 7




                    getent, as detailed in the other answer, also looks at /etc/hosts, and comes with glibc, so has no dependencies on a Linux system.
                    – Asfand Qazi
                    Apr 16 '16 at 13:32







                  5




                  5




                  This seems to be the only solution on here that uses the OS's build in resolver - so works for /etc/hosts as well as DNS.
                  – Gavin Brock
                  Jul 2 '12 at 8:57





                  This seems to be the only solution on here that uses the OS's build in resolver - so works for /etc/hosts as well as DNS.
                  – Gavin Brock
                  Jul 2 '12 at 8:57





                  7




                  7




                  getent, as detailed in the other answer, also looks at /etc/hosts, and comes with glibc, so has no dependencies on a Linux system.
                  – Asfand Qazi
                  Apr 16 '16 at 13:32




                  getent, as detailed in the other answer, also looks at /etc/hosts, and comes with glibc, so has no dependencies on a Linux system.
                  – Asfand Qazi
                  Apr 16 '16 at 13:32










                  up vote
                  38
                  down vote













                  The following command using dig allows you to read the result directly without any sed/awk/etc. magic:



                  $ dig +short unix.stackexchange.com
                  64.34.119.12


                  dig is also included in the dnsutils package.




                  Note: dig has a return value of 0, even if the name could not be resolved. Thus, you'd need to check if the output is empty instead of checking the return value:



                  hostname=unix.stackexchange.com

                  ip=`dig +short $hostname`

                  if [ -n "$ip" ]; then
                  echo IP: $ip
                  else
                  echo Could not resolve hostname.
                  fi



                  Note 2: If a hostname has multiple IP addresses (try debian.org, for example), all of them will be returned. This "problem" affects all of the tools mentioned in this question so far:






                  share|improve this answer






















                  • Note that if a domain has a CNAME entry its domain may be printed in the first line instead of an IP address.
                    – pcworld
                    Apr 27 at 23:27














                  up vote
                  38
                  down vote













                  The following command using dig allows you to read the result directly without any sed/awk/etc. magic:



                  $ dig +short unix.stackexchange.com
                  64.34.119.12


                  dig is also included in the dnsutils package.




                  Note: dig has a return value of 0, even if the name could not be resolved. Thus, you'd need to check if the output is empty instead of checking the return value:



                  hostname=unix.stackexchange.com

                  ip=`dig +short $hostname`

                  if [ -n "$ip" ]; then
                  echo IP: $ip
                  else
                  echo Could not resolve hostname.
                  fi



                  Note 2: If a hostname has multiple IP addresses (try debian.org, for example), all of them will be returned. This "problem" affects all of the tools mentioned in this question so far:






                  share|improve this answer






















                  • Note that if a domain has a CNAME entry its domain may be printed in the first line instead of an IP address.
                    – pcworld
                    Apr 27 at 23:27












                  up vote
                  38
                  down vote










                  up vote
                  38
                  down vote









                  The following command using dig allows you to read the result directly without any sed/awk/etc. magic:



                  $ dig +short unix.stackexchange.com
                  64.34.119.12


                  dig is also included in the dnsutils package.




                  Note: dig has a return value of 0, even if the name could not be resolved. Thus, you'd need to check if the output is empty instead of checking the return value:



                  hostname=unix.stackexchange.com

                  ip=`dig +short $hostname`

                  if [ -n "$ip" ]; then
                  echo IP: $ip
                  else
                  echo Could not resolve hostname.
                  fi



                  Note 2: If a hostname has multiple IP addresses (try debian.org, for example), all of them will be returned. This "problem" affects all of the tools mentioned in this question so far:






                  share|improve this answer














                  The following command using dig allows you to read the result directly without any sed/awk/etc. magic:



                  $ dig +short unix.stackexchange.com
                  64.34.119.12


                  dig is also included in the dnsutils package.




                  Note: dig has a return value of 0, even if the name could not be resolved. Thus, you'd need to check if the output is empty instead of checking the return value:



                  hostname=unix.stackexchange.com

                  ip=`dig +short $hostname`

                  if [ -n "$ip" ]; then
                  echo IP: $ip
                  else
                  echo Could not resolve hostname.
                  fi



                  Note 2: If a hostname has multiple IP addresses (try debian.org, for example), all of them will be returned. This "problem" affects all of the tools mentioned in this question so far:







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 15 '11 at 18:32

























                  answered Sep 15 '11 at 18:23









                  Heinzi

                  6651613




                  6651613











                  • Note that if a domain has a CNAME entry its domain may be printed in the first line instead of an IP address.
                    – pcworld
                    Apr 27 at 23:27
















                  • Note that if a domain has a CNAME entry its domain may be printed in the first line instead of an IP address.
                    – pcworld
                    Apr 27 at 23:27















                  Note that if a domain has a CNAME entry its domain may be printed in the first line instead of an IP address.
                  – pcworld
                  Apr 27 at 23:27




                  Note that if a domain has a CNAME entry its domain may be printed in the first line instead of an IP address.
                  – pcworld
                  Apr 27 at 23:27










                  up vote
                  32
                  down vote













                  getent hosts unix.stackexchange.com | cut -d' ' -f1





                  share|improve this answer
















                  • 4




                    Also consider ahosts, ahostsv4, ahostsv6 with getent.
                    – 0xC0000022L
                    Apr 29 '15 at 22:12










                  • cut will not for getent's, which use t to separate columns. This is the case on Solaris.
                    – ceving
                    Mar 18 '16 at 9:51






                  • 1




                    @ceving: On Solaris you might have to run cut without -d (defaults to t as delimiter ). On Linux it's spaces, thus the line above works.
                    – sborsky
                    Apr 7 '16 at 22:24














                  up vote
                  32
                  down vote













                  getent hosts unix.stackexchange.com | cut -d' ' -f1





                  share|improve this answer
















                  • 4




                    Also consider ahosts, ahostsv4, ahostsv6 with getent.
                    – 0xC0000022L
                    Apr 29 '15 at 22:12










                  • cut will not for getent's, which use t to separate columns. This is the case on Solaris.
                    – ceving
                    Mar 18 '16 at 9:51






                  • 1




                    @ceving: On Solaris you might have to run cut without -d (defaults to t as delimiter ). On Linux it's spaces, thus the line above works.
                    – sborsky
                    Apr 7 '16 at 22:24












                  up vote
                  32
                  down vote










                  up vote
                  32
                  down vote









                  getent hosts unix.stackexchange.com | cut -d' ' -f1





                  share|improve this answer












                  getent hosts unix.stackexchange.com | cut -d' ' -f1






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 4 '13 at 19:12









                  sborsky

                  729511




                  729511







                  • 4




                    Also consider ahosts, ahostsv4, ahostsv6 with getent.
                    – 0xC0000022L
                    Apr 29 '15 at 22:12










                  • cut will not for getent's, which use t to separate columns. This is the case on Solaris.
                    – ceving
                    Mar 18 '16 at 9:51






                  • 1




                    @ceving: On Solaris you might have to run cut without -d (defaults to t as delimiter ). On Linux it's spaces, thus the line above works.
                    – sborsky
                    Apr 7 '16 at 22:24












                  • 4




                    Also consider ahosts, ahostsv4, ahostsv6 with getent.
                    – 0xC0000022L
                    Apr 29 '15 at 22:12










                  • cut will not for getent's, which use t to separate columns. This is the case on Solaris.
                    – ceving
                    Mar 18 '16 at 9:51






                  • 1




                    @ceving: On Solaris you might have to run cut without -d (defaults to t as delimiter ). On Linux it's spaces, thus the line above works.
                    – sborsky
                    Apr 7 '16 at 22:24







                  4




                  4




                  Also consider ahosts, ahostsv4, ahostsv6 with getent.
                  – 0xC0000022L
                  Apr 29 '15 at 22:12




                  Also consider ahosts, ahostsv4, ahostsv6 with getent.
                  – 0xC0000022L
                  Apr 29 '15 at 22:12












                  cut will not for getent's, which use t to separate columns. This is the case on Solaris.
                  – ceving
                  Mar 18 '16 at 9:51




                  cut will not for getent's, which use t to separate columns. This is the case on Solaris.
                  – ceving
                  Mar 18 '16 at 9:51




                  1




                  1




                  @ceving: On Solaris you might have to run cut without -d (defaults to t as delimiter ). On Linux it's spaces, thus the line above works.
                  – sborsky
                  Apr 7 '16 at 22:24




                  @ceving: On Solaris you might have to run cut without -d (defaults to t as delimiter ). On Linux it's spaces, thus the line above works.
                  – sborsky
                  Apr 7 '16 at 22:24










                  up vote
                  25
                  down vote













                  The solutions given so far mostly work in the simpler case: the hostname directly resolves to a single IPv4 address. This might be the only case where you need to resolve hostnames, but if not, below is a discussion on some cases that you might need to handle.



                  Chris Down and Heinzi briefly discussed the case where the hostname resolves to more than one IP addresses. In this case (and others below), basic scripting under the assumption that a hostname directly resolves to a single IP address may break. Below, an example with a hostname resolving to more than a single IP address:



                  $ host www.l.google.com
                  www.l.google.com has address 209.85.148.147
                  www.l.google.com has address 209.85.148.103
                  www.l.google.com has address 209.85.148.99
                  www.l.google.com has address 209.85.148.106
                  www.l.google.com has address 209.85.148.105
                  www.l.google.com has address 209.85.148.104


                  But what is www.l.google.com? This is where the alias case needs to be introduced. Let's check the example below:



                  $ host www.google.com
                  www.google.com is an alias for www.l.google.com.
                  www.l.google.com has address 74.125.39.103
                  www.l.google.com has address 74.125.39.147
                  www.l.google.com has address 74.125.39.105
                  www.l.google.com has address 74.125.39.99
                  www.l.google.com has address 74.125.39.106
                  www.l.google.com has address 74.125.39.104


                  So www.google.com does not directly resolve to IP addresses, but to an alias that itself resolves to multiple IP addresses. For more information on aliases, check here. Of course, the case where an alias has a single IP address is possible, as shown below:



                  $ host g.www.ms.akadns.net
                  g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
                  lb1.www.ms.akadns.net has address 207.46.19.190


                  But can aliases be chained? The answer is yes:



                  $ host www.microsoft.com
                  www.microsoft.com is an alias for toggle.www.ms.akadns.net.
                  toggle.www.ms.akadns.net is an alias for g.www.ms.akadns.net.
                  g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
                  lb1.www.ms.akadns.net has address 207.46.19.254

                  $ host www.google.fr
                  www.google.fr is an alias for www.google.com.
                  www.google.com is an alias for www.l.google.com.
                  www.l.google.com has address 74.125.39.147
                  www.l.google.com has address 74.125.39.103
                  www.l.google.com has address 74.125.39.99
                  www.l.google.com has address 74.125.39.106
                  www.l.google.com has address 74.125.39.104
                  www.l.google.com has address 74.125.39.105


                  I did not find any example where a hostname resolves to an alias that does not resolve to an IP address, but I think the case might occur.



                  More than multiple IP addresses and aliases, is there some other special cases... what about IPv6? You could try:



                  $ host ipv6.google.com
                  ipv6.google.com is an alias for ipv6.l.google.com.
                  ipv6.l.google.com has IPv6 address 2a00:1450:8007::68


                  Where the hostname ipv6.google.com is an IPv6-only hostname. What about dual-stack hostnames:



                  $ host www.facebook.com
                  www.facebook.com has address 66.220.153.15
                  www.facebook.com has IPv6 address 2620:0:1c08:4000:face:b00c::


                  Again about IPv6, if your host is IPv4 only, you can still resolve IPv6 addresses (tested on a IPv4 only WinXP and with ipv6.google.com, you could try it on Linux). In this case, the resolution succeeds, but a ping fails with an unknown host error message. This might be a case where your scripting fails.



                  I hope those remarks were useful.






                  share|improve this answer


















                  • 2




                    What a great complement to the accepted answer, showing all the edge cases that one might want to deal with in scripting. My version host does not even state "has address" for my boxes.
                    – Mihai Danila
                    Feb 15 '13 at 23:23














                  up vote
                  25
                  down vote













                  The solutions given so far mostly work in the simpler case: the hostname directly resolves to a single IPv4 address. This might be the only case where you need to resolve hostnames, but if not, below is a discussion on some cases that you might need to handle.



                  Chris Down and Heinzi briefly discussed the case where the hostname resolves to more than one IP addresses. In this case (and others below), basic scripting under the assumption that a hostname directly resolves to a single IP address may break. Below, an example with a hostname resolving to more than a single IP address:



                  $ host www.l.google.com
                  www.l.google.com has address 209.85.148.147
                  www.l.google.com has address 209.85.148.103
                  www.l.google.com has address 209.85.148.99
                  www.l.google.com has address 209.85.148.106
                  www.l.google.com has address 209.85.148.105
                  www.l.google.com has address 209.85.148.104


                  But what is www.l.google.com? This is where the alias case needs to be introduced. Let's check the example below:



                  $ host www.google.com
                  www.google.com is an alias for www.l.google.com.
                  www.l.google.com has address 74.125.39.103
                  www.l.google.com has address 74.125.39.147
                  www.l.google.com has address 74.125.39.105
                  www.l.google.com has address 74.125.39.99
                  www.l.google.com has address 74.125.39.106
                  www.l.google.com has address 74.125.39.104


                  So www.google.com does not directly resolve to IP addresses, but to an alias that itself resolves to multiple IP addresses. For more information on aliases, check here. Of course, the case where an alias has a single IP address is possible, as shown below:



                  $ host g.www.ms.akadns.net
                  g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
                  lb1.www.ms.akadns.net has address 207.46.19.190


                  But can aliases be chained? The answer is yes:



                  $ host www.microsoft.com
                  www.microsoft.com is an alias for toggle.www.ms.akadns.net.
                  toggle.www.ms.akadns.net is an alias for g.www.ms.akadns.net.
                  g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
                  lb1.www.ms.akadns.net has address 207.46.19.254

                  $ host www.google.fr
                  www.google.fr is an alias for www.google.com.
                  www.google.com is an alias for www.l.google.com.
                  www.l.google.com has address 74.125.39.147
                  www.l.google.com has address 74.125.39.103
                  www.l.google.com has address 74.125.39.99
                  www.l.google.com has address 74.125.39.106
                  www.l.google.com has address 74.125.39.104
                  www.l.google.com has address 74.125.39.105


                  I did not find any example where a hostname resolves to an alias that does not resolve to an IP address, but I think the case might occur.



                  More than multiple IP addresses and aliases, is there some other special cases... what about IPv6? You could try:



                  $ host ipv6.google.com
                  ipv6.google.com is an alias for ipv6.l.google.com.
                  ipv6.l.google.com has IPv6 address 2a00:1450:8007::68


                  Where the hostname ipv6.google.com is an IPv6-only hostname. What about dual-stack hostnames:



                  $ host www.facebook.com
                  www.facebook.com has address 66.220.153.15
                  www.facebook.com has IPv6 address 2620:0:1c08:4000:face:b00c::


                  Again about IPv6, if your host is IPv4 only, you can still resolve IPv6 addresses (tested on a IPv4 only WinXP and with ipv6.google.com, you could try it on Linux). In this case, the resolution succeeds, but a ping fails with an unknown host error message. This might be a case where your scripting fails.



                  I hope those remarks were useful.






                  share|improve this answer


















                  • 2




                    What a great complement to the accepted answer, showing all the edge cases that one might want to deal with in scripting. My version host does not even state "has address" for my boxes.
                    – Mihai Danila
                    Feb 15 '13 at 23:23












                  up vote
                  25
                  down vote










                  up vote
                  25
                  down vote









                  The solutions given so far mostly work in the simpler case: the hostname directly resolves to a single IPv4 address. This might be the only case where you need to resolve hostnames, but if not, below is a discussion on some cases that you might need to handle.



                  Chris Down and Heinzi briefly discussed the case where the hostname resolves to more than one IP addresses. In this case (and others below), basic scripting under the assumption that a hostname directly resolves to a single IP address may break. Below, an example with a hostname resolving to more than a single IP address:



                  $ host www.l.google.com
                  www.l.google.com has address 209.85.148.147
                  www.l.google.com has address 209.85.148.103
                  www.l.google.com has address 209.85.148.99
                  www.l.google.com has address 209.85.148.106
                  www.l.google.com has address 209.85.148.105
                  www.l.google.com has address 209.85.148.104


                  But what is www.l.google.com? This is where the alias case needs to be introduced. Let's check the example below:



                  $ host www.google.com
                  www.google.com is an alias for www.l.google.com.
                  www.l.google.com has address 74.125.39.103
                  www.l.google.com has address 74.125.39.147
                  www.l.google.com has address 74.125.39.105
                  www.l.google.com has address 74.125.39.99
                  www.l.google.com has address 74.125.39.106
                  www.l.google.com has address 74.125.39.104


                  So www.google.com does not directly resolve to IP addresses, but to an alias that itself resolves to multiple IP addresses. For more information on aliases, check here. Of course, the case where an alias has a single IP address is possible, as shown below:



                  $ host g.www.ms.akadns.net
                  g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
                  lb1.www.ms.akadns.net has address 207.46.19.190


                  But can aliases be chained? The answer is yes:



                  $ host www.microsoft.com
                  www.microsoft.com is an alias for toggle.www.ms.akadns.net.
                  toggle.www.ms.akadns.net is an alias for g.www.ms.akadns.net.
                  g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
                  lb1.www.ms.akadns.net has address 207.46.19.254

                  $ host www.google.fr
                  www.google.fr is an alias for www.google.com.
                  www.google.com is an alias for www.l.google.com.
                  www.l.google.com has address 74.125.39.147
                  www.l.google.com has address 74.125.39.103
                  www.l.google.com has address 74.125.39.99
                  www.l.google.com has address 74.125.39.106
                  www.l.google.com has address 74.125.39.104
                  www.l.google.com has address 74.125.39.105


                  I did not find any example where a hostname resolves to an alias that does not resolve to an IP address, but I think the case might occur.



                  More than multiple IP addresses and aliases, is there some other special cases... what about IPv6? You could try:



                  $ host ipv6.google.com
                  ipv6.google.com is an alias for ipv6.l.google.com.
                  ipv6.l.google.com has IPv6 address 2a00:1450:8007::68


                  Where the hostname ipv6.google.com is an IPv6-only hostname. What about dual-stack hostnames:



                  $ host www.facebook.com
                  www.facebook.com has address 66.220.153.15
                  www.facebook.com has IPv6 address 2620:0:1c08:4000:face:b00c::


                  Again about IPv6, if your host is IPv4 only, you can still resolve IPv6 addresses (tested on a IPv4 only WinXP and with ipv6.google.com, you could try it on Linux). In this case, the resolution succeeds, but a ping fails with an unknown host error message. This might be a case where your scripting fails.



                  I hope those remarks were useful.






                  share|improve this answer














                  The solutions given so far mostly work in the simpler case: the hostname directly resolves to a single IPv4 address. This might be the only case where you need to resolve hostnames, but if not, below is a discussion on some cases that you might need to handle.



                  Chris Down and Heinzi briefly discussed the case where the hostname resolves to more than one IP addresses. In this case (and others below), basic scripting under the assumption that a hostname directly resolves to a single IP address may break. Below, an example with a hostname resolving to more than a single IP address:



                  $ host www.l.google.com
                  www.l.google.com has address 209.85.148.147
                  www.l.google.com has address 209.85.148.103
                  www.l.google.com has address 209.85.148.99
                  www.l.google.com has address 209.85.148.106
                  www.l.google.com has address 209.85.148.105
                  www.l.google.com has address 209.85.148.104


                  But what is www.l.google.com? This is where the alias case needs to be introduced. Let's check the example below:



                  $ host www.google.com
                  www.google.com is an alias for www.l.google.com.
                  www.l.google.com has address 74.125.39.103
                  www.l.google.com has address 74.125.39.147
                  www.l.google.com has address 74.125.39.105
                  www.l.google.com has address 74.125.39.99
                  www.l.google.com has address 74.125.39.106
                  www.l.google.com has address 74.125.39.104


                  So www.google.com does not directly resolve to IP addresses, but to an alias that itself resolves to multiple IP addresses. For more information on aliases, check here. Of course, the case where an alias has a single IP address is possible, as shown below:



                  $ host g.www.ms.akadns.net
                  g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
                  lb1.www.ms.akadns.net has address 207.46.19.190


                  But can aliases be chained? The answer is yes:



                  $ host www.microsoft.com
                  www.microsoft.com is an alias for toggle.www.ms.akadns.net.
                  toggle.www.ms.akadns.net is an alias for g.www.ms.akadns.net.
                  g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
                  lb1.www.ms.akadns.net has address 207.46.19.254

                  $ host www.google.fr
                  www.google.fr is an alias for www.google.com.
                  www.google.com is an alias for www.l.google.com.
                  www.l.google.com has address 74.125.39.147
                  www.l.google.com has address 74.125.39.103
                  www.l.google.com has address 74.125.39.99
                  www.l.google.com has address 74.125.39.106
                  www.l.google.com has address 74.125.39.104
                  www.l.google.com has address 74.125.39.105


                  I did not find any example where a hostname resolves to an alias that does not resolve to an IP address, but I think the case might occur.



                  More than multiple IP addresses and aliases, is there some other special cases... what about IPv6? You could try:



                  $ host ipv6.google.com
                  ipv6.google.com is an alias for ipv6.l.google.com.
                  ipv6.l.google.com has IPv6 address 2a00:1450:8007::68


                  Where the hostname ipv6.google.com is an IPv6-only hostname. What about dual-stack hostnames:



                  $ host www.facebook.com
                  www.facebook.com has address 66.220.153.15
                  www.facebook.com has IPv6 address 2620:0:1c08:4000:face:b00c::


                  Again about IPv6, if your host is IPv4 only, you can still resolve IPv6 addresses (tested on a IPv4 only WinXP and with ipv6.google.com, you could try it on Linux). In this case, the resolution succeeds, but a ping fails with an unknown host error message. This might be a case where your scripting fails.



                  I hope those remarks were useful.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 10 at 10:57









                  Jeff Schaller

                  33.6k851113




                  33.6k851113










                  answered Sep 21 '11 at 15:13









                  jfg956

                  3,13711323




                  3,13711323







                  • 2




                    What a great complement to the accepted answer, showing all the edge cases that one might want to deal with in scripting. My version host does not even state "has address" for my boxes.
                    – Mihai Danila
                    Feb 15 '13 at 23:23












                  • 2




                    What a great complement to the accepted answer, showing all the edge cases that one might want to deal with in scripting. My version host does not even state "has address" for my boxes.
                    – Mihai Danila
                    Feb 15 '13 at 23:23







                  2




                  2




                  What a great complement to the accepted answer, showing all the edge cases that one might want to deal with in scripting. My version host does not even state "has address" for my boxes.
                  – Mihai Danila
                  Feb 15 '13 at 23:23




                  What a great complement to the accepted answer, showing all the edge cases that one might want to deal with in scripting. My version host does not even state "has address" for my boxes.
                  – Mihai Danila
                  Feb 15 '13 at 23:23










                  up vote
                  17
                  down vote













                  To avoid the problem with aliases and always get a single IP address ready for use:



                  python -c 'import socket; print socket.gethostbyname("www.example.com")'





                  share|improve this answer




















                  • I made a bash function to stick in my bashrc from this answer: gist.github.com/brycepg/ba117a37de53906dc8fcc312bd7d5fee
                    – Bryce Guinta
                    Aug 11 '17 at 16:58















                  up vote
                  17
                  down vote













                  To avoid the problem with aliases and always get a single IP address ready for use:



                  python -c 'import socket; print socket.gethostbyname("www.example.com")'





                  share|improve this answer




















                  • I made a bash function to stick in my bashrc from this answer: gist.github.com/brycepg/ba117a37de53906dc8fcc312bd7d5fee
                    – Bryce Guinta
                    Aug 11 '17 at 16:58













                  up vote
                  17
                  down vote










                  up vote
                  17
                  down vote









                  To avoid the problem with aliases and always get a single IP address ready for use:



                  python -c 'import socket; print socket.gethostbyname("www.example.com")'





                  share|improve this answer












                  To avoid the problem with aliases and always get a single IP address ready for use:



                  python -c 'import socket; print socket.gethostbyname("www.example.com")'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 12 '12 at 9:34









                  dbernt

                  17112




                  17112











                  • I made a bash function to stick in my bashrc from this answer: gist.github.com/brycepg/ba117a37de53906dc8fcc312bd7d5fee
                    – Bryce Guinta
                    Aug 11 '17 at 16:58

















                  • I made a bash function to stick in my bashrc from this answer: gist.github.com/brycepg/ba117a37de53906dc8fcc312bd7d5fee
                    – Bryce Guinta
                    Aug 11 '17 at 16:58
















                  I made a bash function to stick in my bashrc from this answer: gist.github.com/brycepg/ba117a37de53906dc8fcc312bd7d5fee
                  – Bryce Guinta
                  Aug 11 '17 at 16:58





                  I made a bash function to stick in my bashrc from this answer: gist.github.com/brycepg/ba117a37de53906dc8fcc312bd7d5fee
                  – Bryce Guinta
                  Aug 11 '17 at 16:58











                  up vote
                  15
                  down vote













                  ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/).*//" | sed -e "s/.*(//"


                  works without dependencies on other systems (and for hosts specified in /etc/hosts)






                  share|improve this answer


















                  • 2




                    The use of ping is what I needed as I need the value from the hosts file but the sed pattern parsing correctly but this worked ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/^[^(]*[(]//" | sed -e "s/[)].*$//"
                    – ManiacZX
                    Jan 18 '13 at 19:23







                  • 1




                    To resolve something on my home network like myhostname.local this works so for me this is the best answer.
                    – Matt Friedman
                    Jan 24 '15 at 2:59






                  • 1




                    May I suggest this also: ping -q -c 1 -t 1 bahface.local | grep -m 1 PING | cut -d "(" -f2 | cut -d ")" -f1
                    – Matt Friedman
                    Jan 24 '15 at 3:08










                  • getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> works for declarations inside /etc/hosts, too ... and it's the go-to-tool for all kinds of system databases (passwd, group, aliases, services).
                    – 0xC0000022L
                    Apr 30 '15 at 7:53














                  up vote
                  15
                  down vote













                  ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/).*//" | sed -e "s/.*(//"


                  works without dependencies on other systems (and for hosts specified in /etc/hosts)






                  share|improve this answer


















                  • 2




                    The use of ping is what I needed as I need the value from the hosts file but the sed pattern parsing correctly but this worked ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/^[^(]*[(]//" | sed -e "s/[)].*$//"
                    – ManiacZX
                    Jan 18 '13 at 19:23







                  • 1




                    To resolve something on my home network like myhostname.local this works so for me this is the best answer.
                    – Matt Friedman
                    Jan 24 '15 at 2:59






                  • 1




                    May I suggest this also: ping -q -c 1 -t 1 bahface.local | grep -m 1 PING | cut -d "(" -f2 | cut -d ")" -f1
                    – Matt Friedman
                    Jan 24 '15 at 3:08










                  • getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> works for declarations inside /etc/hosts, too ... and it's the go-to-tool for all kinds of system databases (passwd, group, aliases, services).
                    – 0xC0000022L
                    Apr 30 '15 at 7:53












                  up vote
                  15
                  down vote










                  up vote
                  15
                  down vote









                  ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/).*//" | sed -e "s/.*(//"


                  works without dependencies on other systems (and for hosts specified in /etc/hosts)






                  share|improve this answer














                  ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/).*//" | sed -e "s/.*(//"


                  works without dependencies on other systems (and for hosts specified in /etc/hosts)







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 16 '13 at 17:28









                  Gilles

                  512k12010151547




                  512k12010151547










                  answered Aug 10 '12 at 21:20









                  Andrew McGregor

                  15112




                  15112







                  • 2




                    The use of ping is what I needed as I need the value from the hosts file but the sed pattern parsing correctly but this worked ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/^[^(]*[(]//" | sed -e "s/[)].*$//"
                    – ManiacZX
                    Jan 18 '13 at 19:23







                  • 1




                    To resolve something on my home network like myhostname.local this works so for me this is the best answer.
                    – Matt Friedman
                    Jan 24 '15 at 2:59






                  • 1




                    May I suggest this also: ping -q -c 1 -t 1 bahface.local | grep -m 1 PING | cut -d "(" -f2 | cut -d ")" -f1
                    – Matt Friedman
                    Jan 24 '15 at 3:08










                  • getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> works for declarations inside /etc/hosts, too ... and it's the go-to-tool for all kinds of system databases (passwd, group, aliases, services).
                    – 0xC0000022L
                    Apr 30 '15 at 7:53












                  • 2




                    The use of ping is what I needed as I need the value from the hosts file but the sed pattern parsing correctly but this worked ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/^[^(]*[(]//" | sed -e "s/[)].*$//"
                    – ManiacZX
                    Jan 18 '13 at 19:23







                  • 1




                    To resolve something on my home network like myhostname.local this works so for me this is the best answer.
                    – Matt Friedman
                    Jan 24 '15 at 2:59






                  • 1




                    May I suggest this also: ping -q -c 1 -t 1 bahface.local | grep -m 1 PING | cut -d "(" -f2 | cut -d ")" -f1
                    – Matt Friedman
                    Jan 24 '15 at 3:08










                  • getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> works for declarations inside /etc/hosts, too ... and it's the go-to-tool for all kinds of system databases (passwd, group, aliases, services).
                    – 0xC0000022L
                    Apr 30 '15 at 7:53







                  2




                  2




                  The use of ping is what I needed as I need the value from the hosts file but the sed pattern parsing correctly but this worked ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/^[^(]*[(]//" | sed -e "s/[)].*$//"
                  – ManiacZX
                  Jan 18 '13 at 19:23





                  The use of ping is what I needed as I need the value from the hosts file but the sed pattern parsing correctly but this worked ping -q -c 1 -t 1 your_host_here | grep PING | sed -e "s/^[^(]*[(]//" | sed -e "s/[)].*$//"
                  – ManiacZX
                  Jan 18 '13 at 19:23





                  1




                  1




                  To resolve something on my home network like myhostname.local this works so for me this is the best answer.
                  – Matt Friedman
                  Jan 24 '15 at 2:59




                  To resolve something on my home network like myhostname.local this works so for me this is the best answer.
                  – Matt Friedman
                  Jan 24 '15 at 2:59




                  1




                  1




                  May I suggest this also: ping -q -c 1 -t 1 bahface.local | grep -m 1 PING | cut -d "(" -f2 | cut -d ")" -f1
                  – Matt Friedman
                  Jan 24 '15 at 3:08




                  May I suggest this also: ping -q -c 1 -t 1 bahface.local | grep -m 1 PING | cut -d "(" -f2 | cut -d ")" -f1
                  – Matt Friedman
                  Jan 24 '15 at 3:08












                  getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> works for declarations inside /etc/hosts, too ... and it's the go-to-tool for all kinds of system databases (passwd, group, aliases, services).
                  – 0xC0000022L
                  Apr 30 '15 at 7:53




                  getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname> works for declarations inside /etc/hosts, too ... and it's the go-to-tool for all kinds of system databases (passwd, group, aliases, services).
                  – 0xC0000022L
                  Apr 30 '15 at 7:53










                  up vote
                  14
                  down vote













                  Simple but usefull:



                  1. getent ahostsv4 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

                  2. getent ahostsv6 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

                  3. getent hosts google.de | head -n 1 | cut -d ' ' -f 1

                  All commands will resolve an IP address if host still exist. If host points to CNAME it will also get the IP in that case.



                  The first command returns the resolved IPv4 address



                  The second command returns the resolved IPv6 address



                  The third command will return the owners preferred address what may IPv4 or IPv6 address.






                  share|improve this answer




















                  • By far the most simple one. And its avilable by default. Not like host that require install of the bindutils
                    – michaelbn
                    Oct 21 '15 at 6:45















                  up vote
                  14
                  down vote













                  Simple but usefull:



                  1. getent ahostsv4 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

                  2. getent ahostsv6 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

                  3. getent hosts google.de | head -n 1 | cut -d ' ' -f 1

                  All commands will resolve an IP address if host still exist. If host points to CNAME it will also get the IP in that case.



                  The first command returns the resolved IPv4 address



                  The second command returns the resolved IPv6 address



                  The third command will return the owners preferred address what may IPv4 or IPv6 address.






                  share|improve this answer




















                  • By far the most simple one. And its avilable by default. Not like host that require install of the bindutils
                    – michaelbn
                    Oct 21 '15 at 6:45













                  up vote
                  14
                  down vote










                  up vote
                  14
                  down vote









                  Simple but usefull:



                  1. getent ahostsv4 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

                  2. getent ahostsv6 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

                  3. getent hosts google.de | head -n 1 | cut -d ' ' -f 1

                  All commands will resolve an IP address if host still exist. If host points to CNAME it will also get the IP in that case.



                  The first command returns the resolved IPv4 address



                  The second command returns the resolved IPv6 address



                  The third command will return the owners preferred address what may IPv4 or IPv6 address.






                  share|improve this answer












                  Simple but usefull:



                  1. getent ahostsv4 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

                  2. getent ahostsv6 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1

                  3. getent hosts google.de | head -n 1 | cut -d ' ' -f 1

                  All commands will resolve an IP address if host still exist. If host points to CNAME it will also get the IP in that case.



                  The first command returns the resolved IPv4 address



                  The second command returns the resolved IPv6 address



                  The third command will return the owners preferred address what may IPv4 or IPv6 address.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 15 '14 at 14:42









                  Tom Freudenberg

                  24123




                  24123











                  • By far the most simple one. And its avilable by default. Not like host that require install of the bindutils
                    – michaelbn
                    Oct 21 '15 at 6:45

















                  • By far the most simple one. And its avilable by default. Not like host that require install of the bindutils
                    – michaelbn
                    Oct 21 '15 at 6:45
















                  By far the most simple one. And its avilable by default. Not like host that require install of the bindutils
                  – michaelbn
                  Oct 21 '15 at 6:45





                  By far the most simple one. And its avilable by default. Not like host that require install of the bindutils
                  – michaelbn
                  Oct 21 '15 at 6:45











                  up vote
                  5
                  down vote













                  Here is a slight variation of the ping approach that takes "unknown host" into account (by piping through stderr) and uses tr to avoid the use of sed regexps:



                  ping -c1 -t1 -W0 www.example.com 2>&1 | tr -d '():' | awk '/^PING/print $3'


                  In case it's important to capture the exit value, then the following will work (although less elegant):



                  ping -c1 -t1 -W0 www.example.com &>/dev/null && ping -c1 -t1 -W0 www.example.com 2>&1 | tr -d '():' | awk '/^PING/print $3'





                  share|improve this answer




















                  • I love this solution, because it works without any extra tools.
                    – Radon8472
                    Aug 9 at 8:14














                  up vote
                  5
                  down vote













                  Here is a slight variation of the ping approach that takes "unknown host" into account (by piping through stderr) and uses tr to avoid the use of sed regexps:



                  ping -c1 -t1 -W0 www.example.com 2>&1 | tr -d '():' | awk '/^PING/print $3'


                  In case it's important to capture the exit value, then the following will work (although less elegant):



                  ping -c1 -t1 -W0 www.example.com &>/dev/null && ping -c1 -t1 -W0 www.example.com 2>&1 | tr -d '():' | awk '/^PING/print $3'





                  share|improve this answer




















                  • I love this solution, because it works without any extra tools.
                    – Radon8472
                    Aug 9 at 8:14












                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  Here is a slight variation of the ping approach that takes "unknown host" into account (by piping through stderr) and uses tr to avoid the use of sed regexps:



                  ping -c1 -t1 -W0 www.example.com 2>&1 | tr -d '():' | awk '/^PING/print $3'


                  In case it's important to capture the exit value, then the following will work (although less elegant):



                  ping -c1 -t1 -W0 www.example.com &>/dev/null && ping -c1 -t1 -W0 www.example.com 2>&1 | tr -d '():' | awk '/^PING/print $3'





                  share|improve this answer












                  Here is a slight variation of the ping approach that takes "unknown host" into account (by piping through stderr) and uses tr to avoid the use of sed regexps:



                  ping -c1 -t1 -W0 www.example.com 2>&1 | tr -d '():' | awk '/^PING/print $3'


                  In case it's important to capture the exit value, then the following will work (although less elegant):



                  ping -c1 -t1 -W0 www.example.com &>/dev/null && ping -c1 -t1 -W0 www.example.com 2>&1 | tr -d '():' | awk '/^PING/print $3'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 4 '13 at 15:22









                  Stefan Farestam

                  15111




                  15111











                  • I love this solution, because it works without any extra tools.
                    – Radon8472
                    Aug 9 at 8:14
















                  • I love this solution, because it works without any extra tools.
                    – Radon8472
                    Aug 9 at 8:14















                  I love this solution, because it works without any extra tools.
                  – Radon8472
                  Aug 9 at 8:14




                  I love this solution, because it works without any extra tools.
                  – Radon8472
                  Aug 9 at 8:14










                  up vote
                  4
                  down vote













                  You could use host:



                  hostname=example.org

                  # strips the IP
                  IP=$( host $hostname | sed -e "s/.* //" )

                  # checks for errors
                  if [ $? -ne 0 ] ; then
                  echo "Error: cannot resolve $hostname" 1>&2
                  exit 1;
                  fi





                  share|improve this answer


























                    up vote
                    4
                    down vote













                    You could use host:



                    hostname=example.org

                    # strips the IP
                    IP=$( host $hostname | sed -e "s/.* //" )

                    # checks for errors
                    if [ $? -ne 0 ] ; then
                    echo "Error: cannot resolve $hostname" 1>&2
                    exit 1;
                    fi





                    share|improve this answer
























                      up vote
                      4
                      down vote










                      up vote
                      4
                      down vote









                      You could use host:



                      hostname=example.org

                      # strips the IP
                      IP=$( host $hostname | sed -e "s/.* //" )

                      # checks for errors
                      if [ $? -ne 0 ] ; then
                      echo "Error: cannot resolve $hostname" 1>&2
                      exit 1;
                      fi





                      share|improve this answer














                      You could use host:



                      hostname=example.org

                      # strips the IP
                      IP=$( host $hostname | sed -e "s/.* //" )

                      # checks for errors
                      if [ $? -ne 0 ] ; then
                      echo "Error: cannot resolve $hostname" 1>&2
                      exit 1;
                      fi






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Sep 15 '11 at 17:46

























                      answered Sep 15 '11 at 15:01









                      Matteo

                      6,55543556




                      6,55543556




















                          up vote
                          4
                          down vote













                          To complete Chris Down's answer, and address jfgagne comments about (possibly chained) aliases, here is a solution that :



                          • takes into account multiple IPs

                          • takes into account one or more aliases (CNAME)

                          • does not query /etc/hosts file (in my case I didn't want it); to query it, dbernt's python solution is perfect)


                          • does not use awk/sed



                            dig +short www.alias.com | grep -v ".$" | head -n 1


                          Always returns the first IP address, or empty tring if not resolved. with version of dig :



                           $ dig -v
                          DiG 9.8.1-P1





                          share|improve this answer




















                          • Thanks, other answers assume "dig +short" always returns a single ip address. They weren't accounting for CNAMEs.
                            – jamshid
                            May 10 '16 at 0:46














                          up vote
                          4
                          down vote













                          To complete Chris Down's answer, and address jfgagne comments about (possibly chained) aliases, here is a solution that :



                          • takes into account multiple IPs

                          • takes into account one or more aliases (CNAME)

                          • does not query /etc/hosts file (in my case I didn't want it); to query it, dbernt's python solution is perfect)


                          • does not use awk/sed



                            dig +short www.alias.com | grep -v ".$" | head -n 1


                          Always returns the first IP address, or empty tring if not resolved. with version of dig :



                           $ dig -v
                          DiG 9.8.1-P1





                          share|improve this answer




















                          • Thanks, other answers assume "dig +short" always returns a single ip address. They weren't accounting for CNAMEs.
                            – jamshid
                            May 10 '16 at 0:46












                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          To complete Chris Down's answer, and address jfgagne comments about (possibly chained) aliases, here is a solution that :



                          • takes into account multiple IPs

                          • takes into account one or more aliases (CNAME)

                          • does not query /etc/hosts file (in my case I didn't want it); to query it, dbernt's python solution is perfect)


                          • does not use awk/sed



                            dig +short www.alias.com | grep -v ".$" | head -n 1


                          Always returns the first IP address, or empty tring if not resolved. with version of dig :



                           $ dig -v
                          DiG 9.8.1-P1





                          share|improve this answer












                          To complete Chris Down's answer, and address jfgagne comments about (possibly chained) aliases, here is a solution that :



                          • takes into account multiple IPs

                          • takes into account one or more aliases (CNAME)

                          • does not query /etc/hosts file (in my case I didn't want it); to query it, dbernt's python solution is perfect)


                          • does not use awk/sed



                            dig +short www.alias.com | grep -v ".$" | head -n 1


                          Always returns the first IP address, or empty tring if not resolved. with version of dig :



                           $ dig -v
                          DiG 9.8.1-P1






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 30 '14 at 15:26









                          Franck

                          412




                          412











                          • Thanks, other answers assume "dig +short" always returns a single ip address. They weren't accounting for CNAMEs.
                            – jamshid
                            May 10 '16 at 0:46
















                          • Thanks, other answers assume "dig +short" always returns a single ip address. They weren't accounting for CNAMEs.
                            – jamshid
                            May 10 '16 at 0:46















                          Thanks, other answers assume "dig +short" always returns a single ip address. They weren't accounting for CNAMEs.
                          – jamshid
                          May 10 '16 at 0:46




                          Thanks, other answers assume "dig +short" always returns a single ip address. They weren't accounting for CNAMEs.
                          – jamshid
                          May 10 '16 at 0:46










                          up vote
                          3
                          down vote













                           php -r "echo gethostbyname('unix.stackexchange.com');"





                          share|improve this answer




















                          • this works, but it requires that php is installed on your terminal
                            – Radon8472
                            Aug 9 at 8:29






                          • 1




                            can be useful on a typical php docker container where "host", "dig" etc. are not available
                            – Fabian Schmengler
                            Sep 5 at 19:42














                          up vote
                          3
                          down vote













                           php -r "echo gethostbyname('unix.stackexchange.com');"





                          share|improve this answer




















                          • this works, but it requires that php is installed on your terminal
                            – Radon8472
                            Aug 9 at 8:29






                          • 1




                            can be useful on a typical php docker container where "host", "dig" etc. are not available
                            – Fabian Schmengler
                            Sep 5 at 19:42












                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote









                           php -r "echo gethostbyname('unix.stackexchange.com');"





                          share|improve this answer












                           php -r "echo gethostbyname('unix.stackexchange.com');"






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 29 '15 at 22:01









                          Mohammed Zourob

                          311




                          311











                          • this works, but it requires that php is installed on your terminal
                            – Radon8472
                            Aug 9 at 8:29






                          • 1




                            can be useful on a typical php docker container where "host", "dig" etc. are not available
                            – Fabian Schmengler
                            Sep 5 at 19:42
















                          • this works, but it requires that php is installed on your terminal
                            – Radon8472
                            Aug 9 at 8:29






                          • 1




                            can be useful on a typical php docker container where "host", "dig" etc. are not available
                            – Fabian Schmengler
                            Sep 5 at 19:42















                          this works, but it requires that php is installed on your terminal
                          – Radon8472
                          Aug 9 at 8:29




                          this works, but it requires that php is installed on your terminal
                          – Radon8472
                          Aug 9 at 8:29




                          1




                          1




                          can be useful on a typical php docker container where "host", "dig" etc. are not available
                          – Fabian Schmengler
                          Sep 5 at 19:42




                          can be useful on a typical php docker container where "host", "dig" etc. are not available
                          – Fabian Schmengler
                          Sep 5 at 19:42










                          up vote
                          3
                          down vote













                          nmap -sP 192.168.178.0/24|grep YOUR_HOSTNAME|sed -n 's/.*[(]([0-9.]*)[)].*/1/p'


                          was the solution I found without DNS server






                          share|improve this answer
























                            up vote
                            3
                            down vote













                            nmap -sP 192.168.178.0/24|grep YOUR_HOSTNAME|sed -n 's/.*[(]([0-9.]*)[)].*/1/p'


                            was the solution I found without DNS server






                            share|improve this answer






















                              up vote
                              3
                              down vote










                              up vote
                              3
                              down vote









                              nmap -sP 192.168.178.0/24|grep YOUR_HOSTNAME|sed -n 's/.*[(]([0-9.]*)[)].*/1/p'


                              was the solution I found without DNS server






                              share|improve this answer












                              nmap -sP 192.168.178.0/24|grep YOUR_HOSTNAME|sed -n 's/.*[(]([0-9.]*)[)].*/1/p'


                              was the solution I found without DNS server







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 14 '15 at 15:07









                              Philippe Gachoud

                              373210




                              373210




















                                  up vote
                                  3
                                  down vote













                                  I would have liked to add this as a comment to Andrew McGregor Re: ping. However it wouldn't let me, so I need to add this as another answer. (If somebody can move it into a comment, feel free to.)



                                  This is another variant, only using ping and grep:



                                  ping -q -c1 -t1 your_host_here | grep -Eo "([0-9]+.?)4"


                                  grep -E for extended regular expression and
                                  grep -o to return only the matching part.
                                  the regexp itself looks for one or multiple digits ([0-9]+) and optionally a dot (.?) four times (4)






                                  share|improve this answer
























                                    up vote
                                    3
                                    down vote













                                    I would have liked to add this as a comment to Andrew McGregor Re: ping. However it wouldn't let me, so I need to add this as another answer. (If somebody can move it into a comment, feel free to.)



                                    This is another variant, only using ping and grep:



                                    ping -q -c1 -t1 your_host_here | grep -Eo "([0-9]+.?)4"


                                    grep -E for extended regular expression and
                                    grep -o to return only the matching part.
                                    the regexp itself looks for one or multiple digits ([0-9]+) and optionally a dot (.?) four times (4)






                                    share|improve this answer






















                                      up vote
                                      3
                                      down vote










                                      up vote
                                      3
                                      down vote









                                      I would have liked to add this as a comment to Andrew McGregor Re: ping. However it wouldn't let me, so I need to add this as another answer. (If somebody can move it into a comment, feel free to.)



                                      This is another variant, only using ping and grep:



                                      ping -q -c1 -t1 your_host_here | grep -Eo "([0-9]+.?)4"


                                      grep -E for extended regular expression and
                                      grep -o to return only the matching part.
                                      the regexp itself looks for one or multiple digits ([0-9]+) and optionally a dot (.?) four times (4)






                                      share|improve this answer












                                      I would have liked to add this as a comment to Andrew McGregor Re: ping. However it wouldn't let me, so I need to add this as another answer. (If somebody can move it into a comment, feel free to.)



                                      This is another variant, only using ping and grep:



                                      ping -q -c1 -t1 your_host_here | grep -Eo "([0-9]+.?)4"


                                      grep -E for extended regular expression and
                                      grep -o to return only the matching part.
                                      the regexp itself looks for one or multiple digits ([0-9]+) and optionally a dot (.?) four times (4)







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 3 '16 at 17:31









                                      Dweia

                                      411




                                      411




















                                          up vote
                                          2
                                          down vote













                                          here's a Bash recipe I cooked up using other folk's answers — first tries /etc/hosts, then falls back to nslookup:



                                          resolveip() awk 'print $1' )
                                          if [ -z "$ip" ]
                                          then
                                          ip=$( dig +short "$host" )
                                          if [ -z "$ip" ]
                                          then
                                          echo "unable to resolve '$host'" >&2
                                          return 1
                                          else
                                          echo "$ip"
                                          return 0
                                          fi
                                          else
                                          echo "$ip"
                                          return 0
                                          fi
                                          fi






                                          share|improve this answer






















                                          • To be clear, getent hosts isn't just a lookup in /etc/hosts - it's a full-on DNS-resolving call to gethostbyaddr(3), and it's very unlikely to fail in a case where dig will succeed. See the man page for getent.
                                            – Stuart P. Bentley
                                            Aug 3 '14 at 11:42










                                          • @Stuart is right — i've learned a great deal since writing that and oversimplified a powerful command. getent remains my favorite, although i also like dig +short
                                            – RubyTuesdayDONO
                                            Aug 3 '14 at 18:26














                                          up vote
                                          2
                                          down vote













                                          here's a Bash recipe I cooked up using other folk's answers — first tries /etc/hosts, then falls back to nslookup:



                                          resolveip() awk 'print $1' )
                                          if [ -z "$ip" ]
                                          then
                                          ip=$( dig +short "$host" )
                                          if [ -z "$ip" ]
                                          then
                                          echo "unable to resolve '$host'" >&2
                                          return 1
                                          else
                                          echo "$ip"
                                          return 0
                                          fi
                                          else
                                          echo "$ip"
                                          return 0
                                          fi
                                          fi






                                          share|improve this answer






















                                          • To be clear, getent hosts isn't just a lookup in /etc/hosts - it's a full-on DNS-resolving call to gethostbyaddr(3), and it's very unlikely to fail in a case where dig will succeed. See the man page for getent.
                                            – Stuart P. Bentley
                                            Aug 3 '14 at 11:42










                                          • @Stuart is right — i've learned a great deal since writing that and oversimplified a powerful command. getent remains my favorite, although i also like dig +short
                                            – RubyTuesdayDONO
                                            Aug 3 '14 at 18:26












                                          up vote
                                          2
                                          down vote










                                          up vote
                                          2
                                          down vote









                                          here's a Bash recipe I cooked up using other folk's answers — first tries /etc/hosts, then falls back to nslookup:



                                          resolveip() awk 'print $1' )
                                          if [ -z "$ip" ]
                                          then
                                          ip=$( dig +short "$host" )
                                          if [ -z "$ip" ]
                                          then
                                          echo "unable to resolve '$host'" >&2
                                          return 1
                                          else
                                          echo "$ip"
                                          return 0
                                          fi
                                          else
                                          echo "$ip"
                                          return 0
                                          fi
                                          fi






                                          share|improve this answer














                                          here's a Bash recipe I cooked up using other folk's answers — first tries /etc/hosts, then falls back to nslookup:



                                          resolveip() awk 'print $1' )
                                          if [ -z "$ip" ]
                                          then
                                          ip=$( dig +short "$host" )
                                          if [ -z "$ip" ]
                                          then
                                          echo "unable to resolve '$host'" >&2
                                          return 1
                                          else
                                          echo "$ip"
                                          return 0
                                          fi
                                          else
                                          echo "$ip"
                                          return 0
                                          fi
                                          fi







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Jan 11 '14 at 3:51









                                          Anthon

                                          59k1796160




                                          59k1796160










                                          answered Jan 11 '14 at 1:16









                                          RubyTuesdayDONO

                                          1314




                                          1314











                                          • To be clear, getent hosts isn't just a lookup in /etc/hosts - it's a full-on DNS-resolving call to gethostbyaddr(3), and it's very unlikely to fail in a case where dig will succeed. See the man page for getent.
                                            – Stuart P. Bentley
                                            Aug 3 '14 at 11:42










                                          • @Stuart is right — i've learned a great deal since writing that and oversimplified a powerful command. getent remains my favorite, although i also like dig +short
                                            – RubyTuesdayDONO
                                            Aug 3 '14 at 18:26
















                                          • To be clear, getent hosts isn't just a lookup in /etc/hosts - it's a full-on DNS-resolving call to gethostbyaddr(3), and it's very unlikely to fail in a case where dig will succeed. See the man page for getent.
                                            – Stuart P. Bentley
                                            Aug 3 '14 at 11:42










                                          • @Stuart is right — i've learned a great deal since writing that and oversimplified a powerful command. getent remains my favorite, although i also like dig +short
                                            – RubyTuesdayDONO
                                            Aug 3 '14 at 18:26















                                          To be clear, getent hosts isn't just a lookup in /etc/hosts - it's a full-on DNS-resolving call to gethostbyaddr(3), and it's very unlikely to fail in a case where dig will succeed. See the man page for getent.
                                          – Stuart P. Bentley
                                          Aug 3 '14 at 11:42




                                          To be clear, getent hosts isn't just a lookup in /etc/hosts - it's a full-on DNS-resolving call to gethostbyaddr(3), and it's very unlikely to fail in a case where dig will succeed. See the man page for getent.
                                          – Stuart P. Bentley
                                          Aug 3 '14 at 11:42












                                          @Stuart is right — i've learned a great deal since writing that and oversimplified a powerful command. getent remains my favorite, although i also like dig +short
                                          – RubyTuesdayDONO
                                          Aug 3 '14 at 18:26




                                          @Stuart is right — i've learned a great deal since writing that and oversimplified a powerful command. getent remains my favorite, although i also like dig +short
                                          – RubyTuesdayDONO
                                          Aug 3 '14 at 18:26










                                          up vote
                                          2
                                          down vote













                                          Maybe not the most concise, but it seems to be robust and efficient:



                                          # $(get_host_dns_short "google.com")
                                          #
                                          # Outputs the IPv4 IP Address of a hostname, resolved by DNS. Returns 0 if DNS
                                          # responded successfully; 1 otherwise. Will mask error output.
                                          function get_host_dns_short()
                                          return 1



                                          This will output a single IPv4 IP, as well as return 1 in the event of failure, while masking stderr output.



                                          You can use it like this:



                                          GOOGLE_IP="$(get_host_dns_short "google.com")"
                                          if [[ $? -eq 0 ]]; then
                                          echo "Google's IP is $GOOGLE_IP."
                                          else
                                          echo "Failed to resolve Google's IP."
                                          fi



                                          Google's IP is 216.58.192.46.




                                          If you want an IPv6 address instead, just replace -4 with -6.






                                          share|improve this answer
























                                            up vote
                                            2
                                            down vote













                                            Maybe not the most concise, but it seems to be robust and efficient:



                                            # $(get_host_dns_short "google.com")
                                            #
                                            # Outputs the IPv4 IP Address of a hostname, resolved by DNS. Returns 0 if DNS
                                            # responded successfully; 1 otherwise. Will mask error output.
                                            function get_host_dns_short()
                                            return 1



                                            This will output a single IPv4 IP, as well as return 1 in the event of failure, while masking stderr output.



                                            You can use it like this:



                                            GOOGLE_IP="$(get_host_dns_short "google.com")"
                                            if [[ $? -eq 0 ]]; then
                                            echo "Google's IP is $GOOGLE_IP."
                                            else
                                            echo "Failed to resolve Google's IP."
                                            fi



                                            Google's IP is 216.58.192.46.




                                            If you want an IPv6 address instead, just replace -4 with -6.






                                            share|improve this answer






















                                              up vote
                                              2
                                              down vote










                                              up vote
                                              2
                                              down vote









                                              Maybe not the most concise, but it seems to be robust and efficient:



                                              # $(get_host_dns_short "google.com")
                                              #
                                              # Outputs the IPv4 IP Address of a hostname, resolved by DNS. Returns 0 if DNS
                                              # responded successfully; 1 otherwise. Will mask error output.
                                              function get_host_dns_short()
                                              return 1



                                              This will output a single IPv4 IP, as well as return 1 in the event of failure, while masking stderr output.



                                              You can use it like this:



                                              GOOGLE_IP="$(get_host_dns_short "google.com")"
                                              if [[ $? -eq 0 ]]; then
                                              echo "Google's IP is $GOOGLE_IP."
                                              else
                                              echo "Failed to resolve Google's IP."
                                              fi



                                              Google's IP is 216.58.192.46.




                                              If you want an IPv6 address instead, just replace -4 with -6.






                                              share|improve this answer












                                              Maybe not the most concise, but it seems to be robust and efficient:



                                              # $(get_host_dns_short "google.com")
                                              #
                                              # Outputs the IPv4 IP Address of a hostname, resolved by DNS. Returns 0 if DNS
                                              # responded successfully; 1 otherwise. Will mask error output.
                                              function get_host_dns_short()
                                              return 1



                                              This will output a single IPv4 IP, as well as return 1 in the event of failure, while masking stderr output.



                                              You can use it like this:



                                              GOOGLE_IP="$(get_host_dns_short "google.com")"
                                              if [[ $? -eq 0 ]]; then
                                              echo "Google's IP is $GOOGLE_IP."
                                              else
                                              echo "Failed to resolve Google's IP."
                                              fi



                                              Google's IP is 216.58.192.46.




                                              If you want an IPv6 address instead, just replace -4 with -6.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Aug 4 '15 at 19:47









                                              Will

                                              1,451722




                                              1,451722




















                                                  up vote
                                                  2
                                                  down vote













                                                  dig +noall +answer +nocomments example.com | awk 'printf "%-36st%sn", $1, $5 '






                                                  share|improve this answer


















                                                  • 1




                                                    Some context in what way that answer improves over the already existing ones would be great. Also, please indent commands by 4 spaces (cf. the markdown syntax).
                                                    – maxschlepzig
                                                    Mar 17 '16 at 21:14














                                                  up vote
                                                  2
                                                  down vote













                                                  dig +noall +answer +nocomments example.com | awk 'printf "%-36st%sn", $1, $5 '






                                                  share|improve this answer


















                                                  • 1




                                                    Some context in what way that answer improves over the already existing ones would be great. Also, please indent commands by 4 spaces (cf. the markdown syntax).
                                                    – maxschlepzig
                                                    Mar 17 '16 at 21:14












                                                  up vote
                                                  2
                                                  down vote










                                                  up vote
                                                  2
                                                  down vote









                                                  dig +noall +answer +nocomments example.com | awk 'printf "%-36st%sn", $1, $5 '






                                                  share|improve this answer














                                                  dig +noall +answer +nocomments example.com | awk 'printf "%-36st%sn", $1, $5 '







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Mar 17 '16 at 22:29









                                                  MelBurslan

                                                  5,16511433




                                                  5,16511433










                                                  answered Mar 17 '16 at 21:03









                                                  D P Baker

                                                  211




                                                  211







                                                  • 1




                                                    Some context in what way that answer improves over the already existing ones would be great. Also, please indent commands by 4 spaces (cf. the markdown syntax).
                                                    – maxschlepzig
                                                    Mar 17 '16 at 21:14












                                                  • 1




                                                    Some context in what way that answer improves over the already existing ones would be great. Also, please indent commands by 4 spaces (cf. the markdown syntax).
                                                    – maxschlepzig
                                                    Mar 17 '16 at 21:14







                                                  1




                                                  1




                                                  Some context in what way that answer improves over the already existing ones would be great. Also, please indent commands by 4 spaces (cf. the markdown syntax).
                                                  – maxschlepzig
                                                  Mar 17 '16 at 21:14




                                                  Some context in what way that answer improves over the already existing ones would be great. Also, please indent commands by 4 spaces (cf. the markdown syntax).
                                                  – maxschlepzig
                                                  Mar 17 '16 at 21:14










                                                  up vote
                                                  1
                                                  down vote













                                                  1 line resolve a list of hostname



                                                  for LINE in `cat ~/Desktop/mylist`; do a=$(nslookup $LINE | awk '/^Address: / print $1 '); echo $a >> ~/Desktop/ip; done





                                                  share|improve this answer
























                                                    up vote
                                                    1
                                                    down vote













                                                    1 line resolve a list of hostname



                                                    for LINE in `cat ~/Desktop/mylist`; do a=$(nslookup $LINE | awk '/^Address: / print $1 '); echo $a >> ~/Desktop/ip; done





                                                    share|improve this answer






















                                                      up vote
                                                      1
                                                      down vote










                                                      up vote
                                                      1
                                                      down vote









                                                      1 line resolve a list of hostname



                                                      for LINE in `cat ~/Desktop/mylist`; do a=$(nslookup $LINE | awk '/^Address: / print $1 '); echo $a >> ~/Desktop/ip; done





                                                      share|improve this answer












                                                      1 line resolve a list of hostname



                                                      for LINE in `cat ~/Desktop/mylist`; do a=$(nslookup $LINE | awk '/^Address: / print $1 '); echo $a >> ~/Desktop/ip; done






                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Mar 23 '17 at 9:59









                                                      Andy Wong

                                                      112




                                                      112




















                                                          up vote
                                                          1
                                                          down vote













                                                          I am doing this all the time on my Mac which does not have getent. ping seems like a hack. I would like to take /etc/hosts into account as well.



                                                          So, I wrote a stupid wrapper for dns.lookup for you who have Node.js installed to provide a CLI:



                                                          $ npm install -g lookup-hostname
                                                          $ lookup google.com
                                                          62.243.192.89





                                                          share|improve this answer
























                                                            up vote
                                                            1
                                                            down vote













                                                            I am doing this all the time on my Mac which does not have getent. ping seems like a hack. I would like to take /etc/hosts into account as well.



                                                            So, I wrote a stupid wrapper for dns.lookup for you who have Node.js installed to provide a CLI:



                                                            $ npm install -g lookup-hostname
                                                            $ lookup google.com
                                                            62.243.192.89





                                                            share|improve this answer






















                                                              up vote
                                                              1
                                                              down vote










                                                              up vote
                                                              1
                                                              down vote









                                                              I am doing this all the time on my Mac which does not have getent. ping seems like a hack. I would like to take /etc/hosts into account as well.



                                                              So, I wrote a stupid wrapper for dns.lookup for you who have Node.js installed to provide a CLI:



                                                              $ npm install -g lookup-hostname
                                                              $ lookup google.com
                                                              62.243.192.89





                                                              share|improve this answer












                                                              I am doing this all the time on my Mac which does not have getent. ping seems like a hack. I would like to take /etc/hosts into account as well.



                                                              So, I wrote a stupid wrapper for dns.lookup for you who have Node.js installed to provide a CLI:



                                                              $ npm install -g lookup-hostname
                                                              $ lookup google.com
                                                              62.243.192.89






                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered May 14 '17 at 0:16









                                                              Thomas Jensen

                                                              1112




                                                              1112




















                                                                  up vote
                                                                  1
                                                                  down vote













                                                                  dig is too slow, nslookup is much faster



                                                                  nslookup google.com | grep -Po 'Address:s*[0-9.]+' | tail -1 | sed -e 's/Address:s*//g'





                                                                  share|improve this answer
























                                                                    up vote
                                                                    1
                                                                    down vote













                                                                    dig is too slow, nslookup is much faster



                                                                    nslookup google.com | grep -Po 'Address:s*[0-9.]+' | tail -1 | sed -e 's/Address:s*//g'





                                                                    share|improve this answer






















                                                                      up vote
                                                                      1
                                                                      down vote










                                                                      up vote
                                                                      1
                                                                      down vote









                                                                      dig is too slow, nslookup is much faster



                                                                      nslookup google.com | grep -Po 'Address:s*[0-9.]+' | tail -1 | sed -e 's/Address:s*//g'





                                                                      share|improve this answer












                                                                      dig is too slow, nslookup is much faster



                                                                      nslookup google.com | grep -Po 'Address:s*[0-9.]+' | tail -1 | sed -e 's/Address:s*//g'






                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Nov 14 '17 at 3:40









                                                                      Andrey Izman

                                                                      1112




                                                                      1112




















                                                                          up vote
                                                                          1
                                                                          down vote













                                                                          I don't know the easiest way for a bash-script but if you want to resolve a hostname and see if the host is up, use ping!



                                                                          ping -a hostname -c 1


                                                                          Will ping the host one time and resolve the hostname to IP-address.



                                                                          $ ping -a www.google.com -c 1
                                                                          PING www.google.com (216.58.211.132) 56(84) bytes of data.
                                                                          64 bytes from arn09s10-in-f4.1e100.net (216.58.211.132): icmp_seq=1 ttl=54 time=1.51 ms





                                                                          share|improve this answer






















                                                                          • using ping is good, because everybody has it, but you need to to filter the IP-Part from the outputs, if you like to use it in a script.
                                                                            – Radon8472
                                                                            Aug 9 at 8:27














                                                                          up vote
                                                                          1
                                                                          down vote













                                                                          I don't know the easiest way for a bash-script but if you want to resolve a hostname and see if the host is up, use ping!



                                                                          ping -a hostname -c 1


                                                                          Will ping the host one time and resolve the hostname to IP-address.



                                                                          $ ping -a www.google.com -c 1
                                                                          PING www.google.com (216.58.211.132) 56(84) bytes of data.
                                                                          64 bytes from arn09s10-in-f4.1e100.net (216.58.211.132): icmp_seq=1 ttl=54 time=1.51 ms





                                                                          share|improve this answer






















                                                                          • using ping is good, because everybody has it, but you need to to filter the IP-Part from the outputs, if you like to use it in a script.
                                                                            – Radon8472
                                                                            Aug 9 at 8:27












                                                                          up vote
                                                                          1
                                                                          down vote










                                                                          up vote
                                                                          1
                                                                          down vote









                                                                          I don't know the easiest way for a bash-script but if you want to resolve a hostname and see if the host is up, use ping!



                                                                          ping -a hostname -c 1


                                                                          Will ping the host one time and resolve the hostname to IP-address.



                                                                          $ ping -a www.google.com -c 1
                                                                          PING www.google.com (216.58.211.132) 56(84) bytes of data.
                                                                          64 bytes from arn09s10-in-f4.1e100.net (216.58.211.132): icmp_seq=1 ttl=54 time=1.51 ms





                                                                          share|improve this answer














                                                                          I don't know the easiest way for a bash-script but if you want to resolve a hostname and see if the host is up, use ping!



                                                                          ping -a hostname -c 1


                                                                          Will ping the host one time and resolve the hostname to IP-address.



                                                                          $ ping -a www.google.com -c 1
                                                                          PING www.google.com (216.58.211.132) 56(84) bytes of data.
                                                                          64 bytes from arn09s10-in-f4.1e100.net (216.58.211.132): icmp_seq=1 ttl=54 time=1.51 ms






                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          edited Nov 25 '17 at 17:33









                                                                          Philip Kirkbride

                                                                          2,3172671




                                                                          2,3172671










                                                                          answered Oct 27 '17 at 6:12









                                                                          user257655

                                                                          111




                                                                          111











                                                                          • using ping is good, because everybody has it, but you need to to filter the IP-Part from the outputs, if you like to use it in a script.
                                                                            – Radon8472
                                                                            Aug 9 at 8:27
















                                                                          • using ping is good, because everybody has it, but you need to to filter the IP-Part from the outputs, if you like to use it in a script.
                                                                            – Radon8472
                                                                            Aug 9 at 8:27















                                                                          using ping is good, because everybody has it, but you need to to filter the IP-Part from the outputs, if you like to use it in a script.
                                                                          – Radon8472
                                                                          Aug 9 at 8:27




                                                                          using ping is good, because everybody has it, but you need to to filter the IP-Part from the outputs, if you like to use it in a script.
                                                                          – Radon8472
                                                                          Aug 9 at 8:27










                                                                          up vote
                                                                          -1
                                                                          down vote













                                                                          host -t a cisco.com


                                                                          this command will show ip address ( will reslove domain to IP )






                                                                          share|improve this answer


























                                                                            up vote
                                                                            -1
                                                                            down vote













                                                                            host -t a cisco.com


                                                                            this command will show ip address ( will reslove domain to IP )






                                                                            share|improve this answer
























                                                                              up vote
                                                                              -1
                                                                              down vote










                                                                              up vote
                                                                              -1
                                                                              down vote









                                                                              host -t a cisco.com


                                                                              this command will show ip address ( will reslove domain to IP )






                                                                              share|improve this answer














                                                                              host -t a cisco.com


                                                                              this command will show ip address ( will reslove domain to IP )







                                                                              share|improve this answer














                                                                              share|improve this answer



                                                                              share|improve this answer








                                                                              edited Sep 27 at 19:23









                                                                              Jeff Schaller

                                                                              33.6k851113




                                                                              33.6k851113










                                                                              answered Sep 27 at 16:55









                                                                              aaa

                                                                              1




                                                                              1



























                                                                                   

                                                                                  draft saved


                                                                                  draft discarded















































                                                                                   


                                                                                  draft saved


                                                                                  draft discarded














                                                                                  StackExchange.ready(
                                                                                  function ()
                                                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f20784%2fhow-can-i-resolve-a-hostname-to-an-ip-address-in-a-bash-script%23new-answer', 'question_page');

                                                                                  );

                                                                                  Post as a guest













































































                                                                                  Popular posts from this blog

                                                                                  How to check contact read email or not when send email to Individual?

                                                                                  Bahrain

                                                                                  Postfix configuration issue with fips on centos 7; mailgun relay