1

Can anyone help me on this:

I am trying to make batch which will:

  1. get_my_ip
  2. reset_router
  3. check if changed?
IF YES END, ELSE GO TO END


:GET_MY_IP

    for /f "skip=4 usebackq tokens=2" %%a in ('nslookup myip.opendns.com resolver1.opendns.com') do echo %%a

:RESET_ROUTER_AND_WAIT_TIMER

:CHECK_IP
IF NEW_IP == PREVIOUS_IP GOTO RESET_ROUTER AND INCREMENT TIMER TIME

:IP_IS_NOT_THE_SAME
END

For BATCH I am using command promt - command

for /f "skip=4 usebackq tokens=2" %%a in ('nslookup myip.opendns.com resolver1.opendns.com') do echo %%a

to get external IP address

1
  • Please can you post all your code ( From get_my_ip to IP_IS_NOT_THE _SAME ) Commented Sep 9, 2016 at 9:03

1 Answer 1

1

This code is just for the external IP, so you can start from this :

@echo off
set "IPLog=%userprofile%\Desktop\%~n0.txt"
Set "MyCommand=nslookup myip.opendns.com resolver1.opendns.com 2^>nul"
for /f "skip=4 delims=: tokens=2" %%a in ('%MyCommand%') do (
    Set "MyExtIP=%%a"
)
Call :Trim %MyExtIP%
echo My Extenal IP Adress is : %MyExtIP%
echo My Extenal IP Adress is : %MyExtIP% > "%IPLog%"
PAUSE & start "" "%IPLog%" & exit
::*************************************************************************************
:Trim <String>
set "vbsfile=%tmp%\%~n0.vbs"
(
    echo Wscript.echo Trim("%~1"^)
)>"%vbsfile%"
for /f "delims=" %%a in ('Cscript /nologo "%vbsfile%"') do ( set "MyExtIP=%%a" )
If Exist "%vbsfile%" Del "%vbsfile%"
exit /b
::*************************************************************************************

Edit : 10/09/2016

@echo off
:Checkloop
Call :GetIP
Call :CheckIP
rem goto:checkloop
::******************************************************************************
:GetIP
set "IPLog=%userprofile%\Desktop\%~n0.txt"
Set "MyCommand=nslookup myip.opendns.com resolver1.opendns.com 2^>nul"
for /f "skip=4 delims=: tokens=2" %%a in ('%MyCommand%') Do (
    Set "MyExtIP=%%a"
)
Call :Trim %MyExtIP%
echo My Extenal IP Adress is : "%MyExtIP%"
echo %MyExtIP% > "%IPLog%"
Exit /b
::*************************************************************************************
:CheckIP
If Not Exist "%IPLog%" Call :GetIP
rem Set /p CheckIP=<%IPLog%
for /f "delims= " %%g in ('Type "%IPLog%"') do ( set "CheckIP=%%g")
rem echo "%CheckIP%" from file & pause
If "%CheckIP%"=="%MyExtIP%" ( Cls & color 0A & echo the IP Adress = %CheckIP% does not changed
) else (
    Cls & Color 0C & echo The IP Adress is changed to %MyExtIP%
)
Timeout /T 30 /Nobreak
goto:checkloop
::*************************************************************************************
:Trim <String>
set "vbsfile=%tmp%\%~n0.vbs"
(
    echo Wscript.echo Trim("%~1"^)
)>"%vbsfile%"
for /f "delims=" %%a in ('Cscript /nologo "%vbsfile%"') do ( set "MyExtIP=%%a" )
If Exist "%vbsfile%" Del "%vbsfile%"
exit /b
::*************************************************************************************
Sign up to request clarification or add additional context in comments.

4 Comments

It seems this only gets the public IP address. It will not get the external (WAN) IP address if the ISP is using CGN. For CGN, it will get the ISP's public IP address, and this may not be useful. More and more ISPs are using CGN (just search for it on Super User), and it is causing problems for end-users. Unfortunately, it seems most ISPs using CGN are not using the IPv4 range reserved for CGN (`100.64.0.0/10'), and they are using RFC 1918 private address space which may conflict with the customers' network addressing.
Hi thanks for answer! Can you help me finalize this now. If I put this code in some "check_ip.bat" my final batch should look something like: @Echo off check_ip.bat :START reset_router.bat check_ip.bat IF IP_previous==IP_NEW goto START else FINISH: :FINISH exit This code should be placed in some "final.bat" How to handle this IF part?
@Jovan Check my last edit and i don't know how to reset your router ?
@Hackoo I am doing it via bat file (which using/executing TST10 commands for deletion and creation of PoEE). Usual telnet don't have waiting option (as per my knowledge) So I need now to now where to put reset.bat in your excellent code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.