📜  变量中的 ip 地址 - Shell-Bash 代码示例

📅  最后修改于: 2022-03-11 14:49:44.331000             🧑  作者: Mango

代码示例1
# The original web page offers a few alternatives. This one seems to be easy to understand, replicate and customize
# It will grab the first non 'localhost' and non 'ipv6' address
IP=$(ifconfig | grep -e 'inet' | grep -v 'inet6' | grep -v '127.0.0.1' | head -n 1 | awk '{print $2}')


# However, sometime it returns a 192.168.x.x type of address.
# In which case, this will return the public IP
IP=$(ip route get 8.8.8.8 | awk '{print $3}')