Batch file to find and ping default gateway

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kainjinez
Posts: 1
Joined: 28 Sep 2013 14:12

Batch file to find and ping default gateway

#1 Post by kainjinez » 28 Sep 2013 14:23

Hi all. I'm searching for a .bat file to find and ping default gateway which i can connect to Internet. Because my LAN connection at my school doesn't support DHCP and I have to find true default gateway anytime. Please help me.
I've found a Batch code on internet. But it wasn't work.

Code: Select all

@Set title=Pinging the Default Gateway...
@(color 6A) & (@Title %title%)
@echo please wait...

::# 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%
)
@echo Done
pause.


When i ran it as admin, it said "Done" but it wasn't write true gateway to pingtest.txt. I'm using Windows 7 and I want it works on both W7 and Server 2k too. Thanks alot!
Thanks!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file to find and ping default gateway

#2 Post by penpen » 28 Sep 2013 16:08

First to say:
The script you've found only returns the ip of the actually used gateway and not the ip adress of the default gateway.
If you haven't specify a gateway in the TCP/IP properties of your lan connection then the result is empty always.
It will be empty, too, if none of your gateways you have specified are turned on.

Additionally:
The easiest way is to ask for the gateway adress, i assume it will nearly never be changed.
The only reason: If a damaged hardware is replaced, it may be that the adress changes.

penpen

Post Reply