How can I extract only a list of IP4 addresses from the output of ipconfig?
Use the following batch file (test.cmd):
@echo off
setlocal
setlocal enabledelayedexpansion
for /f "usebackq tokens=2 delims=:" %%a in (`ipconfig ^| findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"`) do (
set _temp=%%a
rem remove leading space
set _ipaddress=!_temp:~1!
echo !_ipaddress!
)
endlocal
Example usage and output:
> ipconfig | findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
IPv4 Address. . . . . . . . . . . : 192.168.42.78
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.42.129
> test
192.168.42.78
255.255.255.0
192.168.42.129
Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
- for /f - Loop command against the results of another command.
- ipconfig - Configure IP (Internet Protocol configuration)
- set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
- setlocal - Set options to control the visibility of environment variables in a batch file.
- variables - Extract part of a variable (substring).