Explain batch script - need some help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
demour
Posts: 1
Joined: 18 Jan 2020 22:10

Explain batch script - need some help

#1 Post by demour » 18 Jan 2020 22:20

Can someone help me and explain the following batch script as precisely as possible?
Please, thank you.

FOR /f "TOKEN=5" %%i IN ('NETSH INTERFACE IPv4 SHOW ROUTE ^|
FIND "0.0.0.0/0"') DO @FOR /f "TOKEN=2" %%i IN (' NETSH INTERFACE IPv4 SHOW ADDRESSES %%i ^|
FIND "IP-Adresse"') DO @ECHO IPv4 : %%i >> %Ausgabe%

Best regards, Demour

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

Re: Explain batch script - need some help

#2 Post by penpen » 21 Jan 2020 14:42

If i remember right, then ...

You list your pc's routing table using:

Code: Select all

NETSH INTERFACE IPv4 SHOW ROUTE
The pipe "^|" redirects the output to a find command to filter the (unique) entry of the default IPv4-Prefix "0.0.0.0/0" (== the range of IP addresses from 0.0.0.0 to 255.255.255.255 == all possible IP addresses).
The outer for/f-loop assigns the value of the fifth token (== Interface-Index of the default routing device) to the inner-for-variable %%i.
So the following command lists the configuration of the Interface used for default IPv4 routing.

Code: Select all

NETSH INTERFACE IPv4 SHOW ADDRESSES %%i
The pipe after that redirects the output to a find command that filters lines containing the String "IP-Adresse" (typically there's only one, although there might be more).
The inner for/f-loop assigns the value of the IPv4-Address of the default routing device to the inner-for-variable %%i.
The echo command writes the found address(es) to the redirected output which is a file with its name contained in the environment variable "Ausgabe".


penpen

siberia-man
Posts: 133
Joined: 26 Dec 2013 09:28
Contact:

Re: Explain batch script - need some help

#3 Post by siberia-man » 22 Jan 2020 00:10

I slightly reformated the code from the OP. Just to make it more readable.

Code: Select all

FOR /f "TOKEN=5" %%i IN ( '
	NETSH INTERFACE IPv4 SHOW ROUTE ^|
	FIND "0.0.0.0/0"
' ) DO 
@FOR /f "TOKEN=2" %%i IN ( '
	NETSH INTERFACE IPv4 SHOW ADDRESSES %%i ^|
	FIND "IP-Adresse"
') DO 
@ECHO IPv4 : %%i >> %Ausgabe%
Finally this set of commands looks for the IP address, which can be assumed as default or internal IP address that is used for accessing the internet. The set of commands can be simplified
-- at least one loop (instead of twos in the example above)
-- locale settings independent ("IP-Adresse" AFAIK in German, "IP Address" in English, something else in others)

Code: Select all

for /f "tokens=4" %%a in ( '
    netstat -rn ^| find " 0.0.0.0 "
' ) do @echo:%%a
This code reads a routing table, looks for the default route and outputs the internal IP address (4th field). I use something similar to setup a proxy's address in Cygwin depending on if my current internal address matches some subnet.

Post Reply