findstr default gateway

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lazna
Posts: 62
Joined: 27 Dec 2012 10:54

findstr default gateway

#1 Post by lazna » 23 Nov 2022 08:07

Trying to get default route info from netstat output, but have no luck. Findstr also accept lines containing string

Code: Select all

224.0.0.0
what (by my opinion) does not meet pattern. What am I doing wrong?

Code: Select all

netstat -rn | findstr "0.0.0.0"
BTW: Need to get default route exactly from netstat output for some reason, not by netsh, WMI, PS or any other way.

miskox
Posts: 626
Joined: 28 Jun 2010 03:46

Re: findstr default gateway

#2 Post by miskox » 23 Nov 2022 11:09

You should provide more info. Here is the output from my netstat command:

Code: Select all

c:\>netstat -rn|findstr "0.0.0.0"
          0.0.0.0          0.0.0.0      192.168.8.1    192.168.8.108     25
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
        224.0.0.0        240.0.0.0         On-link     192.168.8.108    281
So what info do you need?

Try this:

Code: Select all

c:\>netstat -rn|findstr /L /C:" 0.0.0.0"
          0.0.0.0          0.0.0.0      192.168.8.1    192.168.8.108     25
Add space before the first 0. Note /C switch.

You should read viewtopic.php?f=3&t=6108#p38525
Read this too:

Code: Select all

findstr /?
Provide your netstat output and what you want to get.

Saso

lazna
Posts: 62
Joined: 27 Dec 2012 10:54

Re: findstr default gateway

#3 Post by lazna » 23 Nov 2022 11:50

expect only three lines containing pattern, but got more lines

Code: Select all

netstat -rn | findstr /C:"0.0.0.0"
          0.0.0.0          0.0.0.0      10.12.70.65      10.12.70.66    281
          0.0.0.0          0.0.0.0      192.168.1.1    192.168.1.236     50
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
        224.0.0.0        240.0.0.0         On-link    169.254.71.163    281
        224.0.0.0        240.0.0.0         On-link       10.12.70.66    281
        224.0.0.0        240.0.0.0         On-link     192.168.1.236    306
          0.0.0.0          0.0.0.0      10.12.70.65  Default

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: findstr default gateway

#4 Post by atfon » 23 Nov 2022 12:24

lazna wrote:
23 Nov 2022 11:50
expect only three lines containing pattern, but got more lines

Code: Select all

netstat -rn | findstr /C:"0.0.0.0"
          0.0.0.0          0.0.0.0      10.12.70.65      10.12.70.66    281
          0.0.0.0          0.0.0.0      192.168.1.1    192.168.1.236     50
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
        224.0.0.0        240.0.0.0         On-link    169.254.71.163    281
        224.0.0.0        240.0.0.0         On-link       10.12.70.66    281
        224.0.0.0        240.0.0.0         On-link     192.168.1.236    306
          0.0.0.0          0.0.0.0      10.12.70.65  Default
You missed what miskox said about putting a space before the first 0 in your findstr command. It should be like this:

Code: Select all

netstat -rn|findstr /L /C:" 0.0.0.0"

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: findstr default gateway

#5 Post by OJBakker » 23 Nov 2022 12:25

Your output is correct because literal string "0.0.0.0" is part of string "240.0.0.0".
Add a space to avoid these false positives.
netstat -rn | findstr /C:" 0.0.0.0"

lazna
Posts: 62
Joined: 27 Dec 2012 10:54

Re: findstr default gateway

#6 Post by lazna » 23 Nov 2022 12:55

Thanks all!

Finaly solved by

Code: Select all

netstat -rn | findstr /R "\<0.0.0.0"
, it look to me better than put space as a part of patterns.

BTW: Sorry unattential reading

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: findstr default gateway

#7 Post by Hackoo » 26 Nov 2022 00:47

You can create a batch file in order to get the Default Gateway and the Local IP Address from netstat command :wink:

Code: Select all

@echo off
Title Get Default Gateway and Local IP Address from netstat command
@for /f "tokens=3,4 delims= " %%a in ('netstat -rn ^| findstr "\<0.0.0.0"') do ( 
	Set "Gateway=%%a"
	Set "MyIP=%%b"
)
echo( Gateway = %Gateway%
echo( MyIP    = %MyIp%
pause

Post Reply