Subnet calculator to ping ip addresses
Posted: 22 Apr 2010 09:35
Hello all
I have been putting together a subnet calculator with additional feature that I want it to ping each address. So far I can generate a list of the ip addresses in a subnet. Problem is my list includes the subnet number and the broadcast address and I only want useable host ips in the list. Can anyone help? My code so far is:
@Echo Off
Setlocal Enabledelayedexpansion
echo
:BEGIN
cls
set /p getip= Enter the ip address?
set /p getsbn= Enter subnet mask?
rem get ip and split into vars
for /f "tokens=1,2,3,4 delims=." %%G in ("%getip%") do (
set ipa=%%G&set ipb=%%H&set ipc=%%I&set ipd=%%J
)
set ipadd=%ipa%.%ipb%.%ipc%.%ipd%
echo IP address %ipadd%
rem get subnet mask and split into vars
for /f "tokens=1,2,3,4 delims=." %%G in ("%getsbn%") do (
set sba=%%G&set sbb=%%H&set sbc=%%I&set sbd=%%J
)
set sbmsk=%sba%.%sbb%.%sbc%.%sbd%
echo subnet mask %sbmsk%
:CALCS
rem calculate the inverse mask
set /a wr=255-%sba%
set /a xr=255-%sbb%
set /a yr=255-%sbc%
set /a zr=255-%sbd%
set invmsk=%wr%.%xr%.%yr%.%zr%
echo inverse mask %invmsk%
rem get network number and split into vars
set /a wt=%wr%+1
set /a qtw=%ipa%/%wt%
set /a rmtw=%ipa%%%wt%
set /a nta=%qtw%*%wt%
set /a xt=%xr%+1
set /a qtx=%ipb%/%xt%
set /a rmtx=%ipb%%%xt%
set /a ntb=%qtx%*%xt%
set /a yt=%yr%+1
set /a qty=%ipc%/%yt%
set /a rmty=%ipc%%%yt%
set /a ntc=%qty%*%yt%
set /a zt=%zr%+1
set /a qtz=%ipd%/%zt%
set /a rmtz=%ipd%%%zt%
set /a ntd=%qtz%*%zt%
set ntwk=%nta%.%ntb%.%ntc%.%ntd%
rem get broadcast address and split into vars
set /a ws=%wr%+%nta%
set /a xs=%xr%+%ntb%
set /a ys=%yr%+%ntc%
set /a zs=%zr%+%ntd%
set bct=%ws%.%xs%.%ys%.%zs%
rem check the ip address does not match the subnet no or broadcast address
set /a ntsum=%nta%+%ntb%+%ntc%+%ntd%
set /a bcsum=%ws%+%xs%+%ys%+%zs%
set /a ipsum=%ipa%+%ipb%+%ipc%+%ipd%
if %ntsum%==%ipsum% goto :BADNET
if %bcsum%==%ipsum% goto :BADBRD
rem get the first and last useable addresses
set /a ntdf=%ntd%+1
set /a zsl=%zs%-1
set ipfst=%nta%.%ntb%.%ntc%.%ntdf%
set iplst=%ws%.%xs%.%ys%.%zsl%
echo network %ntwk%
echo broadcast address %bct%
echo first address %ipfst%
echo last address %iplst%
For /l %%P in (%nta%,1,%ws%) do (
set p=%%P
For /l %%O in (%ntb%,1,%xs%) do (
set o=%%O
For /l %%M in (%ntc%,1,%ys%) do (
set m=%%M
For /l %%N in (%ntd%,1,%zs%) do (
set n=%%N
echo !p!.!o!.!m!.!n! >> %TEMP%\ipcalc.txt
)
)
)
)
%TEMP%\ipcalc.txt
del %TEMP%\ipcalc.txt
I have been putting together a subnet calculator with additional feature that I want it to ping each address. So far I can generate a list of the ip addresses in a subnet. Problem is my list includes the subnet number and the broadcast address and I only want useable host ips in the list. Can anyone help? My code so far is:
@Echo Off
Setlocal Enabledelayedexpansion
echo
:BEGIN
cls
set /p getip= Enter the ip address?
set /p getsbn= Enter subnet mask?
rem get ip and split into vars
for /f "tokens=1,2,3,4 delims=." %%G in ("%getip%") do (
set ipa=%%G&set ipb=%%H&set ipc=%%I&set ipd=%%J
)
set ipadd=%ipa%.%ipb%.%ipc%.%ipd%
echo IP address %ipadd%
rem get subnet mask and split into vars
for /f "tokens=1,2,3,4 delims=." %%G in ("%getsbn%") do (
set sba=%%G&set sbb=%%H&set sbc=%%I&set sbd=%%J
)
set sbmsk=%sba%.%sbb%.%sbc%.%sbd%
echo subnet mask %sbmsk%
:CALCS
rem calculate the inverse mask
set /a wr=255-%sba%
set /a xr=255-%sbb%
set /a yr=255-%sbc%
set /a zr=255-%sbd%
set invmsk=%wr%.%xr%.%yr%.%zr%
echo inverse mask %invmsk%
rem get network number and split into vars
set /a wt=%wr%+1
set /a qtw=%ipa%/%wt%
set /a rmtw=%ipa%%%wt%
set /a nta=%qtw%*%wt%
set /a xt=%xr%+1
set /a qtx=%ipb%/%xt%
set /a rmtx=%ipb%%%xt%
set /a ntb=%qtx%*%xt%
set /a yt=%yr%+1
set /a qty=%ipc%/%yt%
set /a rmty=%ipc%%%yt%
set /a ntc=%qty%*%yt%
set /a zt=%zr%+1
set /a qtz=%ipd%/%zt%
set /a rmtz=%ipd%%%zt%
set /a ntd=%qtz%*%zt%
set ntwk=%nta%.%ntb%.%ntc%.%ntd%
rem get broadcast address and split into vars
set /a ws=%wr%+%nta%
set /a xs=%xr%+%ntb%
set /a ys=%yr%+%ntc%
set /a zs=%zr%+%ntd%
set bct=%ws%.%xs%.%ys%.%zs%
rem check the ip address does not match the subnet no or broadcast address
set /a ntsum=%nta%+%ntb%+%ntc%+%ntd%
set /a bcsum=%ws%+%xs%+%ys%+%zs%
set /a ipsum=%ipa%+%ipb%+%ipc%+%ipd%
if %ntsum%==%ipsum% goto :BADNET
if %bcsum%==%ipsum% goto :BADBRD
rem get the first and last useable addresses
set /a ntdf=%ntd%+1
set /a zsl=%zs%-1
set ipfst=%nta%.%ntb%.%ntc%.%ntdf%
set iplst=%ws%.%xs%.%ys%.%zsl%
echo network %ntwk%
echo broadcast address %bct%
echo first address %ipfst%
echo last address %iplst%
For /l %%P in (%nta%,1,%ws%) do (
set p=%%P
For /l %%O in (%ntb%,1,%xs%) do (
set o=%%O
For /l %%M in (%ntc%,1,%ys%) do (
set m=%%M
For /l %%N in (%ntd%,1,%zs%) do (
set n=%%N
echo !p!.!o!.!m!.!n! >> %TEMP%\ipcalc.txt
)
)
)
)
%TEMP%\ipcalc.txt
del %TEMP%\ipcalc.txt