Page 1 of 1

The fastest way to detect internal IP address

Posted: 17 Jan 2022 12:04
by siberia-man
I use the following command to detect internal IP address (the 4th field):

Code: Select all

netstat -rn | findstr "\<0.0.0.0\>"
In my opinion it's too slow. I think it's because of netstat as the slowest command itself. I'd like use faster command so route print, powershell or querying wmic don't cover my requirements for speed. During my tests I found yet another approach that print the same as netstat -rn and seems extremely fast:

Code: Select all

route print 0.0.0.0 | findstr "\<0.0.0.0\>"
My question

Is it 100% reliable way for detecting internal IP address?

Re: The fastest way to detect internal IP address

Posted: 18 Jan 2022 08:13
by Squashman
siberia-man wrote:
17 Jan 2022 12:04
My question

Is it 100% reliable way for detecting internal IP address?
If your end goal is to find all devices on your network the answer is no.

Re: The fastest way to detect internal IP address

Posted: 19 Jan 2022 05:17
by siberia-man
Squashman wrote:
18 Jan 2022 08:13
... your end goal is ...
just to detect the currently active IP address which is assumed assigned for routing default traffic towards outside.

Re: The fastest way to detect internal IP address

Posted: 24 Jan 2022 03:48
by jfl
> Is it 100% reliable way for detecting internal IP address?

Filtering on 0.0.0.0 only gives you the interfaces that are connected to the Internet, not those on private LANs isolated from the outside world. (A common case for servers at work!)

Re: The fastest way to detect internal IP address

Posted: 26 Jan 2022 09:28
by siberia-man
jfl wrote:
24 Jan 2022 03:48
Filtering on 0.0.0.0 only gives you the interfaces that are connected to the Internet...
Yes. It's exactly what I want. But...
jfl wrote:
24 Jan 2022 03:48
... not those on private LANs isolated from the outside world. (A common case for servers at work!)
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.

My 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\>"

Re: The fastest way to detect internal IP address

Posted: 26 Jan 2022 16:29
by sst
siberia-man wrote:
26 Jan 2022 09:28
My 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\>"
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:28
... I hope only it will be the only interface or the only gateway.
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.
In this case tracert can be used as a backup measure to determine the currently active default gateway.
For example

Code: Select all

tracert -d -w 100 -h 1 8.8.8.8
But as you know this can't be a reliable method either.

It all depends on what you are trying to accomplish. If you are doing this out curiosity that's fine, But if you have a specific goal there maybe better or more reliable ways for doing that. I hope you were not trapped in a XY situatiom.

Re: The fastest way to detect internal IP address

Posted: 26 Jan 2022 16:34
by Squashman
siberia-man wrote:
17 Jan 2022 12:04
I use the following command to detect internal IP address (the 4th field):

Code: Select all

netstat -rn | findstr "\<0.0.0.0\>"
In my opinion it's too slow. I think it's because of netstat as the slowest command itself.
Slow computer then. On my 8th Gen I7 I blinked and this was done.

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
If this speed is a problem for such a trivial task then you might as well write your own Assembler program or C program and start counting clock cycles.

Re: The fastest way to detect internal IP address

Posted: 27 Jan 2022 00:07
by Hackoo
Hi :wink:
You can try this code :

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
::-----------------------------------------------------------------------------------

Re: The fastest way to detect internal IP address

Posted: 27 Jan 2022 05:37
by siberia-man
Hackoo wrote:
27 Jan 2022 00:07

Code: Select all

...
ipconfig ^| findstr IPv4
...
nslookup myip.opendns.com resolver1.opendns.com 2^>nul
...
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).

Testing external IP address is a bit simpler and looks more straightforward. But it has some shortcomes as well: some resources detect an IP address given by your ISP, others can recognized your external proxy only.

In my practice I use my own posix shell script working well under linux, cygwin and busybox.