Page 1 of 1

Detecting loopback device via batch

Posted: 05 Mar 2016 15:26
by noprogrammer
Hi,

I need a routine to detect which loopback device has been chosen within the hosts file, i.e.
127.0.0.1 or 0.0.0.0: I need to return the appropriate value as variable (for another batch file).

What do you think?

Re: Detecting loopback device via batch

Posted: 05 Mar 2016 17:02
by aGerman
There is neither a setting for a loopback device in my hosts file nor any uncommented line either.
Although according to Wikipedia this should work:

Code: Select all

@echo off &setlocal
set "loopback="
for /f "tokens=2*" %%i in (
  'reg query "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "DataBasePath"'
) do for /f "tokens=*" %%k in ('call type "%%~j\hosts"') do (
  for /f "tokens=1,3 eol=#" %%l in ("%%k") do if /i "%%m"=="loopback" set "loopback=%%l"
)

if defined loopback echo %loopback%
pause

Regards
aGerman

Re: Detecting loopback device via batch

Posted: 05 Mar 2016 17:22
by penpen
If i remember right, then:
The host file is a mapping of host names to ip adresses, and has nothing to do with network adapters (like the loopback device).
The ip adress 0/x (0.0.0.0:x) is reserved and is not allowed except in initialization process when a host learns its own (full) ip adress:
http://tools.ietf.org/html/rfc1122#page-29.

Because of that it may be used in the hosts file to direct to a non existing host (target adress 0 is an invalid target ip adress):

Code: Select all

0.0.0.0   unsafe-host-name.domain-name   unsafe-host-name 

The ip adress 2130706433 (127.0.0.1) should always be valid for loopback devices (if installed):

Code: Select all

ping 2130706433

Sidenote:
I assume you could check if a loopback adapter is installed with:

Code: Select all

ipconfig /all


penpen

Re: Detecting loopback device via batch

Posted: 06 Mar 2016 17:20
by noprogrammer
@penpen:
Maybe I mixed something up - I meant the localhost "mapping":
the Windows default is 127.0.0.1, some people change it to 0.0.0.0 later.
Before appending new content the correct mapping must be detected.

@aGerman:
Thanks for the script, very cool concept! I changed it slightly and will test it for the intended purpose next.

Re: Detecting loopback device via batch

Posted: 09 Mar 2016 04:48
by noprogrammer
aGerman, I now use the following script

Code: Select all

@echo off &SetLocal
Set "rkey=HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"
For /f "tokens=3*" %%? In ('reg query %rkey% /v "DataBasePath"') Do (
  For /f "tokens=*" %%i In ('call type "%%~?\hosts"') Do (
     For /f "tokens=1-3 eol=#" %%j In ("%%i") Do If /i "%%k"=="localhost" Set "l=%%j"
  )
  Set "h=%%~?\hosts"
)
Echo hosts file: %h%
Echo localhost: %l%
If Not Exist %h% Echo File not found!
The output will be something like

Code: Select all

hosts file: %SystemRoot%\System32\drivers\etc\hosts
localhost: 127.0.0.1
File not found!

I also want %h% to hold the hosts path and exactly this won't work as expected.
Just guessing, would something like !SystemRoot!\System32\drivers\etc\hosts help?
Or is there another trick? It works if I put it without variable:

Code: Select all

If Exist %SystemRoot%\System32\drivers\etc\hosts Echo File found
:?

Re: Detecting loopback device via batch

Posted: 09 Mar 2016 05:38
by b0gus
noprogrammer, 7-th line write as:

Code: Select all

call Set "h=%%~?\hosts"

Re: Detecting loopback device via batch

Posted: 09 Mar 2016 17:05
by aGerman
%SystemRoot% was read as literal expression in the registry. Once the percent signs are part of a string literal which is saved in a variable you would have to expand the variable that contains %SystemRoot% and after that the literal %SystemRoot% itself. That kind of double expansion will usually be done using CALL as b0gus suggested.
For the same reason CALL is already present in the 2nd FOR loop. A simple TYPE without CALL wouldn't work there.

Example to emulate this behavior:

Code: Select all

@echo off &setlocal
>"test.txt" echo %%SystemRoot%%

echo ~~~~~~~~~~
echo content of test.txt:
type "test.txt"

echo ~~~~~~~~~~
<"test.txt" set /p var=
echo content of var:
echo %var%

echo ~~~~~~~~~~
echo double expansion via call:
call echo %var%

pause


Regards
aGerman