Solved Check LAN IP is correct firewall forwarded IP for Server

Netgear / Dg834g
November 20, 2013 at 17:56:29
Specs: Windows XP, HexaCore AMD FX-6100, 1400 MHz (7 x 200) / 4gb DDR
Can someone help me with a small problem, I spent a lot of time on here to find a way to make my batch file do some basic checks before launching programs on my server, the main one is to make sure the local IP is the one in port forwarding. Some times I swap over to my backup server (I CHANGE IP PER MACHINE IN THE ROUTER SETTINGS ITS EASIER TO SWAP 2 MACHINES AROUND THIS WAY) but my other server will sometimes be run in maintenance mode, when it IS running in maintenance mode I have configured my batch schedules to skip loading SERVER applications at start up. The problem is ONE server has more than one network adapter so it is causing the applications to not run when they are supposed to as the batch script is looking at the WRONG adapter and getting wrong data.

Q: How can I find the ACTIVE IN USE ADAPTER ?

here is the batch code is question below!

:: BEGIN BATCH CODE
@echo off
setlocal enabledelayedexpansion
:: ----------------------------
set state="C:\state\status.txt"
:: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
if exist echo. del echo.
set thispc=192.168.2.10
ipconfig /all | find "IP Address" > %computername%.txt
FOR /F "tokens=2 delims=:" %%a in (%computername%.txt) do set IP=%%a
del %computername%.txt
set IP=%IP:~1%
if "%thispc%"=="%ip%" goto true
if not "%thispc%"=="%ip%" goto false
goto end
:true
echo TRUE
>> %state% echo TRUE The Server IP is port forwarded
sleep 5
cls
goto loadaps

:false
echo FALSE
sleep 10
cls
echo.
echo Expected IP address is "%thispc%" actual ip is "%IP%"
>> %state% echo as this PC is not using the correct
>> %state% echo ip address (NO PORT FORWARD)
>> %state% echo the server will not start the
>> %state% echo usual SERVER APPLCATIONS.
goto nodo
:loadaps
:: -----------------------------------
:: APPLICATIONS ARE LOADED IN THIS
:: SECTION OMMITTED TO MAKE THIS
:: FILE SMALLER
:: -----------------------------------
:end
:nodo
pause
cls
endlocal
cls
:: END BATCH CODE

If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://www.book-stores.com/angels-

message edited by Ortorea


See More: Check LAN IP is correct firewall forwarded IP for Server

Report •

#1
November 20, 2013 at 18:50:40
I found some other commands that seem to work better but I am getting a trailing space problem !

netsh diag show ip | find "IPAddress" > %computername%.txt
FOR /F "tokens=2 delims=,=" %%a in (%computername%.txt) do set IP=%%a

Michelle


OK so this works but the trailing space is annoying me !!!

:: START BATCH FILE IP CHECK FOR PORT FORWARDED PC

@echo off
setlocal enabledelayedexpansion
:: ----------------------------
set state="C:\state\status.txt"
:: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
if exist echo. del echo.
:: NOTE NEED TO ADD A TRAILING SPACE AFTER BELOW IP FOR IT TO WORK
set thispc=192.168.2.10
netsh diag show ip | find "IPAddress" > %computername%.txt
FOR /F "tokens=2 delims=,=" %%a in (%computername%.txt) do set IP=%%a
del %computername%.txt
set IP=%IP:~1%
if "%thispc%"=="%ip%" goto true
if not "%thispc%"=="%ip%" goto false
goto end
:true
echo TRUE
>> %state% echo TRUE The Server IP is port forwarded
sleep 2
cls
goto loadaps

:false
echo FALSE
sleep 2
cls
echo.
echo Expected IP address is "%thispc%" actual ip is "%IP%"
>> %state% echo as this PC is not using the correct
>> %state% echo ip address (NO PORT FORWARD)
>> %state% echo the server will not start the
>> %state% echo usual SERVER APPLCATIONS.
goto nodo
:loadaps
:: -----------------------------------
:: APPLICATIONS ARE LOADED IN THIS
:: SECTION OMMITTED TO MAKE THIS
:: FILE SMALLER
:: -----------------------------------
:end
:nodo
pause
cls
endlocal
cls

:: END BATCH FILE IP CHECK FOR PORT FORWARDED PC


If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://www.book-stores.com/angels-

message edited by Ortorea


Report •

#2
November 20, 2013 at 23:24:29
I found my answer to the trailing spaces problem here: http://www.computing.net/answers/pr...

So now it works great thanks to Mechanix2Go :D

:: BEGIN BATCH FILE

@echo off
setlocal enabledelayedexpansion
:: ----------------------------
set state="C:\state\status.txt"
set thispc=192.168.2.26
netsh diag show ip | find "IPAddress" > %computername%.txt
:: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
For /F "tokens=2 delims=,= " %%a in (%computername%.txt) do (
:: strips out spaces in text files
call :sub1 %%a
>> IP# echo.!S!
)

goto ipaquired
:sub1
set S=%*
goto :eof

:ipaquired
set /p IP=<IP#
del IP#
:: ##################################
if "%thispc%"=="%ip%" goto true
if not "%thispc%"=="%ip%" goto false
goto nodo
:true
echo TRUE
>> %state% echo TRUE The Server IP is the port forwarded One Set in Router
>> %state% echo Expected IP address of %computername% is %thispc% actual ip is %IP%
sleep 2
cls
goto loadaps

:false
echo FALSE
sleep 2
cls
echo.
echo Expected IP address is "%thispc%" actual ip is "%IP%"
>> %state% echo Expected IP address of %computername% is %thispc% actual ip is %IP%
>> %state% echo as this PC is not using the correct
>> %state% echo ip address (NO PORT FORWARD)
>> %state% echo the server will not start the
>> %state% echo usual SERVER APPLCATIONS.
goto nodo
:loadaps
:: -----------------------------------
:: APPLICATIONS ARE LOADED IN THIS
:: SECTION OMMITTED TO MAKE THIS
:: FILE SMALLER
:: -----------------------------------
:nodo
pause
endlocal
cls

:: END BATCH FILE

If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://www.book-stores.com/angels-


Report •

#3
November 21, 2013 at 18:49:16
✔ Best Answer
Hello :-O

that worked fine, but just for posterity, this also did (begin snippet):

for /f "tokens=3" %%a in ('netsh diag show ip ^|find /i "ipaddress"') do (
set ip=%%a
goto :: ################################## where this would be a real label equiv
)
...
:: ##################################
echo [%ip%]
pause
::==== end snip
this eliminates two temp files and some lines of code, but functionally the same.


Report •

Related Solutions

#4
November 22, 2013 at 01:57:11
Hello nbrane :) so glad your still doing your coding magic that is awesome I am so glad your still around, seems few people are noticing my posts lol I will integrate that and test thanks :)

Michelle

PS I made another variation on a Router Reboot script, nbrane you might find this useful also as their is nothing worse than spending ages trying to figure out why the Internet is not working how it should, reason I suggest you have a look is I am interested in an extra piece of code I will add that to the actual post http://www.computing.net/answers/pr...

If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://www.book-stores.com/angels-

message edited by Ortorea


Report •

Ask Question