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

后端 未结 29 1282
春和景丽
春和景丽 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:49

    I always wind up needing this at the most unexpected times and, without fail, wind up searching for threads like this on SO. So I wrote a simple script to get IPv4 addresses via netstat, called echoip - you can find it here. The bash for network addresses looks like this, it also gets your public address from ipecho.net:

    IPV4='\d+(\.\d+){3}'
    INTERFACES=`netstat -i | grep -E "$IPV4" | cut -d ' ' -f 1`
    INTERFACE_IPS=`netstat -i | grep -oE "$IPV4"`
    
    for i in "${!INTERFACES[@]}"; do
      printf "%s:\t%s\n" "${INTERFACES[$i]}" "${INTERFACE_IPS[$i]}"
    done
    

    The echoip script yields an output like this:

    $ echoip
    public: 26.106.59.169
    en0:    10.1.10.2
    

提交回复
热议问题