Solved

Extracting multiple Def Gateways in batch file

Posted on 2011-09-14
8
921 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 51

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 51

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
 
LVL 51

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 51

Expert Comment

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

~bp
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now