Code: Select all
netstat -rn | findstr "\<0.0.0.0\>"
Code: Select all
route print 0.0.0.0 | findstr "\<0.0.0.0\>"
Is it 100% reliable way for detecting internal IP address?
Moderator: DosItHelp
Code: Select all
netstat -rn | findstr "\<0.0.0.0\>"
Code: Select all
route print 0.0.0.0 | findstr "\<0.0.0.0\>"
If your end goal is to find all devices on your network the answer is no.siberia-man wrote: ↑17 Jan 2022 12:04My question
Is it 100% reliable way for detecting internal IP address?
Yes. It's exactly what I want. But...
I agree that for isolated hosts it gives falsy result. However, in any case, it gives the interfaces associated with the default gateways. I hope only it will be the only interface or the only gateway.
Code: Select all
netstat -rn | findstr "\<0.0.0.0\>"
Code: Select all
route print 0.0.0.0 | findstr "\<0.0.0.0\>"
The routing table is the same regardless of the program that used to retrieve the table. There could be difference in the presentation layer, but the table is the same. In your case the presentation is also exactly the same, So there is no difference, except for additional data that netstat displays which are unrelated to the ipv4 routing table.siberia-man wrote: ↑26 Jan 2022 09:28My concern is about whether the next commands give the same result or not.
Code: Select all
netstat -rn | findstr "\<0.0.0.0\>"
Code: Select all
route print 0.0.0.0 | findstr "\<0.0.0.0\>"
If there are multiple default gateways (0.0.0.0) , which is a bad configuration anyway, the metric value can be used to determine which interface or gateway is likely to be used for accessing the outside network. Lower metric values have higher priorities. But it is not a very reliable measure as windows may consider other factors besides the metric value for multiple gateways. And if two default gateways are configured with same metric value the situation is worse.siberia-man wrote: ↑26 Jan 2022 09:28... I hope only it will be the only interface or the only gateway.
Code: Select all
tracert -d -w 100 -h 1 8.8.8.8
Slow computer then. On my 8th Gen I7 I blinked and this was done.siberia-man wrote: ↑17 Jan 2022 12:04I use the following command to detect internal IP address (the 4th field):
In my opinion it's too slow. I think it's because of netstat as the slowest command itself.Code: Select all
netstat -rn | findstr "\<0.0.0.0\>"
Code: Select all
TimeThis : Command Line : netstat -rn
TimeThis : Start Time : Wed Jan 26 16:40:37 2022
TimeThis : End Time : Wed Jan 26 16:40:37 2022
TimeThis : Elapsed Time : 00:00:00.214
Code: Select all
@echo off
Title Get LocalIP and WANIP
Call :GET_LocalIP
echo( & echo LocalIP : %LocalIP%
pause
Call :GET_WANIP
echo( & echo LocalIP : %WANIP%
Pause & exit
::-----------------------------------------------------------------------------------
:GET_LocalIP <LocalIP>
Set "LocalIP="
@for /f "delims=: tokens=2" %%a in ('ipconfig ^| findstr IPv4') do set "IPaddr=%%a"
Call :Trim "%IPaddr%" LocalIP
Exit /B
::-----------------------------------------------------------------------------------
:Get_WANIP
Set "MyCommand=nslookup myip.opendns.com resolver1.opendns.com 2^>nul"
@for /f "skip=4 delims=: tokens=2" %%a in ('%MyCommand%') do (
Set "WANIP=%%a"
)
Call :Trim "%WANIP%" WANIP
Exit /B
::-----------------------------------------------------------------------------------
:Trim <Var> <NewVar>
>"%tmp%\%~n0.vbs" (echo Wscript.echo Trim("%~1"^))
@for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do set "%2=%%a"
Del "%tmp%\%~n0.vbs"
exit /b
::-----------------------------------------------------------------------------------
I don't recommend to rely on the outcomes of ipconfig and other CLI tools (for example, netsh interface) used in Windows. I found that most of them are localized so the first command fails (tested in Russian).Hackoo wrote: ↑27 Jan 2022 00:07Code: Select all
... ipconfig ^| findstr IPv4 ... nslookup myip.opendns.com resolver1.opendns.com 2^>nul ...