How do I extract the IPv4 IP Addresses from the output of ipconfig, and then filter it so that my output only includes a list of the IP addresses?
8,961
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).
Related videos on Youtube
Author by
Admin
Updated on September 18, 2022Comments
-
Admin over 1 year
How do I extract the IPv4 IP Address from the output of ipconfig
I read through this post and it was very helpful. I was just wondering if the is a way I can extract only the IP Addresses (xxx.xxx.xxx.xxx). Best way I could think of using notepad to find all / replace all.
Is there a method that I can use via command line?
-
G-Man Says 'Reinstate Monica' over 6 yearsIn what way does the question you linked to not provide you with all the information you need? Please do not respond in comments; edit your question to be clearer and more complete.
-
DavidPostill over 6 years@G-Man Clearly my answer to the other question gives some clues as to how it might be solved, but needs tweaking to provide the output that the OP wants. Most users are not sufficiently knowledge about batch programming to make those changes. I have provide an answer for the OP which I believe answers his question.
-
DavidPostill over 6 years@G-Man I'm reasonably proficient with batch files, but I still had to think about how to add a regular expression to findstr to match dotted quad ip addresses.
-
G-Man Says 'Reinstate Monica' over 6 years@DavidPostill: But my point is, you didn’t need to. The “search for
IPv4
/ split on:
/ strip leading space” approach works. And aren’t moderators always berating users that “if you find yourself posting substantially the same answer to two separate questions (in the same community), you should stop, and flag one of them as a duplicate instead”? -
DavidPostill over 6 years@G-Man No it doesn't. That approach only find the first dotted quad address (which happens to be a line containing the string IP4). If you actually bothered to read my answer you will see it extracts a list of 3 IP addresses. As for your second point my answer here is substantially different from my other answer.
-
DavidPostill over 6 years@G-Man Note the question title asks for a list of IP addresses not just the single address labelled IP4.
-
Mathew Lionnet over 6 yearsAnd you really should look into powershell.
-