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
Subnet calculator to ping ip addresses
Moderator: DosItHelp
Re: Subnet calculator to ping ip addresses
with a new pair of eyes in the morning it all became clear or at least a little less blurry and I've got it working as intended.
The for loop now sets and compares the strings for the first/last and I changed some variables to be in alphabetical order. Main change is I added
set i=%nta%......
set s=%ws%......
if !i!.!j!.!k!.!l! EQU !m!.!n!.!o!.!p! (set /a count=1) ELSE (
if !s!.!t!.!u!.!v! NEQ !m!.!n!.!o!.!p! (echo !m!.!n!.!o!.!p! ..etc
I'm a bit stuck in that I had to create a variable count for no reason other than giving it something to do. I have to put my hand up to being an amateur and if anyone has any comments or suggestions on how to tidy things up I'd appreciate it.
The whole for loop now reads like this:
For /l %%M in (%nta%,1,%ws%) do (
set m=%%M
For /l %%N in (%ntb%,1,%xs%) do (
set n=%%N
For /l %%O in (%ntc%,1,%ys%) do (
set o=%%O
For /l %%P in (%ntd%,1,%zs%) do (
set p=%%P
set i=%nta%&set j=%ntb%&set k=%ntc%&set l=%ntd%
set s=%ws%&set t=%xs%&set u=%ys%&set v=%zs%
if !i!.!j!.!k!.!l! EQU !m!.!n!.!o!.!p! (set /a count=1) ELSE (
if !s!.!t!.!u!.!v! NEQ !m!.!n!.!o!.!p! (echo !m!.!n!.!o!.!p! >> %TEMP%\ipcalc.txt)
)
)
)
)
)
%TEMP%\ipcalc.txt
del %TEMP%\ipcalc.txt
The for loop now sets and compares the strings for the first/last and I changed some variables to be in alphabetical order. Main change is I added
set i=%nta%......
set s=%ws%......
if !i!.!j!.!k!.!l! EQU !m!.!n!.!o!.!p! (set /a count=1) ELSE (
if !s!.!t!.!u!.!v! NEQ !m!.!n!.!o!.!p! (echo !m!.!n!.!o!.!p! ..etc
I'm a bit stuck in that I had to create a variable count for no reason other than giving it something to do. I have to put my hand up to being an amateur and if anyone has any comments or suggestions on how to tidy things up I'd appreciate it.
The whole for loop now reads like this:
For /l %%M in (%nta%,1,%ws%) do (
set m=%%M
For /l %%N in (%ntb%,1,%xs%) do (
set n=%%N
For /l %%O in (%ntc%,1,%ys%) do (
set o=%%O
For /l %%P in (%ntd%,1,%zs%) do (
set p=%%P
set i=%nta%&set j=%ntb%&set k=%ntc%&set l=%ntd%
set s=%ws%&set t=%xs%&set u=%ys%&set v=%zs%
if !i!.!j!.!k!.!l! EQU !m!.!n!.!o!.!p! (set /a count=1) ELSE (
if !s!.!t!.!u!.!v! NEQ !m!.!n!.!o!.!p! (echo !m!.!n!.!o!.!p! >> %TEMP%\ipcalc.txt)
)
)
)
)
)
%TEMP%\ipcalc.txt
del %TEMP%\ipcalc.txt
Re: Subnet calculator to ping ip addresses
Think I have finished with this thread. I've added in validation and tidied up a bit. Seen a post Find an IP address posted by locbtran and MarkL provides a very neat solution to what is basically the same but his uses less time and space and has more relevant output - something I'll need to work on.
For anyone that might find it helpful heres my finished script.
@Echo Off
Setlocal Enabledelayedexpansion
echo
:BEGIN
cls
echo.
set /p getip= Enter the ip address?
cls
echo.
set /p getsbn= Enter the subnet mask?
cls
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%
if %ipa% GTR 255 goto :BADIP
if %ipb% GTR 255 goto :BADIP
if %ipc% GTR 255 goto :BADIP
if %ipd% GTR 255 goto :BADIP
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%
rem need to add valid nos 255,254,252,248,240,224,192,128,0
set /a vldcnt=0
for %%? in (0 128 192 224 240 248 252 254 255) do (if %sba% EQU %%? set /a vldcnt=%vldcnt%+1)
for %%? in (0 128 192 224 240 248 252 254 255) do (if %sbb% EQU %%? set /a vldcnt=%vldcnt%+1)
for %%? in (0 128 192 224 240 248 252 254 255) do (if %sbc% EQU %%? set /a vldcnt=%vldcnt%+1)
for %%? in (0 128 192 224 240 248 252 254 255) do (if %sbd% EQU %%? set /a vldcnt=%vldcnt%+1)
if %vldcnt% NEQ 4 goto :BADMASK
rem check that mask values drop to zero after any previous octet has a value less than 255
if %sba% EQU 255 (goto :SUBB) ELSE (
if %sbb% NEQ 0 goto :BADMASK
if %sbc% NEQ 0 goto :BADMASK
if %sbd% NEQ 0 goto :BADMASK
)
:SUBB
if %sbb% EQU 255 (goto :SUBC) ELSE (
if %sbc% NEQ 0 goto :BADMASK
if %sbd% NEQ 0 goto :BADMASK
)
:SUBC
if %sbc% EQU 255 (goto :CALCS) ELSE (
if %sbd% NEQ 0 goto :BADMASK
)
: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%
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%
if %nta% GTR 255 set nta=0
if %ntb% GTR 255 set ntb=0
if %ntc% GTR 255 set ntc=0
if %ntd% GTR 255 set ntd=0
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%
set i=%nta%&set j=%ntb%&set k=%ntc%&set l=%ntd%
set s=%ws%&set t=%xs%&set u=%ys%&set v=%zs%
set w=%ipa%&set x=%ipb%&set y=%ipc%&set z=%ipd%
if !i!.!j!.!k!.!l! EQU !w!.!x!.!y!.!z! goto :BADNET
if !s!.!t!.!u!.!v! EQU !w!.!x!.!y!.!z! goto :BADBRD
set /a ntdf=%ntd%+1
set /a zsl=%zs%-1
set ipfst=%nta%.%ntb%.%ntc%.%ntdf%
set iplst=%ws%.%xs%.%ys%.%zsl%
echo.
echo. IP address .............. %ipadd%
echo. Subnet mask ............. %sbmsk%
echo. Inverse mask ............ %invmsk%
echo. Network number .......... %ntwk%
echo. Broadcast address ....... %bct%
echo. First host IP address ... %ipfst%
echo. Last host IP address .... %iplst%
echo.
echo.
set o==O
set /p o= Would like to ping the host IP addresses [Y/N]?
if /I %o%==Y goto :PRALL
if /I %o%==N goto :AGAIN
:PRALL
For /l %%M in (%nta%,1,%ws%) do (
set m=%%M
For /l %%N in (%ntb%,1,%xs%) do (
set n=%%N
For /l %%O in (%ntc%,1,%ys%) do (
set o=%%O
For /l %%P in (%ntd%,1,%zs%) do (
set p=%%P
set i=%nta%&set j=%ntb%&set k=%ntc%&set l=%ntd%
set s=%ws%&set t=%xs%&set u=%ys%&set v=%zs%
if !i!.!j!.!k!.!l! EQU !m!.!n!.!o!.!p! (set /a count=1) ELSE (
if !s!.!t!.!u!.!v! NEQ !m!.!n!.!o!.!p! (ping !m!.!n!.!o!.!p! >> %TEMP%\ipcalc.txt)
)
)
)
)
)
%TEMP%\ipcalc.txt
del %TEMP%\ipcalc.txt
:AGAIN
cls
set c==N
echo.
set /p c= Would you like to start again[Y/N]?
if /I %c%==Y goto :BEGIN
if /I %c%==N goto :EOF
goto :AGAIN
:BADIP
echo. The ip address is not valid
pause
goto :BEGIN
:BADNET
echo. The network number and ip address cannot be the same
pause
goto :BEGIN
:BADBRD
echo. The broadcast address and ip address cannot be the same
pause
goto :BEGIN
:BADMASK
echo. The subnet number reported a bad mask
pause
goto :BEGIN
For anyone that might find it helpful heres my finished script.
@Echo Off
Setlocal Enabledelayedexpansion
echo
:BEGIN
cls
echo.
set /p getip= Enter the ip address?
cls
echo.
set /p getsbn= Enter the subnet mask?
cls
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%
if %ipa% GTR 255 goto :BADIP
if %ipb% GTR 255 goto :BADIP
if %ipc% GTR 255 goto :BADIP
if %ipd% GTR 255 goto :BADIP
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%
rem need to add valid nos 255,254,252,248,240,224,192,128,0
set /a vldcnt=0
for %%? in (0 128 192 224 240 248 252 254 255) do (if %sba% EQU %%? set /a vldcnt=%vldcnt%+1)
for %%? in (0 128 192 224 240 248 252 254 255) do (if %sbb% EQU %%? set /a vldcnt=%vldcnt%+1)
for %%? in (0 128 192 224 240 248 252 254 255) do (if %sbc% EQU %%? set /a vldcnt=%vldcnt%+1)
for %%? in (0 128 192 224 240 248 252 254 255) do (if %sbd% EQU %%? set /a vldcnt=%vldcnt%+1)
if %vldcnt% NEQ 4 goto :BADMASK
rem check that mask values drop to zero after any previous octet has a value less than 255
if %sba% EQU 255 (goto :SUBB) ELSE (
if %sbb% NEQ 0 goto :BADMASK
if %sbc% NEQ 0 goto :BADMASK
if %sbd% NEQ 0 goto :BADMASK
)
:SUBB
if %sbb% EQU 255 (goto :SUBC) ELSE (
if %sbc% NEQ 0 goto :BADMASK
if %sbd% NEQ 0 goto :BADMASK
)
:SUBC
if %sbc% EQU 255 (goto :CALCS) ELSE (
if %sbd% NEQ 0 goto :BADMASK
)
: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%
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%
if %nta% GTR 255 set nta=0
if %ntb% GTR 255 set ntb=0
if %ntc% GTR 255 set ntc=0
if %ntd% GTR 255 set ntd=0
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%
set i=%nta%&set j=%ntb%&set k=%ntc%&set l=%ntd%
set s=%ws%&set t=%xs%&set u=%ys%&set v=%zs%
set w=%ipa%&set x=%ipb%&set y=%ipc%&set z=%ipd%
if !i!.!j!.!k!.!l! EQU !w!.!x!.!y!.!z! goto :BADNET
if !s!.!t!.!u!.!v! EQU !w!.!x!.!y!.!z! goto :BADBRD
set /a ntdf=%ntd%+1
set /a zsl=%zs%-1
set ipfst=%nta%.%ntb%.%ntc%.%ntdf%
set iplst=%ws%.%xs%.%ys%.%zsl%
echo.
echo. IP address .............. %ipadd%
echo. Subnet mask ............. %sbmsk%
echo. Inverse mask ............ %invmsk%
echo. Network number .......... %ntwk%
echo. Broadcast address ....... %bct%
echo. First host IP address ... %ipfst%
echo. Last host IP address .... %iplst%
echo.
echo.
set o==O
set /p o= Would like to ping the host IP addresses [Y/N]?
if /I %o%==Y goto :PRALL
if /I %o%==N goto :AGAIN
:PRALL
For /l %%M in (%nta%,1,%ws%) do (
set m=%%M
For /l %%N in (%ntb%,1,%xs%) do (
set n=%%N
For /l %%O in (%ntc%,1,%ys%) do (
set o=%%O
For /l %%P in (%ntd%,1,%zs%) do (
set p=%%P
set i=%nta%&set j=%ntb%&set k=%ntc%&set l=%ntd%
set s=%ws%&set t=%xs%&set u=%ys%&set v=%zs%
if !i!.!j!.!k!.!l! EQU !m!.!n!.!o!.!p! (set /a count=1) ELSE (
if !s!.!t!.!u!.!v! NEQ !m!.!n!.!o!.!p! (ping !m!.!n!.!o!.!p! >> %TEMP%\ipcalc.txt)
)
)
)
)
)
%TEMP%\ipcalc.txt
del %TEMP%\ipcalc.txt
:AGAIN
cls
set c==N
echo.
set /p c= Would you like to start again[Y/N]?
if /I %c%==Y goto :BEGIN
if /I %c%==N goto :EOF
goto :AGAIN
:BADIP
echo. The ip address is not valid
pause
goto :BEGIN
:BADNET
echo. The network number and ip address cannot be the same
pause
goto :BEGIN
:BADBRD
echo. The broadcast address and ip address cannot be the same
pause
goto :BEGIN
:BADMASK
echo. The subnet number reported a bad mask
pause
goto :BEGIN