Page 1 of 1

findstr default gateway

Posted: 23 Nov 2022 08:07
by lazna
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.

Re: findstr default gateway

Posted: 23 Nov 2022 11:09
by miskox
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

Re: findstr default gateway

Posted: 23 Nov 2022 11:50
by lazna
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

Re: findstr default gateway

Posted: 23 Nov 2022 12:24
by atfon
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"

Re: findstr default gateway

Posted: 23 Nov 2022 12:25
by OJBakker
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"

Re: findstr default gateway

Posted: 23 Nov 2022 12:55
by lazna
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

Re: findstr default gateway

Posted: 26 Nov 2022 00:47
by Hackoo
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