# .bat file to check ip range then do work



## G-Stress (Feb 4, 2008)

Just had a good Idea and not sure where to get started. I want to create a .bat file that checks ip's and if an ip is = to say 192.168.1.101 or if the adapter falls in the say 192.168.1.? range then DO this...

just not sure how to tell it to check the ip or ip ranges.


----------



## TheOutcaste (Aug 8, 2007)

```
:::::::::::::::::::::::::::::::::::::::::::::::::::
:Checks a range of IP addresses then performs
:an action based on whether the system responded
:to a ping command. Note that if the system has
:a firewall set to ignore pings, it will appear
:to be not present. Set _t4 to the first 3 numbers
:of the IP address. This example will check
:192.168.9.10 through 192.168.9.20
:::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal enabledelayedexpansion
:Timeout for ping command in milliseconds
Set _t0=500
:start IP (last number)
Set _t1=10
:ending IP (last number)
Set _t2=20
Set _t4=192.168.9.
for /L %%I in (%_t1%,1,%_t2%) do set _t3=%%I & (ping %_t4%%%I -n 1 -w %_t0% >nul
) & call:_e!errorlevel!
echo All IPs tested
:::::::::::::::::::::::::::::::::::::::::::::::::::
:clear the temp variables that were used
:::::::::::::::::::::::::::::::::::::::::::::::::::
for /L %%I in (0,1,4) do set _t%%I=
goto:eof
:::::::::::::::::::::::::::::
:End of main
:::::::::::::::::::::::::::::
:Subroutines Below here
:::::::::::::::::::::::::::::::::::::::::::::::::::
:Enter code to execute if the IP address is present in this section
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_e0
echo %_t4%%_t3% exists
goto:eof
:::::::::::::::::::::::::::::::::::::::::::::::::::
:Enter code to execute if the IP address is not present in this section
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_e1
Echo %_t4%%_t3% does not exist
goto:eof
```
This will do it, unless ping has been blocked. Ping followed by an arp command could work around that. Need some experimenting for that.

Jerry


----------



## G-Stress (Feb 4, 2008)

Thanks Jerry,

I understand this for the most part I'm gonna play around with it and look online first to see what exactly does what toward the middle of the code to see if I can understand better and have some fun with this.


----------

