?
Solved

Extracting multiple Def Gateways in batch file

Posted on 2011-09-14
8
Medium Priority
?
934 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 56

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 7

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 56

Accepted Solution

by:
Bill Prew earned 1000 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 56

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 7

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 7

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 7

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 56

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

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Suggested Courses

764 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