58

Is it possible in windows cmd line to check all of the network addresses (with ping or similar) to see which ones are taken/ have active devices:

ie. something that does something like the following:

for i = 0 to 255
    ping 192.168.1.i //Print this
end

This is psuedo code obviously. I am wondering if it is possible to do something like this in windows cmd. It would be great if you didn't need a batch file, but i understand if this is impossible.

PS. Also please mention if there is a program to do this, but it would be nice to do it in cmd.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • 4
    I have no knoledge, just wondering... would `ping 192.168.1.255` get responses from all the devices in the subnet? – FrancescoMM Dec 04 '12 at 22:53

12 Answers12

126

Open the Command Prompt and type in the following:

FOR /L %i IN (1,1,254) DO ping -n 1 192.168.10.%i | FIND /i "Reply">>c:\ipaddresses.txt

Change 192.168.10 to match you own network.

By using -n 1 you are asking for only 1 packet to be sent to each computer instead of the usual 4 packets.

The above command will ping all IP Addresses on the 192.168.10.0 network and create a text document in the C:\ drive called ipaddresses.txt. This text document should only contain IP Addresses that replied to the ping request.

Although it will take quite a bit longer to complete, you can also resolve the IP Addresses to HOST names by simply adding -a to the ping command.

FOR /L %i IN (1,1,254) DO ping -a -n 1 192.168.10.%i | FIND /i "Reply">>c:\ipaddresses.txt

This is from Here

starball
  • 20,030
  • 7
  • 43
  • 238
RGG
  • 1,833
  • 1
  • 15
  • 28
  • Great answer, however, it currently prints out that the find parameter is not correct. How can I fix that? – Fantastic Mr Fox Dec 04 '12 at 22:56
  • Try to paste the command in a text editor and delete and re-add the quotes. The quotes you copy are some HTML encoded double quote. – RGG Dec 04 '12 at 22:58
  • 1
    I've edited the post to fix the smart quotes. Should work now. :-) – Mark Dec 05 '12 at 02:49
  • This command appears to cause the command prompt to crash on Windows 8. – Paul Lammertsma Sep 11 '13 at 18:55
  • 2
    be aware that depending on your windows configuration c:\ipaddresses.txt might not be writeable for a none Admin user, change the path to a folder that is writeable to the current user – Dukeatcoding Jan 16 '15 at 15:44
  • 4
    To increase the speed of this script running, add the -w parameter to set a relatively low timeout to wait for each reply. On my fast, local network I set this to 10 and it cut the time to run the script in less than half. – Konr Ness Jan 12 '16 at 23:45
  • 4
    If using this in a batch file, one should use `%%i` instead of `%i`. – sancho.s ReinstateMonicaCellio Oct 18 '16 at 04:21
  • 1
    On a German language windows installation, use "Antwort" instead of "Reply": `FOR /L %i IN (1,1,254) DO ping -a -n 1 192.168.10.%i | FIND /i "Antwort">>d:\ipaddresses.txt` – Christoph Bimminger Jan 29 '19 at 23:45
  • The problem with this is it is not consistent - it misses alive devices. – NetMage Mar 19 '21 at 18:09
40

I know this is a late response, but a neat way of doing this is to ping the broadcast address which populates your local arp cache.

This can then be shown by running arp -a which will list all the addresses in you local arp table.

ping 192.168.1.255
arp -a

Hopefully this is a nice neat option that people can use.

