1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with Windows scripting or batch file

Discussion in 'Windows 7' started by Dick Kimball, Aug 30, 2010.

  1. Dick Kimball

    Dick Kimball Flightless Bird

    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?
     
  2. Gene E. Bloch

    Gene E. Bloch Flightless Bird

    On Mon, 30 Aug 2010 17:15:54 -0500, Dick Kimball wrote:

    > 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 :)

    --
    Gene E. Bloch (Stumbling Bloch)
     
  3. Dick Kimball

    Dick Kimball Flightless Bird

    On Mon, 30 Aug 2010 16:02:32 -0700, "Gene E. Bloch"
    <not-me@other.invalid> wrote:

    >On Mon, 30 Aug 2010 17:15:54 -0500, Dick Kimball wrote:
    >
    >> 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 :)


    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.
     
  4. Gene E. Bloch

    Gene E. Bloch Flightless Bird

    On Mon, 30 Aug 2010 21:20:48 -0500, Dick Kimball wrote:

    > On Mon, 30 Aug 2010 16:02:32 -0700, "Gene E. Bloch"
    > <not-me@other.invalid> wrote:
    >
    >>On Mon, 30 Aug 2010 17:15:54 -0500, Dick Kimball wrote:
    >>
    >>> 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 :)

    >
    > 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...

    --
    Gene E. Bloch (Stumbling Bloch)
     
  5. Dick Kimball

    Dick Kimball Flightless Bird

    On Mon, 30 Aug 2010 21:26:08 -0700, "Gene E. Bloch"
    <not-me@other.invalid> wrote:

    >On Mon, 30 Aug 2010 21:20:48 -0500, Dick Kimball wrote:
    >
    >> On Mon, 30 Aug 2010 16:02:32 -0700, "Gene E. Bloch"
    >> <not-me@other.invalid> wrote:
    >>
    >>>On Mon, 30 Aug 2010 17:15:54 -0500, Dick Kimball wrote:
    >>>
    >>>> 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 :)

    >>
    >> 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...



    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
     
  6. Gene E. Bloch

    Gene E. Bloch Flightless Bird

    On Tue, 31 Aug 2010 10:06:18 -0500, Dick Kimball wrote:

    > On Mon, 30 Aug 2010 21:26:08 -0700, "Gene E. Bloch"
    > <not-me@other.invalid> wrote:
    >
    >>On Mon, 30 Aug 2010 21:20:48 -0500, Dick Kimball wrote:
    >>
    >>> On Mon, 30 Aug 2010 16:02:32 -0700, "Gene E. Bloch"
    >>> <not-me@other.invalid> wrote:
    >>>
    >>>>On Mon, 30 Aug 2010 17:15:54 -0500, Dick Kimball wrote:
    >>>>
    >>>>> 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 :)
    >>>
    >>> 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...

    >
    >
    > 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 :)

    --
    Gene E. Bloch (Stumbling Bloch)
     

Share This Page