Solved

Extracting multiple Def Gateways in batch file

Posted on 2011-09-14
8
931 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 55

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 55

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 55

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 55

Expert Comment

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

~bp
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

No single Antivirus application (despite claims by manufacturers) will catch or protect you from all Virus / Malware or Spyware threats. That doesn't stop you from further protecting yourself however - and this article is to show you how.
Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

717 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