Detecting loopback device via batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Detecting loopback device via batch

#1 Post by noprogrammer » 05 Mar 2016 15:26

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Detecting loopback device via batch

#2 Post by aGerman » 05 Mar 2016 17:02

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Detecting loopback device via batch

#3 Post by penpen » 05 Mar 2016 17:22

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

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Re: Detecting loopback device via batch

#4 Post by noprogrammer » 06 Mar 2016 17:20

@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.

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Re: Detecting loopback device via batch

#5 Post by noprogrammer » 09 Mar 2016 04:48

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

b0gus
Posts: 7
Joined: 02 Mar 2016 12:58

Re: Detecting loopback device via batch

#6 Post by b0gus » 09 Mar 2016 05:38

noprogrammer, 7-th line write as:

Code: Select all

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Detecting loopback device via batch

#7 Post by aGerman » 09 Mar 2016 17:05

%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

Post Reply