I need to find IP of device or devices in local network. I only know that their MAC address should start with "xx-xx-xx-xx-". I found following script that do almost what I need:
#echo off
:top
:: Loop through arp table entries and look for my device's MAC address
for /f "tokens=1-5 skip=3" %%f in ('arp -a') do (
if "%%g"=="xx-xx-xx-xx-xx-xx" set ip=%%f
)
if "%ip%"=="" (
echo Discovering network...
:: Ping all IPs from 192.168.0.1 to 254
for /L %%N in (1,1,254) do start /b ping -n 1 -w 200 192.168.0.%%N >nul
timeout 1 >nul
goto :top
) else (
echo Device found found: %ip%
)
pause
But this script search only with full MAC and only one device. How make it search subMAC and several devices?
the following works for me:
#echo off
:top
:: Loop through arp table entries and look for my device's MAC address
set "ip="
for /f "tokens=2" %%f in ('arp -a^|find " d4-85-64"') do set ip=%%f
if "%ip%"=="" (
echo Discovering network...
:: Ping all IPs from 192.168.0.1 to 254
for /L %%N in (1,1,254) do start /b ping -n 1 -w 200 192.168.0.%%N >nul
timeout 1 >nul
goto :top
)
echo Device found: %ip%
pause
Changes:
set "ip=" to empty variable (in case, it already exists (from previous run))
removed skip (not needed),
set "tokens=2" (that's all, we need)
included find " xx-xx-xx" to search the desired line (with leading space for "start with") (of course I choosed a MAC existing in MY network)
moved success message out of if (not neccesary, but cleaner code)
Related
I'm currently supporting a team of techs who are upgrading machines in offices and I need to keep track of the amount of machines that are online.
Currently what I have is a text file (OfficeName.txt) with a list of machine names, and a batch file (OfficeName.bat).
What I want to happen is for it to loop through the list of machines and ping them.
If the ping is successful, remove the name from the list and increment a counter by 1, if unsuccessful, then move on to the next machine in the list.
The issue I'm having is that if a machine's ping result comes back with "could not find host", it still sets the errorlevel to 0, so I can't use an IF/ELSE.
My current attempt looks like this:
#echo off
setlocal EnableDelayedExpansion
set /a counter=0
set "NVC="
for /F %%a in (%~n0.txt) do set "NVC=!NVC! %%a"
:ping
for %%i in (%NVC%) do (
ping %%i -n 1 >nul | find "TTL=" >nul || echo. %%i is offline.
set /a counter+=1
echo %%i is online
set "NVC=!NVC: %%i=!"
)
cls
echo. %counter% machines are online.
if defined NVC goto :ping
echo All machines in %~n0 are online.
pause
The problem is that once the "%%i is offline" line is done, it just continues to the next line and removes it anyway.
Is there a way to skip the 3 lines below the ping if the ping result is bad, and continue with the next iteration in the list?
Note: I'm running this from Server 2008, pinging Win 8.1 machines.
Your first problem is that you're trying to find "TTL=" in output that's been redirected to NUL. Remove that redirection so FIND gets something to search.
Your second problem is a logic error, where even if you execute the || clause, the other lines are executing too. You need to use an ELSE or another IF clause.
Finally, I think you're overcomplicating the processing of your list of IP addresses.
Try this, and see if it makes sense.
#echo off & setlocal
set /a counteron=0
set /a counteroff=0
for /f %%i in (%~n0.txt) do (
ping %%i -n 1 | find "TTL=" >nul
if errorlevel 1 (
set /a counteroff+=1
echo %%i is offline.
) else (
set /a counteron+=1
echo %%i is online
)
)
echo.
echo %counteron% machines are online.
echo %counteroff% machines are offline.
I want to check if a hostname exists on my PC (ie found in hosts file under C:\Windows\System32\drivers\etc).
Is there a way to find if it exist using a batch command or some other way?
Give a try for this batch file with some extra info :
#echo off
set "SearchString=localhost"
set "LogFile=%userprofile%\Desktop\LogFile.txt"
set "hostspath=%windir%\System32\drivers\etc\hosts"
(
Echo **************************** General info ****************************
Echo Running under: %username% on profile: %userprofile%
Echo Computer name: %computername%
Echo Operating System:
wmic os get caption | findstr /v /r /c:"^$" /c:"^Caption"
Echo Boot Mode:
wmic COMPUTERSYSTEM GET BootupState | find "boot"
Echo Antivirus software installed:
wmic /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct Get displayName | findstr /v /r /c:"^$" /c:"displayName"
Echo Executed on: %date% # %time%
Echo ********************* Hosts' File Contents with the string "%SearchString%" ************************
)>"%LogFile%"
for /f "delims=" %%a in ('Type "%hostspath%" ^| find /I "%SearchString%"') Do (
echo %%a >> "%LogFile%"
)
Start "" "%LogFile%"
Easier and more robust solution
url.bat:
#echo off
set url=%1
ping -n 1 %url% > nul 2> nul
if "%errorlevel%"=="0" (
echo %url% exists
) else (
echo %url% does not exist
)
Test
> url.bat google.com
google.com exists
> url.bat google.commmmmm
google.commmmmm does not exist
What you possibly can do is pinging the hostname you are looking for and then check for certain strings, that will show you if the hostname could be found or not. Would look like this (I guess):
#echo off
setlocal EnableDelayedExpansion
set /p input= "Hostname"
set hostexists=yes
For /f "tokens=1,2" %%a in ('ping -n 1 !input!') do (
if "x%%a"=="xFOO" if "x%%b"=="xBAR" set hostexists=no
)
If "x!hostexists!"=="xno" (
echo. "Does not exist"
) ELSE (
echo. "Does exist"
Pause
Basic thought is that when you try to ping a hostname that is not available, you will get a specific output from the commandline. Try it yourself: Open the cmd.exe (Hit the Windows-Button +R and type cmd) and in the commandline write ping foobar and wait a bit. You should get a message like: Ping-Request could not find "foobar" [...]. You take the first two words and put them into the code: 1st word to FOO and 2nd to BAR.
The program will look into the output of the ping command and place the first two words (=tokens) in %%a and %%b checking if they are equal to the desired words, marking the host does not exist.
I hope this will help :) Not sure if that is what you wanted :D
Greetings
geisterfurz007
I'm trying to write a batch file, that gives me an IP back, but I just know the MAC address
arp -a
wont work for me, because I never pinged that IP before. I want to search for it in the network with literally just knowing the MAC address.
Information: The IP is static.
Assuming, your subnet is 192.168.1.xxx
#echo off
REM clear arp cache (optional):
arp -d
REM ask everybody on the subnet for a response:
for /l %%a in (1,1,255) do start /b ping -n 2 -w 500 192.168.1.%%a >nul
REM wait for the processes to finish:
timeout 2 >nul
REM show the responses (with IP and MAC):
arp -a
When you found the IP, you can try to resolve the computer name with:
ping -a -n 1 192.168.1.xxx
Edit found a way to speed it up.
Edit : just realized you where on Windows. Sadly, there is no native way to ping on braodcast addresses with Windows, but you can use the trick from this answer : FOR /L %i in (1,1,255) do #ping -n 1 192.168.1.%i | find "Reply". It seems super slow though.
Previous answer is below.
As far as I know, you have to fill in your ARP cache to do what you want.
This answer is what you need.
As the post says, you can :
ping all hosts of your LAN by pinging your broadcast address (ping -b -c1 192.168.1.255, replace the IP by your actual broadcast address)
fping your whole network (fping -a -g 192.168.1.0/24, replace with your actual netmask)
nmap the network (nmap -sP 192.168.1.0/24)
nbtscan (windows hosts only, nbtscan 192.168.1.0/24)
And then look up in your ARP cache.
for /f "skip=3 delims=\" %%A in ('net view ^| findstr /v /C:"The command completed successfully"') do Echo %%A >> "%userprofile%\desktop\computerlist.txt"
Lists turned on computers.
wmic /node:#computerlist.txt nicconfig where macaddress='whatever:whatever:etc' get ipaddress /format:htable
To get IP for a specified MACAddress
wmic /node:#computerlist.txt nicconfig get ipaddress /format:htable
to get the IPAddress for all MACAddresses on a computer.
I remember that sachadee posted this code ScanIP-MAC.bat
#echo off
Title Scan for IP and MAC Adress on LAN
mode con cols=60 lines=20
Color A
set ip=192.168.1.1
set debut=1
set fin=10
if exist ping.txt (del ping.txt)
if exist ping2.txt (del ping2.txt)
if exist ping3.txt (del ping3.txt)
if exist ping4.txt (del ping4.txt)
for /L %%i in (%debut%,1,%fin%) do (echo Recherche de la machine : %ip:~0,9%.%%i
for /f "tokens=5 delims= " %%f in ('ping -4 -n 1 %ip:~0,9%.%%i ^|find /i "32"') do (echo %%f >> ping.txt
)
for /f "tokens=1 delims= " %%k in ('Type ping.txt ^|findstr /i "19"') do echo %%k > ping2.txt
for /f %%l in (ping2.txt) do (arp -a %%l >> ping3.txt
)
)
Cmd /U /C Type ping3.txt > ping4.txt
Del ping.txt
Del ping2.txt
Del ping3.txt
Start ping4.txt
I am not a batch user so i dont know the commands but i know the way to do this is sending broadcast ping request to ur network and check for the mac address from the reply's using wireshark.
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.
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
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.
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.
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.
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
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:
Here is a nice writeup on the use of 'arp -d' here if interested -
TechRepublic - Quick Tips Flush the ARP cache in Windows 7
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.
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
aping can provide a list of hosts and whether each has responded to pings.
aping -show all 192.168.1.*
#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.
#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
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
The code below will write the computer name and ip address to file, but I would like it to also write the name of the computers it cannot ping with a fail next to it. I have no idea how I would modify the batch file to do this.
#echo off
Echo Pinging list...
set ComputerList=list.txt
Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0, -1!>>Results.csv
))
pause
You could use errorlevel set by findstr to substitute return string(s) if 'Reply' is not found:
('ping -n 1 -l 1 %%A ^|findstr Reply ^|^| echo Not found Failed:')
where || (escaped here because of for context with ^) means execute only if previous command failed.
As a side note, you should be aware that ping messages are system language dependent (they are translated to language of OS) so 'Reply' as success indicator works only for English versions.
This may not be directly what you are looking for, but I had a similar task: run ping and report success or failure. I'll leave extracting the IP address to you - seeing as you have already done it.
The problem with ping is that it returns success upon name resolution, whether packets get lost or host is unreachable (will report 0% Loss) is irrelevant.
FOR %%a IN (
google.com
a.b.c.d
) DO #FOR /F "delims=" %%p IN (
'PING -w 100 -n 1 %%a ^| findstr ^"Reply Request fail name^"'
) DO #(
ECHO "%%p" | FINDSTR TTL >2 && echo %%a, success, %%p || echo %%a, failed, %%p
) >> Results.csv
Logic: Ping once, filter only lines with the one of the words listed. If TTL exists in resulting line (output to STDERR or NUL to avoid output pollution) echo success, else echo failed.
I'm on English Windows, words will have to be adjusted for other languages.
EDIT:
FOR %%a IN (
google.com
a.b.c.d
) DO #FOR /F "delims=" %%p IN ('PING -n 1 %%a ^| findstr TTL ^|^| echo Failed') DO #(
ECHO "%%p" | FINDSTR TTL >2 && (for /f "tokens=3" %%b IN ("%%p") do #echo %%a, %%b) || echo %%a, failed, %%p
)
Less dependant on language, works only for IPv4, added IP extraction.
Filter ping output for TTL, set output to "Failed" if TTL not found.
If output string contains TTL, extract IP and echo host and IP, else echo host name and output string.