Bash script to get all IP addresses

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

    I would to introduce you to a command-line tool called OSQuery by Facebook which helps you get system info by making SQL-like queries. For your case for instance, you would have enter;

    osquery> select * from interface_addresses; 
    

    Which would output something like;

    interface = wlan0
           address = 192.168.0.101
              mask = 255.255.255.0
         broadcast = 192.168.0.255
    point_to_point = 
    

    Which I find a lot more neat and convenient.

    0 讨论(0)
  • 2020-12-28 23:26
    ifconfig | grep 'inet addr:' | awk {'print $2'} | cut -d ":" -f 2
    
    0 讨论(0)
  • if it is only the "addr:" word that you'd like to remove I would use sed instead of awk like

    ifconfig | grep 'inet addr:' | awk {'print $2'}| grep -v 127.0.0.1 | sed -e 's/addr://'
    
    0 讨论(0)
  • 2020-12-28 23:29

    ifconfig | grep 'inet addr:' | awk {'print $2'} | awk 'BEGIN{FS=":"}{print $2}' | grep -v '127.0.0.1'

    0 讨论(0)
  • 2020-12-28 23:32

    There's no need for grep. Here's one way using awk:

    List only addr:

    ifconfig | awk -F "[: ]+" '/inet addr:/ { if ($4 != "127.0.0.1") print $4 }'
    

    List device and addr:

    ifconfig | awk -v RS="\n\n" '{ for (i=1; i<=NF; i++) if ($i == "inet" && $(i+1) ~ /^addr:/) address = substr($(i+1), 6); if (address != "127.0.0.1") printf "%s\t%s\n", $1, address }'
    
    0 讨论(0)
  • 2020-12-28 23:35

    Here is a similiar script for what I have tested to get an ip-range of addresses, but it is slowly - somebody might give a hint, how to accelerate this ? (the ip-range here is an example for to get all lines, who seems to be up - between Vancouver and Korea) :

    #!/bin/bash

    for ip in {209..210}.{125..206}.{5..231}.{65..72} # any ip between 209.126.230.71 and 210.205.6.66

    do

    printf ' === %s ===\n' "$ip"

    whois "$ip" >> /home/$user/test001.txt

    done

    If this is too trivial or some mistake in it here, simply answer or comment.

    This script would last until finish about 5 to 8 hours.

    0 讨论(0)
提交回复
热议问题