Batch file subnet script

Microsoft Windows xp professional w/serv...
June 9, 2010 at 22:49:52
Specs: Windows XP, 2g
My group has been tasked to locate a large amount of computers on our company network.

right now the standard they are using is just calling clients asking them where the PCs are. It's a headache becuase 50% of the clients called don't know.

I'm trying to put togther a windows script that will
1. Ping computername and resolve the ip address.
2. use the ip address and compare it to know subnets on out company network to find out what subnet it is on.

I have a list of subnets that my company uses.
Network:Downtown Building B
----------- questions.

What type of formula would I use to find out what subnet a particular IP address is on ?

At my disposal:
1. host IP address
2. Subnet mask of host
3. A list of known subnets used by our company.

Thanks in advance for any advice =T

See More: Batch file subnet script

Report •

June 10, 2010 at 06:29:54
You could convert the IP address into binary, then compare
the first 'X' number of bits (23 in your example) of the IP
address to the first 'X' number of bits in the network address:

1111 1111    1111 1111    1111 1110    0000 0000 - Mask
1000 1101    1011 0011    1110 1001    1111 1111 - is not in subnet
1000 1101    1011 0011    1110 1010    0000 0000 - is in subnet
1000 1101    1011 0011    1110 1011    1111 1111 - is in subnet
1000 1101    1011 0011    1110 1100    0000 0000 - is not in subnet

If you want, grab:


from here:
and put them in a folder in your %path%. d2b can be invoked from the command line:


or incorporated into a batch file using:

FOR /F %%a IN ('CALL d2b %IP_addr%') DO (
	SET /A bin_IP_addr=%%a

where %IP_addr% is a variable containing an IP address, and bin_IP_addr is the binary representation of that address. Comparing the binary representation of the IP address can be accomplished by setting a variable to a substring, similar to:

SET subnet=%bin_IP_addr:~0,23%

The rest of the code and logic I'll leave to you...

Note: Those batch files come "as-is", and are subject to change (or disappear!) upon my whim :-)

Report •

June 10, 2010 at 08:06:48
Added note concerning d2b:
You can remove byte and word (space) formatting by deleting
or REMing out the following blocks:


This will condense the octets of an IP address into one string.
It will also break knowing where two numbers passed as input
are separated!

d2b 15 255 will end up looking like:


instead of
0000 1111    1111 1111

Report •

June 10, 2010 at 08:36:44
WoW thanks!!

So given the batch files and code you just provided, I should be able to arrange it to where I can create a batch file that will:
1. prompt for PC ip address | ex: "Enter node IP:"
{I will use : for example input}

2. compare IPto known subnet.txt file | ex:"Example below"

3. output the current subnet it is on | ex: " PC is located on Subnet: City Local Office"
the known subnet text file I am using is formated as:
subnet name | ex: City Local Office
subnet ip & mask |ex:
subnet name | ex: City Local Office
subnet ip & mask |ex:
subnet name | ex: City Local Office
subnet ip & mask |ex:

thank you so much for the assistance orange!

Report •

Related Solutions

June 10, 2010 at 08:43:10
You should really XXX out the first couple Octets of your IPs for your own security. Remember this is a public forum.

Report •

June 10, 2010 at 08:56:47
heh thanks for the heads up but I did'nt use my companies acutal ip addresses in the posting. the ips above are random.
thanks though =)

Report •

June 10, 2010 at 12:20:08
I added two more files to the website:


Subnet.Script.bat needs to be edited on line 6 to specify your list of known subnet text file. It assumes that the text file is strictly in the format of location on one line, subnet on the next. If there are blank lines or out of order lines, all bets are off...

See what you think!

Report •

June 10, 2010 at 12:31:12
Admittedly, it's quick and dirty with no input error checking, and
to me it seems slow.

It is what it is though: as-is ;-)

Report •

June 10, 2010 at 13:15:28
Thanks again for all your help oon this orange.
In the bats above, what format would I use?
for instance would I need to type:

d2b.bat > c:\temp\subnets.txt

(Then type):



I appologize if I typed that wrong but to be completley honest it is a littel(just a little) over my head =T

Report •

June 10, 2010 at 13:31:15
Nope. Just double click the Subnet.Script.bat. I made it
interactive, where Subnet.Script will call the other needed
routines automatically, assuming the .bat files were placed in
a folder found in your %path%.

Here's what my %path% looks like:

C:\>echo %path%
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Roxio Shared\9.0\DLLShared\;C:\Program Files\C
ommon Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Teleca Shared;c:\path;\\DL380-
SERVER\LAN Path;\\DL380-SERVER\LAN Path\exes

Most of my stuff is stored in "\\DL380-SERVER\LAN Path" and "\\DL380-SERVER\LAN Path\exes", with a couple of
"local" .bats stored in "c:\path", folders I created myself.

Report •

June 10, 2010 at 13:48:07

Thank you so much orange!!

Report •

June 10, 2010 at 14:17:14
Glad to hear it!

You could modify Subnet.Script.bat to include logging of the
results, in addition to ECHOing in the window:

FOR /L %%a IN (1,1,!location!) DO (
	IF "%Client_subnet%" EQU "!location_%%a_subnet_bin!" (
		@ECHO Client IP : %Client_addr%
		@ECHO Location  : location_%%a! 
		@ECHO Subnet    : !location_%%a_subnet!/!location_%%a_CIDR!
		>>"%~dp0\%~n0.Results.txt" ECHO Client IP : %Client_addr%
		>>"%~dp0\%~n0.Results.txt" ECHO Location  : !location_%%a! 
		>>"%~dp0\%~n0.Results.txt" ECHO Subnet    : !location_%%a_subnet!/!location_%%a_CIDR!
		>>"%~dp0\%~n0.Results.txt" ECHO.

The above would record for example:

Client IP :
Location  : City Office Two
Subnet    :

into \<path to Subnet.Script>\Subnet.Script.Results.txt,
which may come in handy at some point!

Report •

Ask Question