Ed Mackenzie
  • 679
  • 6
  • 16
  • 2
    on my setup `ping 192.168.1.255; arp -a` alone did not show the dynamic ip but: `0..255 | ForEach-Object {ping 192.168.1.$_}` followed by `arp -a` did (powershell). – daniel.kahlenberg Jun 17 '15 at 16:47
  • 3
    Note that not all devices reply to ping on the broadcast address – Chriz Nov 22 '17 at 14:04
  • Note that in a network with VLANs this will not work as you only ARP for devices in your local subnet. – NetMage Mar 19 '21 at 18:02
  • Sometimes the admins/users on the target machine(s) may configure to ignore ping requests, i.e. don't send ping replies. Then what can you propose to identify those existed devices, but not identified using `ping`? –  Oct 08 '21 at 19:59
  • @daniel.kahlenberg 0..255 | ForEach-Object {ping -n 1 -w 1 192.168.1.$_} does the same but much faster (only one ping for each host with a 1 ms timeout). – Loreno Heer Dec 22 '22 at 20:10
20

Best Utility in terms of speed is Nmap.

write @ cmd prompt:

Nmap -sn -oG ip.txt 192.168.1.1-255

this will just ping all the ip addresses in the range given and store it in simple text file

It takes just 2 secs to scan 255 hosts using Nmap.

Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
spetzz
  • 201
  • 2
  • 2
  • On my machine it took slightly longer than 2 seconds (maybe 10?); however, the batch file above takes about ten minutes, and I needed to run a lot of range searches, so this was the best route for me. – slim Jul 07 '14 at 13:24
12

Provided the windows box is in the same subnet:

for /L %a in (1,1,254) do start ping 192.168.0.%a

This will complete in less than 15 seconds and

arp -a 

will return any alive host.

Fastest native way I know of in Windows.

Andrew Myers
  • 2,754
  • 5
  • 32
  • 40
Vigbjorn
  • 121
  • 1
  • 2
  • 5
    even faster: `for /L %a in (1,1,254) do @start /b ping 146.254.179.%a -w 100 -n 2 >nul`. And you should `arp -d` before to delete any old entries to get just alive hosts. – Stephan Apr 21 '17 at 15:54
  • @Stephan But that also changes the output order to not be sequential. – NetMage Mar 19 '21 at 18:04
  • @NetMage it doesn't have to be sequential - it's just to fill the `arp` database, which displays its entries sequentially anyway. – Stephan Mar 19 '21 at 19:15
  • @Stephan I see what you mean - that doesn't work for me I am on a heavy VLAN network :) – NetMage Mar 19 '21 at 21:24
5

This post asks the same question, but for linux - you may find it helpful. Send a ping to each IP on a subnet

nmap is probably the best tool to use, as it can help identify host OS as well as being faster. It is available for the windows platform on the nmap.org site

Hack5
  • 3,244
  • 17
  • 37
Paul Allen
  • 349
  • 1
  • 9
2

All you are wanting to do is to see if computers are connected to the network and to gather their IP addresses. You can utilize angryIP scanner: http://angryip.org/ to see what IP addresses are in use on a particular subnet or groups of subnets.

I have found this tool very helpful when trying to see what IPs are being used that are not located inside of my DHCP.

Benjamin Trent
  • 7,378
  • 3
  • 31
  • 41
  • The link you provided is giving a 404. http://www.angryip.org works fine though, the subpaths are not needed – anurupr Dec 12 '17 at 16:25
2

An expansion and useful addition to egmackenzie's "arp -a" solution for Windows -

Windows Example searching for my iPhone on the WiFi network

(pre: iPhone WiFi disabled)

  • Open Command Prompt in Admin mode (R.C. Start & look in menu)
  • arp -d <- clear the arp listing!
  • ping 10.1.10.255 <- take your subnet, and ping '255', everyone
  • arp -a
  • iPhone WiFi on
  • ping 10.1.10.255
  • arp -a

See below for example:

enter image description here

Here is a nice writeup on the use of 'arp -d' here if interested -

J-Dizzle
  • 4,861
  • 4
  • 40
  • 50
1

Some things seem appeared to have changed in batch scripts on Windows 8, and the solution above by DGG now causes the Command Prompt to crash.

The following solution worked for me:

