Announcement

Collapse
No announcement yet.

Batch File to Find and ping default gateway automatically?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch File to Find and ping default gateway automatically?

    Hi ,

    I wanted to know how can I create a BAT file which finds the default gateway and pings it automatically.

    Can you people please help.

    Cheers,
    Prad

  • #2
    Re: Batch File to Find and ping default gateway automatically?

    Tried searching? This question on this forum last year.

    What do you have so far? If you show us what your script looks like and tell us the problem you're having with it, then maybe someone will be able to help you.

    http://forums.petri.com/announcement.php?f=68
    Gareth Howells

    BSc (Hons), MBCS, MCP, MCDST, ICCE

    Any advice is given in good faith and without warranty.

    Please give reputation points if somebody has helped you.

    "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

    "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

    Comment


    • #3
      Re: Batch File to Find and ping default gateway automatically?

      Hi ,

      Thanks for replying.
      I infact searched for it and found this thread :-

      ".BAT to ping the default gateway"
      https://www.petri.com/forums/node/37138

      The bat that was there in it worked fine and pinged the default gateway and sent the output to a text file.

      But in the BAT I couldnt understand how this part works :-


      @For /f "tokens=3" %%* in (
      'route.exe print ^|findstr "\<0.0.0.0\>"'
      ) Do @Set "DefaultGateway=%%*"



      I am sorry but I am not that good at BAT files and still learning and "find" searches for something but how is the output of the Find used and why does the findstr look for 0.0.0.0.

      Can you please suggest

      Regards,
      Prad


      Originally posted by gforceindustries View Post
      Tried searching? This question on this forum last year.

      What do you have so far? If you show us what your script looks like and tell us the problem you're having with it, then maybe someone will be able to help you.
      Last edited by Rems; 28th June 2010, 17:49. Reason: added reference using [thread]-tags

      Comment


      • #4
        Re: Batch File to Find and ping default gateway automatically?

        According to the first result for "findstr":

        Originally posted by TechNet
        Findstr
        Searches for patterns of text in files using regular expressions.

        Notice how the pattern in the script is surrounded with \< \> control characters?


        - useful isn't it

        In any case, did you actually try looking at the output of route print at a command prompt? I guess not, since that would have answered your question for you...
        Gareth Howells

        BSc (Hons), MBCS, MCP, MCDST, ICCE

        Any advice is given in good faith and without warranty.

        Please give reputation points if somebody has helped you.

        "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

        "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

        Comment


        • #5
          Re: Batch File to Find and ping default gateway automatically?

          Originally posted by im_prad View Post


          I infact searched for it and found this thread :-

          ".BAT to ping the default gateway"
          http://forums.petri.com/showthread.php?t=36303


          ... But in the BAT I couldnt understand how this part works :-
          Code:
          @For /f "tokens=3" %%* in (
             'route.exe print ^|findstr "\<0.0.0.0\>"'
             ) Do @Set "DefaultGateway=%%*"
          
          how is the output of the Findstr used
          and why does the findstr look for 0.0.0.0


          FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found. (from: http://ss64.com/nt/for_cmd.html)

          The For_command here is: route.exe print. And the Do_command is Set "DefaultGateway=.....

          The output of the For_command is piped ( | ) to the Findstr filter because we only want to process the line showing 0.0.0.0 in the column 'network destination' - on that line there is also the IP address of the default gateway.
          (you must add one escape sign ( ^ ) in front of the pipe sign that is used in the For_command).

          Instead of the Find command use the Findstr command that'll allow us to add word boundries ( \< and \> ) to make sure we search for 0.0.0.0 exactly and not hit also 10.0.0.0.

          Break that line at spaces (is the For /F default setting), in this case we'll get five parts (tokens). The third token is the IP address of the default gateway. We only want this third token so we define the option tokens=3.

          \Rems

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment

          Working...
          X