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
Explain batch script - need some help
Moderator: DosItHelp
Re: Explain batch script - need some help
If i remember right, then ...
You list your pc's routing table using:
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.
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
You list your pc's routing table using:
Code: Select all
NETSH INTERFACE IPv4 SHOW ROUTE
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 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
-
- Posts: 208
- Joined: 26 Dec 2013 09:28
- Contact:
Re: Explain batch script - need some help
I slightly reformated the code from the OP. Just to make it more readable.
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)
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.
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%
-- 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