Bash script to get all IP addresses

后端 未结 12 2423
-上瘾入骨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:12

    I'm always surprised to see people using sed and awk instead of perl.

    But first, using both grep and awk with an extra option, feel free to just:

    ifconfig | grep 'inet addr:' | awk {'print $2'} | awk -F: {'print $2'} | grep -v '127.0.0.1'
    

    replacing the awks with perl:

    ifconfig | grep 'inet addr:' | perl -F\\s\|: -ane 'print "$F[2]\n"' | grep -v '127.0.0.1'
    

    replacing the greps within the same perl script:

    ifconfig | perl -F\\s\|: -ane 'next if !/^inet addr:/ or /127\.0\.0\.1/; print "$F[2]\n"'
    

    and lastly just using the power of perl's regex:

    ifconfig | perl -ne 'next if !/inet addr:(?[0-9.]+)/ or $+{ip} == "127.0.0.1"; print "$+{ip}\n"'
    

提交回复
热议问题