Send a ping to each IP on a subnet
Asked Answered
E

16

70

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);

to enforce arp resolution?

Eurythermal answered 2/2, 2009 at 13:16 Comment(2)
Check if this blog post has what you need.Alvertaalves
for i in $(seq 1 254); do ping -c1 -t 1 192.168.11.$i; done - its Native, without third party tools.Ob
G
79

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/

Garbers answered 17/8, 2015 at 14:11 Comment(2)
As well fping is way lighter than nmapAda
Use -aq if you only want to see the list of reachable addresses, and not individual ping results.Cardinal
Q
129

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
Quarrel answered 2/2, 2009 at 13:48 Comment(5)
Best answer to date because it is the first to be compatible with the reality that not all subnets are the same size, and using the /24 notation can be generalized to any size subnet.Almonte
The nmap it's not available in all machines/servers while the ping it's. That small issue make this option not to be the best answer. But if both tools are present this can be the chosen one.Luci
This command may lost some hosts, but these can be ping, why?Thanks
Could be just nmap -sn 10.0.0.0/24? There'll be no DNS lookup (-n) and from nmap manual In previous releases of Nmap, -sn was known as -sP.. @Thanks some hosts are configured to no answer pings.Corder
This is much faster than pingMorvin
G
79

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/

Garbers answered 17/8, 2015 at 14:11 Comment(2)
As well fping is way lighter than nmapAda
Use -aq if you only want to see the list of reachable addresses, and not individual ping results.Cardinal
C
49

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)

Cleodal answered 2/2, 2009 at 13:20 Comment(2)
note: you may need to add a "-b" in there depending on version/platformHorseflesh
Also, not all operating systems will respond to a broadcast ping (by default).Horseflesh
J
20

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
Judicator answered 23/2, 2016 at 14:11 Comment(0)
N
18

In Bash shell:

#!/bin/sh

COUNTER=1

while [ $COUNTER -lt 254 ]
do
   ping 192.168.1.$COUNTER -c 1
   COUNTER=$(( $COUNTER + 1 ))
done
Nonentity answered 2/2, 2009 at 13:21 Comment(1)
Shouldn't this be $COUNTER -lt 255 to test 192.168.1.254?Vibrant
L
10

The command line utility nmap can do this too:

nmap -sP 192.168.1.*
Longo answered 2/2, 2009 at 13:48 Comment(1)
You'll want to put the parameter in quotes.Skeet
S
7
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.

Sirloin answered 27/11, 2016 at 16:9 Comment(2)
Excellent. 1) Its Native 2) i do not have to install all those crap tools in Arduino.Ob
I also love its use of bare tools found on limited systems too. For OpenWRT users, keep in mind that -t parameter becomes -w for ping.Burrstone
L
6
FOR /L %i in (1,1,254) DO PING 192.168.1.%i -n 1 -w 100 | for /f "tokens=3 delims=: " %j in ('find /i "TTL="') do echo %j>>IPsOnline.txt
Leolaleoline answered 23/1, 2014 at 21:47 Comment(1)
Which shell does have uppercase FOR and this weird syntax? I think marginal technologies have to be explicitly noted.Sorce
A
6

This is a modification of @david-rodríguez-dribeas answer above, which runs all the pings in parallel (much faster) and only shows the output for ip addresses which return the ping.

export COUNTER=1
while [ $COUNTER -lt 255 ]
do
    ping $1$COUNTER -c 1 -w 400 | grep -B 1 "Lost = 0" &
    COUNTER=$(( $COUNTER + 1 ))
done
Argonaut answered 26/1, 2014 at 23:49 Comment(0)
I
3

Under linux, I think ping -b 192.168.1.255 will work (192.168.1.255 is the broadcast address for 192.168.1.*) however IIRC that doesn't work under windows.

Irresolvable answered 2/2, 2009 at 13:25 Comment(0)
A
1

I came late but here is a little script I made for this purpose that I run in Windows PowerShell. You should be able to copy and paste it into the ISE. This will then run the arp command and save the results into a .txt file and open it in notepad.

# Declare Variables
$MyIpAddress
$MyIpAddressLast

# Declare Variable And Get User Inputs
$IpFirstThree=Read-Host 'What is the first three octects of you IP addresses please include the last period?'
$IpStart=Read-Host 'Which IP Address do you want to start with? Include NO periods.'
$IpEnd=Read-Host 'Which IP Address do you want to end with? Include NO periods.'
$SaveMyFilePath=Read-Host 'Enter the file path and name you want for the text file results.'
$PingTries=Read-Host 'Enter the number of times you want to try pinging each address.'

#Run from start ip and ping
#Run the arp -a and output the results to a text file
#Then launch notepad and open the results file
Foreach($MyIpAddressLast in $IpStart..$IpEnd)
{$MyIpAddress=$IpFirstThree+$MyIpAddressLast
    Test-Connection -computername $MyIpAddress -Count $PingTries}
arp -a | Out-File $SaveMyFilePath
notepad.exe $SaveMyFilePath
Audible answered 21/1, 2018 at 2:58 Comment(0)
S
0
#!/bin/sh

COUNTER=$1

while [ $COUNTER -lt 254 ]
do
 echo $COUNTER
 ping -c 1 192.168.1.$COUNTER | grep 'ms'
 COUNTER=$(( $COUNTER + 1 ))
done

#specify start number like this: ./ping.sh 1
#then run another few instances to cover more ground
#aka one at 1, another at 100, another at 200
#this just finds addresses quicker. will only print ttl info when an address resolves
Summer answered 7/4, 2013 at 1:10 Comment(0)
A
0

This Script works well in OpenWRT

In a new file put this code ,

#!/bin/sh
echo "Online IPs" > out.txt
COUNTER=1

while [ $COUNTER -lt 255 ]
do
 ping $1.$COUNTER -c 1 -w 400 | grep "time" >> out.txt &
 COUNTER=$(( $COUNTER + 1 ))
done
killall ping

Set execution permision and launch it

root@r1:/# nping 192.168.1

then view all host connected in out.txt file

Aby answered 30/3, 2021 at 10:9 Comment(0)
W
0

To ping all IPs in subnet, use this command in Terminal (pre-install nmap, if needed with brew install nmap):

sudo nmap -sn -PE 192.168.N.0/24

where 192.168.N.0/24, is must set up of N - where u put ur number between 0 'n 255, if you have IP in subnet like 192.168.110.20 -> use this:

sudo nmap -sn -PE 192.168.110.0/24
Winfield answered 20/2 at 13:7 Comment(0)
D
0

On macOS I use a bash one line command like this which is super fast:

for i in $(seq 254); do { ping -W 100 -c 1 -n 192.168.0.$i | grep "bytes from" & }; done

as xargs didn't work as expected (-d is not supported by macOS xargs). I also added -n to ping to not lookup the DNS name (saves time). The -W 100 was necessary to give my devices enough time to respond to the ping.

Dupondius answered 24/3 at 17:6 Comment(0)
K
-3
for i in $(seq 1 254); do ping -c1 192.168.11.$i; done
Kienan answered 21/10, 2015 at 12:53 Comment(1)
I guess this answer was downvoted because it is lacking any explanation? ...or because it would take 42 minutes (standard ping in Ubuntu, worst case) to complete?Sorce

© 2022 - 2024 — McMap. All rights reserved.