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

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

    We can simply use only 2 commands ( ifconfig + awk ) to get just the IP (v4) we want like so:

    On Linux, assuming to get IP address from eth0 interface, run the following command:

    /sbin/ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'
    

    On OSX, assumming to get IP affffdress from en0 interface, run the following command:

    /sbin/ifconfig en0 | awk '/inet /{print $2}'
    

    To know our public/external IP, add this function in ~/.bashrc

    whatismyip () {
        curl -s "http://api.duckduckgo.com/?q=ip&format=json" | jq '.Answer' | grep --color=auto -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
    }
    

    Then run, whatismyip

提交回复
热议问题