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

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

    This will give you all IPv4 interfaces, including the loopback 127.0.0.1:

    ip -4 addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
    

    This will only show eth0:

    ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
    

    And this way you can get IPv6 addresses:

    ip -6 addr | grep -oP '(?<=inet6\s)[\da-f:]+'
    

    Only eth0 IPv6:

    ip -6 addr show eth0 | grep -oP '(?<=inet6\s)[\da-f:]+'
    

提交回复
热议问题