Which terminal command to get just IP address and nothing else?

后端 未结 29 1089
春和景丽
春和景丽 2020-12-04 06:16

I\'m trying to use just the IP address (inet) as a parameter in a script I wrote.

Is there an easy way in a unix terminal to get just the IP address, rather than loo

相关标签:
29条回答
  • 2020-12-04 06:24

    Generally, it is never guaranteed that a system will only have one IP address, for example, you can have both an ethernet and wlan connections, and if you have an active VPN connection then you'll have yet another IP address.

    Linux

    On Linux, hostname -I will list the current IP address(es). Relying on it always returning just one IP address will most likely not work as expected under some scenarios (i.e. a VPN link is up), so a more reliable way would be converting the result to an array and then loop over the elements:

    ips=($(hostname -I))
    
    for ip in "${ips[@]}"
    do
        echo $ip
    done
    

    OSX

    On OSX, if you know the interface, you could use:

    ~$ ipconfig getifaddr en0
    192.168.1.123
    

    which will return just the IP address.

    Or you could loop over possible interface names, starting with a suffix, i.e. en:

    for NUMBER in $(seq 0 5); do
        ip=`ipconfig getifaddr en$NUMBER`
        if [ -n "$ip" ]; then
            myip="$ip"
            break
        fi
    done
    
    echo $myip
    

    Also, getting the IP address becomes non-deterministic in case both a cable and wifi connections are established, when a machine has more than one ethernet interfaces, or when VPN tunnels are present.

    Getting the external IP

    If you need the external IP, then you can query a text-mode service, for example curl https://ipecho.net/plain would return a plain text external IP.

    And an even faster way to get the external IP is to query a known DNS server:

    dig @ns1-1.akamaitech.net ANY whoami.akamai.net +short
    
    0 讨论(0)
  • 2020-12-04 06:24

    I don't see any answer with nmcli yet which is a command-line tool for controlling NetworkManager.

    So here you go :)

    wolf@linux:~$ nmcli device 
    DEVICE  TYPE      STATE        CONNECTION 
    eth1    ethernet  unavailable  --         
    eth0    ethernet  unmanaged    --         
    lo      loopback  unmanaged    --         
    wolf@linux:~$ 
    

    If you want to get the information from specific network interface (let say lo for this example)

    wolf@linux:~$ nmcli device show lo
    GENERAL.DEVICE:                         lo
    GENERAL.TYPE:                           loopback
    GENERAL.HWADDR:                         00:00:00:00:00:00
    GENERAL.MTU:                            65536
    GENERAL.STATE:                          10 (unmanaged)
    GENERAL.CONNECTION:                     --
    GENERAL.CON-PATH:                       --
    IP4.ADDRESS[1]:                         127.0.0.1/8
    IP4.GATEWAY:                            --
    IP4.ROUTE[1]:                           dst = 127.0.0.0/8, nh = 0.0.0.0,>
    IP4.ROUTE[2]:                           dst = 127.0.0.1/32, nh = 0.0.0.0>
    IP6.ADDRESS[1]:                         ::1/128
    IP6.GATEWAY:                            --
    IP6.ROUTE[1]:                           dst = ::1/128, nh = ::, mt = 256
    IP6.ROUTE[2]:                           dst = ::1/128, nh = ::, mt = 0, >
    wolf@linux:~$ 
    

    But since you just want to get the IP address, just send the output to grep, cut or awk.

    Let's do it step by step. (Not sure what's wrong, the code sample format just didn't work for these 3 example.)

    1. Get the IPv4 line

      wolf@linux:~$ nmcli device show lo | grep 4.A IP4.ADDRESS[1]: 127.0.0.1/8 wolf@linux:~$

    2. Use awk to get the IP

      wolf@linux:~$ nmcli device show lo | awk '/4.A/ {print $2}' 127.0.0.1/8 wolf@linux:~$

    3. Use cut to remove the CIDR notation (/8)

      wolf@linux:~$ nmcli device show lo | awk '/4.A/ {print $2}' | cut -d / -f1 127.0.0.1 wolf@linux:~$

    There your answer.

    Please take note that there are tons of ways to do it using the tools that I demonstrated just now.

    Let's recap the commands that I used.

    nmcli device show lo | grep 4.A
    nmcli device show lo | awk '/4.A/ {print $2}'
    nmcli device show lo | awk '/4.A/ {print $2}' | cut -d / -f1
    

    Sample output for these 3 commands

    Command 1 output

    IP4.ADDRESS[1]:                         127.0.0.1/8
    

    Command 2 output

    127.0.0.1/8
    

    Command 3 output

    127.0.0.1
    
    0 讨论(0)
  • 2020-12-04 06:26

    These two ways worked for me:

    To get IP address of your interface eth0. Replace eth0 in the below example with your interface name. ifconfig eth0 | grep -w "inet" | tr -s " " | cut -f3 -d" "

    Using hostname: This will give you the inet i.e. IPAddress of your etho. using -I will give you inet value of all the interfaces whereever this value is present.

    hostname -i

    0 讨论(0)
  • 2020-12-04 06:27

    When looking up your external IP address on a NATed host, quite a few answers suggest using HTTP based methods like ifconfig.me eg:

    $ curl ifconfig.me/ip
    

    Over the years I have seen many of these sites come and go, I find this DNS based method more robust:

    $ dig +short myip.opendns.com @resolver1.opendns.com
    

    I have this handy alias in my ~/.bashrc:

    alias wip='dig +short myip.opendns.com @resolver1.opendns.com'
    
    0 讨论(0)
  • 2020-12-04 06:31

    To get only the IP address on Mac OS X you can type the following command:

    ipconfig getifaddr en0
    
    0 讨论(0)
  • 2020-12-04 06:31

    I prefer not to use awk and such in scripts.. ip has the option to output in JSON.

    If you leave out $interface then you get all of the ip addresses:

    ip -json addr show $interface | \
      jq -r '.[] | .addr_info[] | select(.family == "inet") | .local'
    
    0 讨论(0)
提交回复
热议问题