Help with Windows scripting or batch file

D

Dick Kimball

I'm trying to write a script or batch file that will toggle a network
route between two possible gateway values. It needs to parse the
output of the "route print" command to get the value of the current
gateway, then toggle it to the other value.

For example, using "route print | find "1.2.3.4" I get the following:
1.2.3.4 255.255.255.0 192.168.15.1 1

In the above case, the current gateway IP is 192.168.15.1, so I would
need to change it to 192.168.16.1. If it were already set to
192.168.16.1, I would need to change it back to 192.168.15.1. So in
effect, I'm simply toggling the gateway IP back and forth between two
IP address values.

It's easy enough to do everything manually at the command prompt, but
I'd like to wrap it up in a single batch file or script.

Commands I'm looking at for this:
route print
route change
for /F
if
find

Does anyone have experience with this, or a pointer to a good
resource?
 
G

Gene E. Bloch

I'm trying to write a script or batch file that will toggle a network
route between two possible gateway values. It needs to parse the
output of the "route print" command to get the value of the current
gateway, then toggle it to the other value.

For example, using "route print | find "1.2.3.4" I get the following:
1.2.3.4 255.255.255.0 192.168.15.1 1

In the above case, the current gateway IP is 192.168.15.1, so I would
need to change it to 192.168.16.1. If it were already set to
192.168.16.1, I would need to change it back to 192.168.15.1. So in
effect, I'm simply toggling the gateway IP back and forth between two
IP address values.

It's easy enough to do everything manually at the command prompt, but
I'd like to wrap it up in a single batch file or script.

Commands I'm looking at for this:
route print
route change
for /F
if
find

Does anyone have experience with this, or a pointer to a good
resource?
The first thing that pops into my mind is to pipe the output of route
print into find "192.168.15.1", check the return code, and if 0 do one
thing, else the other.

If you're a belt and suspenders sorta guy, make the change only if the
find is true, and do the corresponding thing for the other IP address.
That way, no change is made if the result contains neither address. You
could add code to the batch file to report the latter situation.

I am proposing this, not testing it, so I'm leaving you on your own.

Meanwhile, there's always the hope that someone else will reply with a
different suggestion :)
 
D

Dick Kimball

The first thing that pops into my mind is to pipe the output of route
print into find "192.168.15.1", check the return code, and if 0 do one
thing, else the other.

If you're a belt and suspenders sorta guy, make the change only if the
find is true, and do the corresponding thing for the other IP address.
That way, no change is made if the result contains neither address. You
could add code to the batch file to report the latter situation.

I am proposing this, not testing it, so I'm leaving you on your own.

Meanwhile, there's always the hope that someone else will reply with a
different suggestion :)
Done, thanks. :)

In p-code, I check for the .15 route first. If found, change it to
..16, print a success message, pause, and exit.

If the .15 route is not found, I check for the .16 route. If found,
change it to .15, print a success message, pause, and exit.

If the .16 route is not found, it means neither route is present, so
print a message to that effect, pause, and exit.

I've run it a few dozen times and it works perfectly.
 
G

Gene E. Bloch

Done, thanks. :)

In p-code, I check for the .15 route first. If found, change it to
.16, print a success message, pause, and exit.

If the .15 route is not found, I check for the .16 route. If found,
change it to .15, print a success message, pause, and exit.

If the .16 route is not found, it means neither route is present, so
print a message to that effect, pause, and exit.

I've run it a few dozen times and it works perfectly.
Looks like you're a happy camper now. Good...
 
D

Dick Kimball

Looks like you're a happy camper now. Good...

Yep! It's probably a fairly specialized need, but in case anyone else
has a similar use, here's what I ended up with.



@ECHO OFF
:: This file checks for the presence of either of two specific
:: entries in the routing table. If either is found, the
:: entry is changed to the other. If neither is found, the
:: user is alerted and the program ends.

echo.
echo BEFORE:
route print | find "1.2.3.0"


:: Check for 3.1 route, if it exists change it to 4.1 and exit
route print | find "1.2.3.0 255.255.255.0 192.168.3.1 1"
if %errorlevel% ==0 goto CHANGE34


:: Check for 4.1 route, if exists change to 3.1 and exit
route print | find "1.2.3.0 255.255.255.0 192.168.4.1 1"
if %errorlevel% ==0 goto CHANGE43


:: Neither exists - let me know, pause, then exit
:: Could also easily just create the route here
echo Route does not exist! Not found in routing table.
pause
GOTO END


:CHANGE34
route -p change 1.2.3.0 mask 255.255.255.0 192.168.4.1
GOTO END


:CHANGE43
route -p change 1.2.3.0 mask 255.255.255.0 192.168.3.1
GOTO END


:END
echo.
echo ----------------------------
echo.
echo AFTER:
route print | find "1.2.3.0"

echo.
echo DONE!
pause
 
G

Gene E. Bloch

Yep! It's probably a fairly specialized need, but in case anyone else
has a similar use, here's what I ended up with.



@ECHO OFF
:: This file checks for the presence of either of two specific
:: entries in the routing table. If either is found, the
:: entry is changed to the other. If neither is found, the
:: user is alerted and the program ends.

echo.
echo BEFORE:
route print | find "1.2.3.0"


:: Check for 3.1 route, if it exists change it to 4.1 and exit
route print | find "1.2.3.0 255.255.255.0 192.168.3.1 1"
if %errorlevel% ==0 goto CHANGE34


:: Check for 4.1 route, if exists change to 3.1 and exit
route print | find "1.2.3.0 255.255.255.0 192.168.4.1 1"
if %errorlevel% ==0 goto CHANGE43


:: Neither exists - let me know, pause, then exit
:: Could also easily just create the route here
echo Route does not exist! Not found in routing table.
pause
GOTO END


:CHANGE34
route -p change 1.2.3.0 mask 255.255.255.0 192.168.4.1
GOTO END


:CHANGE43
route -p change 1.2.3.0 mask 255.255.255.0 192.168.3.1
GOTO END


:END
echo.
echo ----------------------------
echo.
echo AFTER:
route print | find "1.2.3.0"

echo.
echo DONE!
pause
Thanks. When I looked at find /? I noticed that it didn't document its
return code (I mean errorlevel).

I could have experimented or I could have looked in the O'Reilly book if
I could have found where I had put it, but I didn't need it myself, so I
trusted you to do it. And you did :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top