Look for text and execute conditional action

Microsoft Windows 7 home premium upgrade...
August 23, 2010 at 07:38:07
Specs: Windows 7
I need a batch file that executes "route print" and saves it to a log file. Then looks for either 192.168.0.2 or 192.168.0.3, and, if finds 192.168.0.2, does "route delete 0.0.0.0 && route add 0.0.0.0 mask 0.0.0.0 192.168.0.3". But, if finds 192.168.0.3 - does "route delete 0.0.0.0 && route add 0.0.0.0 mask 0.0.0.0 192.168.0.2"
That will change the default gateway automatically if primary ISP fails.

See More: Look for text and execute conditional action

Report •

#1
August 23, 2010 at 15:13:19
Untested, but I think this will work:

@ECHO OFF

ROUTE PRINT>"Route Print.log"

FINDSTR "192.168.0.2" "Route Print.log"
IF "%ERRORLEVEL%"=="0" (
	ROUTE DELETE 0.0.0.0
	ROUTE ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.3
)

FINDSTR "192.168.0.3" "Route Print.log"
IF "%ERRORLEVEL%"=="0" (
	ROUTE DELETE 0.0.0.0
	ROUTE ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.2
)

EXIT

Let me know!

Report •

#2
August 29, 2010 at 22:58:26
Thank you, Matt123! I really appreciate your help!
I solved the problem with this just adding 2 default gateways with the SAME metric.
This way, Windows automatically switches over to the next gateway in case primary fails.
So, just adding
route add 0.0.0.0 mask 0.0.0.0 192.168.0.2 metric 5
route add 0.0.0.0 mask 0.0.0.0 192.168.0.3 metric 5
solved the problem - no batch file needed in scheduler, windows tries gateways in listed order.
Thank you once again for your help!

Report •
Related Solutions


Ask Question