@echo off
set /a n=0
:repeat
set /a n+=1
echo 192.168.1.%n%
ping -n 1 -w 500 192.168.1.%n% | FIND /i "Reply">>ipaddresses.txt
if %n% lss 254 goto repeat
type ipaddresses.txt
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
1

aping can provide a list of hosts and whether each has responded to pings.

 aping -show all 192.168.1.*
disk eater
  • 121
  • 2
0
@ECHO OFF

IF "%SUBNET%"=="" SET SUBNET=10

:ARGUMENTS
ECHO SUBNET=%SUBNET%
ECHO ARGUMENT %1 
IF "%1"=="SUM" GOTO SUM
IF "%1"=="SLOW" GOTO SLOW
IF "%1"=="ARP" GOTO ARP
IF "%1"=="FAST" GOTO FAST

REM PRINT ARP TABLE BY DEFAULT
:DEFAULT
ARP -a
GOTO END

REM METHOD 1 ADDRESS AT A TIME
:SLOW
ECHO START SCAN
ECHO %0 > ipaddresses.txt
DATE /T >> ipaddresses.txt
TIME /T >> ipaddresses.txt
FOR /L %%i IN (1,1,254) DO ping -a -n 2 192.168.%SUBNET%.%%i | FIND /i "TTL="  >> ipaddresses.txt
GOTO END

REM METHOD 2 MULTITASKING ALL ADDRESS AT SAME TIME
:FAST
ECHO START FAST SCANNING 192.168.%SUBNET%.X
set /a n=0
:FASTLOOP
set /a n+=1
ECHO 192.168.%SUBNET%.%n%
START CMD.exe /c call ipaddress.bat 192.168.%SUBNET%.%n% 
IF %n% lss 254 GOTO FASTLOOP
GOTO END

:SUM
ECHO START SUM
ECHO %0 > ipaddresses.txt
DATE /T >> ipaddresses.txt
TIME /T >> ipaddresses.txt
FOR /L %%i IN (1,1,254) DO TYPE ip192.168.%SUBNET%.%%i.txt | FIND /i "TTL=" >> ipaddresses.txt
FOR /L %%i IN (1,1,254) DO DEL ip192.168.%SUBNET%.%%i.txt
type ipaddresses.txt
GOTO END

:ARP
ARP -a >> ipaddresses.txt
type ipaddresses.txt
GOTO END


:END
ECHO DONE WITH IP SCANNING
ECHO OPTION "%0 SLOW" FOR SCANNING 1 AT A TIME
ECHO OPTION "%0 SUM" FOR COMBINE ALL TO FILE
ECHO OPTION "%0 ARP" FOR ADD ARP - IP LIST
ECHO PARAMETER "SET SUBNET=X" FOR SUBNET
ECHO.
  • Welcome to SO! As Math noted, your answer isn't terribly useful without some context. Please edit your answer to add a description of what it does. – Derek Nov 08 '13 at 17:06
0
@echo off
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
arp -d
setlocal
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr /i "192"`) do (
for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
for /f "tokens=1-4 delims=." %%c in ("%%b") do (
set _o1=%%c
set _o2=%%d
set _o3=%%e
set _o4=%%f
set _3octet=!_o1:~1!.!_o2!.!_o3!.
for /L %%a in (1,1,254) do start /min ping /n 1 /l 1 !_3octet!%%a
)))
endlocal

After you run the batch file, type this command

arp -a

All devices connected to the network will be displayed

A7ec
  • 136
  • 6
-1
for /l %%a in (254, -1, 1) do ( 
    for /l %%b in (1, 1, 254) do (
        for %%c in (20, 168) do (
            for %%e in (172, 192) do (
                ping /n 1 %%e.%%c.%%b.%%a>>ping.txt
            )
        )
    )
)
pause>nul
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • While the answer address OP's question, it's advisable to include description and context regarding posted code. Otherwise, is like a subject without pronoun in a sentence. Upvoted. – Tinel Barb Dec 14 '22 at 14:13