美文网首页
Linux Shell脚本中获取本机IP地址

Linux Shell脚本中获取本机IP地址

作者: JeffreyST | 来源:发表于2018-07-19 16:32 被阅读0次

    1. 获取本机IP地址

    参考链接:Click for more information

    ip -o -4 addr show up primary scope global | grep -E '10.254|10.144' | awk '{print $2,$4}'

    输出结果可能像下面这样

    [root@xxx ~]# ip -o -4 addr show up primary scope global | awk '{print $2,$4}'
    bond0 10.254.5.45/24
    bond1.768 10.141.0.243/24
    bond1.769 10.141.1.243/24
    virbr0 192.168.122.1/24
    docker0 172.17.0.1/16
    
    • -o uses the one-line output format, which is easier to process with read, grep, etc.

    • -4 for IPV4 address

    • up excludes devices that aren't active

    • scope global excludes private/local addresses such as 127.0.0.1 and fe80::/64

    • primary excludes temporary addresses (assuming you want an address that doesn't change)

    • 支持多块网卡设备

    相关文章

      网友评论

          本文标题:Linux Shell脚本中获取本机IP地址

          本文链接:https://www.haomeiwen.com/subject/bibtmftx.html