Send a ping to each IP on a subnet

后端 未结 13 1183
时光说笑
时光说笑 2020-12-07 10:21

Is there a command line based way to send pings to each computer in a subnet? Like

for(int i = 1; i < 254; i++)
    ping(192.168.1.i);

t

相关标签:
13条回答
  • 2020-12-07 10:32

    Not all machines have nmap available, but it's a wonderful tool for any network discovery, and certainly better than iterating through independent ping commands.

    $ nmap -n -sP 10.0.0.0/24
    
    Starting Nmap 4.20 ( http://insecure.org ) at 2009-02-02 07:41 CST
    Host 10.0.0.1 appears to be up.
    Host 10.0.0.10 appears to be up.
    Host 10.0.0.104 appears to be up.
    Host 10.0.0.124 appears to be up.
    Host 10.0.0.125 appears to be up.
    Host 10.0.0.129 appears to be up.
    Nmap finished: 256 IP addresses (6 hosts up) scanned in 2.365 seconds
    
    0 讨论(0)
  • 2020-12-07 10:33

    I would suggest the use of fping with the mask option, since you are not restricting yourself in ping.

    fping -g 192.168.1.0/24
    

    The response will be easy to parse in a script:

    192.168.1.1 is alive
    192.168.1.2 is alive
    192.168.1.3 is alive
    192.168.1.5 is alive
    ...
    192.168.1.4 is unreachable
    192.168.1.6 is unreachable
    192.168.1.7 is unreachable
    ...
    

    Note: Using the argument -a will restrict the output to reachable ip addresses, you may want to use it otherwise fping will also print unreachable addresses:

    fping -a -g 192.168.1.0/24
    

    From man:

    fping differs from ping in that you can specify any number of targets on the command line, or specify a file containing the lists of targets to ping. Instead of sending to one target until it times out or replies, fping will send out a ping packet and move on to the next target in a round-robin fashion.

    More info: http://fping.org/

    0 讨论(0)
  • 2020-12-07 10:35

    I just came around this question, but the answers did not satisfy me. So i rolled my own:

    echo $(seq 254) | xargs -P255 -I% -d" " ping -W 1 -c 1 192.168.0.% | grep -E "[0-1].*?:"
    
    • Advantage 1: You don't need to install any additional tool
    • Advantage 2: It's fast. It does everything in Parallel with a timout for every ping of 1s ("-W 1"). So it will finish in 1s :)
    • Advantage 3: The output is like this
    64 bytes from 192.168.0.16: icmp_seq=1 ttl=64 time=0.019 ms
    64 bytes from 192.168.0.12: icmp_seq=1 ttl=64 time=1.78 ms
    64 bytes from 192.168.0.21: icmp_seq=1 ttl=64 time=2.43 ms
    64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=1.97 ms
    64 bytes from 192.168.0.11: icmp_seq=1 ttl=64 time=619 ms
    

    Edit: And here is the same as script, for when your xargs do not have the -P flag, as is the case in openwrt (i just found out)

    for i in $(seq 255);
    do
     ping -W 1 -c 1 10.0.0.$i | grep 'from' &
    done
    
    0 讨论(0)
  • 2020-12-07 10:35

    The command line utility nmap can do this too:

    nmap -sP 192.168.1.*
    
    0 讨论(0)
  • 2020-12-07 10:35
    for i in $(seq 1 254); do ping -c1 -t 1 192.168.11.$i; done
    

    Adding a -t 1 waits only one second before exiting. This improves the speed a lot if you just have a few devices connected to that subnet.

    0 讨论(0)
  • 2020-12-07 10:36

    Broadcast ping:

    $ ping 192.168.1.255
    PING 192.168.1.255 (192.168.1.255): 56 data bytes
    64 bytes from 192.168.1.154: icmp_seq=0 ttl=64 time=0.104 ms
    64 bytes from 192.168.1.51: icmp_seq=0 ttl=64 time=2.058 ms (DUP!)
    64 bytes from 192.168.1.151: icmp_seq=0 ttl=64 time=2.135 ms (DUP!)
    ...
    

    (Add a -b option on Linux)

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