Solved

Extracting multiple Def Gateways in batch file

Posted on 2011-09-14
8
927 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
  • 4
  • 4
8 Comments
 
LVL 53

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 5

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 53

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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
LVL 53

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 5

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 5

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 5

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 53

Expert Comment

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

~bp
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
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.

856 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