Solved

Extracting multiple Def Gateways in batch file

Posted on 2011-09-14
8
928 Views
Last Modified: 2012-08-14
I am writing a batch file to extract the Default Gateway so that I can ping any and all, and pipe the results to a text file.  

I use Route Print to get the default gateway detail and pipe it thru Findstr.

It works fine when there is only one default gateway, however some have a VPN adapter as well as the Ethernet adapter.  

Route print returns data, in particular the data shown below
IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway              Interface           Metric
          0.0.0.0                   0.0.0.0             5.0.0.1                 5.0.0.3              18000
          0.0.0.0                   0.0.0.0             192.168.0.200     192.168.0.12    10
          5.0.0.0                   255.0.0.0         On-link                 5.0.0.3              9256

When I use the code below (kindly used from http://www.petri.co.il/forums/showthread.php?t=47121) I can get the 1st Def GW (5.0.0.1) in the list, but cant crack extracting the second (192.168.0.200)

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


H E L P
0
Comment
Question by:mbkitmgr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36540171
What do you want to do with the second one (and beyond)?  Do you want to set them in different variables?  Or maybe concatenate them onto the same variable delimited with commas, then you can loop over them with a FOR?  Maybe something like:

@echo off
setlocal EnableDelayedExpansion
set Count=0
For /f "tokens=3" %%* in ('route.exe print ^|findstr "\<0.0.0.0\>"') Do (
  Set /A Count+=1
  Set "DefaultGateway_!Count!=%%*"
)

Open in new window

or

@echo off
setlocal EnableDelayedExpansion
Set DefaultGateway=
For /f "tokens=3" %%* in ('route.exe print ^|findstr "\<0.0.0.0\>"') Do (
  Set /A Count+=1
  If defined DefaultGateway (
    Set "DefaultGateway=!DefaultGateway!,%%*"
  ) else (
    Set "DefaultGateway=%%*"
  )
)

Open in new window

~bp
0
 
LVL 6

Author Comment

by:mbkitmgr
ID: 36540289
Hi billprew - yes the goal is to identify each gateway, ping each one redirected to a text file (Append >>), via the bacth file.  I wanted create a general purpose batch file that can be sent to a client with connectivity issues.

I began with some sample code from Petri forums

rem source
rem http://www.petri.co.il/forums/showthread.php?t=47121

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

::# Redirect output to a textfile
> "pingtest.txt" (
  echo [%Date% %time%] - %title%
  ping %DefaultGateway%
)
notepad pingtest.txt

Open in new window

0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 36540321
Okay, I think the biggest issue is getting the ping code inside the FOR loop.  I'd recommend something more like this.

@echo off
rem source
rem http://www.petri.co.il/forums/showthread.php?t=47121

REM Loop through all gateways
For /f "tokens=3" %%A in ('route.exe print ^|findstr "\<0.0.0.0\>"') Do (
  > "pingtest.txt" (
    echo [%Date% %time%] - %title%
    ping %%A
  )
)

notepad pingtest.txt

Open in new window

~bp
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 54

Expert Comment

by:Bill Prew
ID: 36540325
And I think you are going to have to change:

  > "pingtest.txt" (

to

  >> "pingtest.txt" (

to get all output.

~bp
0
 
LVL 6

Author Comment

by:mbkitmgr
ID: 36540578
When executed I get the result below
[Thu 15/09/2011 12:08:35.01] - [E5500]

Pinging 5.0.0.1 with 32 bytes of data:
Reply from 5.0.0.1: bytes=32 time=317ms TTL=42
Reply from 5.0.0.1: bytes=32 time=317ms TTL=42
Reply from 5.0.0.1: bytes=32 time=317ms TTL=42
Reply from 5.0.0.1: bytes=32 time=316ms TTL=42
Ping statistics for 5.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 316ms, Maximum = 317ms, Average = 316ms

[Thu 15/09/2011 12:09:07.37] - [E5500]
Pinging 5.0.0.1 with 32 bytes of data:
Reply from 5.0.0.1: bytes=32 time=316ms TTL=42
Reply from 5.0.0.1: bytes=32 time=317ms TTL=42
Reply from 5.0.0.1: bytes=32 time=317ms TTL=42
Reply from 5.0.0.1: bytes=32 time=316ms TTL=42
Ping statistics for 5.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 316ms, Maximum = 317ms, Average = 316ms

[Thu 15/09/2011 12:09:07.37] - [E5500]
Pinging 192.168.0.200 with 32 bytes of data:
Reply from 192.168.0.200: bytes=32 time<1ms TTL=255
Reply from 192.168.0.200: bytes=32 time<1ms TTL=255
Reply from 192.168.0.200: bytes=32 time<1ms TTL=255
Reply from 192.168.0.200: bytes=32 time<1ms TTL=255
Ping statistics for 192.168.0.200:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

[Thu 15/09/2011 12:09:07.37] - [E5500]
Pinging 5.0.0.1 with 32 bytes of data:
Reply from 5.0.0.1: bytes=32 time=316ms TTL=42
Reply from 5.0.0.1: bytes=32 time=317ms TTL=42
Reply from 5.0.0.1: bytes=32 time=317ms TTL=42
Reply from 5.0.0.1: bytes=32 time=317ms TTL=42
Ping statistics for 5.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 316ms, Maximum = 317ms, Average = 316ms

Open in new window

0
 
LVL 6

Author Closing Comment

by:mbkitmgr
ID: 36540697
A very quick and consise response.  i'll post my finished script for collective use and refinement
0
 
LVL 6

Author Comment

by:mbkitmgr
ID: 36540701
The finished script (for now).  I chose to use Netsh for XP machines as its diag option is great.  For all the others I needed to single out the default gateways and ping each one.  It was XP's netsh diag that I wanted to emulate.

ECHO.                                                    >AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ...........  Ask Itee Pingtest ! ! ! !  ........... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo [%Date% %time%] - PC ID [%COMPUTERNAME%] >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo      Get IPConfig of Workstation
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo Get IpConfig of %COMPUTERNAME% .................... >>AskIteeLanTest_%COMPUTERNAME%.txt
ipconfig /all  >>AskIteeLanTest_%COMPUTERNAME%.txt
:: Find Windows XP
VER | FINDSTR "5."
IF %ERRORLEVEL% LEQ 0 GOTO xp
GOTO Others
:XP
ECHO.                                                    >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ...........  If XP use Netsh ! ! ! ! !  ........... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
NETSH DIAG PING ADAPTER  >>AskIteeLanTest_%COMPUTERNAME%.txt
GOTO DNS

:Others
ECHO.                                                    >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ...........  Get and Ping DEF GW ! ! !  ........... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
REM Loop through all gateways
For /f "tokens=3" %%A in ('route.exe print ^|findstr "\<0.0.0.0\>"') Do (
  >> "AskIteeLanTest_%COMPUTERNAME%.txt" (
    echo [%Date% %time%] - [%computername%]
    ping %%A
  )
)

ECHO.                                                    >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ...........  Dump Route details! ! ! !  ........... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Route print  >>AskIteeLanTest_%COMPUTERNAME%.txt
ECHO.                                                    >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ...........  Ping Loopback   ! ! ! ! !  ........... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
ping 127.0.0.1 >>AskIteeLanTest_%COMPUTERNAME%.txt

:DNS
ECHO.                                                    >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ...........  Ping Google using DNS ! !  ........... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
ping www.google.com  >>AskIteeLanTest_%COMPUTERNAME%.txt
ECHO.                                                    >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ...........  Ping telstra using DNS  !  ........... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
ping mail.telstra.com >>AskIteeLanTest_%COMPUTERNAME%.txt
ECHO.                                                    >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ...........  F I N I S H E D ! ! ! ! !  ........... >>AskIteeLanTest_%COMPUTERNAME%.txt
Echo ................................................... >>AskIteeLanTest_%COMPUTERNAME%.txt
notepad AskIteeLanTest_%COMPUTERNAME%.txt

Open in new window

0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36542181
Glad that helped, thanks.

~bp
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question