Bash script to get all IP addresses

后端 未结 12 2279
-上瘾入骨i
-上瘾入骨i 2020-12-28 22:51

I am trying to write a bash script to get all IP addresses on a server. The script should work on all major distros. Here is what I have:

ifconf         


        
12条回答
  •  礼貌的吻别
    2020-12-28 23:13

    ifconfig was obsoleted by ip. It also has the flag -o that write outputs easy to parse. Use ip -4 to show only IPV4 addresses. Note the simpler script, it already exclude the loopback address:

    ip -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {print $2" "$4}'
    

    Or if you don't want the networks:

    ip -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {gsub("/", " "); print $2" "$4}'
    

提交回复
热议问题