As the title said, I want to match ip address with batch in windows, please tell me how I can do it?
I see that \"finds
This would fall under the "any other method" part of the title but I used a for loop in a batch command together with findstr to get my desired IP. In my case I set the IP to an environment variable called IP.
Here is the command:
for /f "tokens=3 delims=: " %i in ('netsh interface ip show config name^="Ethernet" ^| findstr "IP Address"') do set IP=%i
Most commands using ipconfig
for example just print out all your IP addresses and I needed a specific one which in my case was for my Ethernet network adapter.
You can see your list of network adapters by using the netsh interface ipv4 show interfaces
command. Most people need Wi-Fi or Ethernet.
You'll see a table like so in the output to the command prompt:
Idx Met MTU State Name
--- ---------- ---------- ------------ ---------------------------
1 75 4294967295 connected Loopback Pseudo-Interface 1
15 25 1500 connected Ethernet
17 5000 1500 connected vEthernet (Default Switch)
32 15 1500 connected vEthernet (DockerNAT)
In the name column you should find the network adapter you want (i.e. Ethernet, Wi-Fi etc.).
As mentioned, I was interested in Ethernet
in my case.
To get the IP for that adapter we can use the netsh command:
netsh interface ip show config name="Ethernet"
This gives us this output:
Configuration for interface "Ethernet"
DHCP enabled: Yes
IP Address: 169.252.27.59
Subnet Prefix: 169.252.0.0/16 (mask 255.255.0.0)
InterfaceMetric: 25
DNS servers configured through DHCP: None
Register with which suffix: Primary only
WINS servers configured through DHCP: None
(I faked the actual IP number above for security reasons
A simplistic regex string would be
(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.(1?[0-9]?[0-9]|2[0-4][0-9])|25[0-5]
to match exactly the range of allowable IP addresses.
EDIT: Based on comments below and Regular expressions in findstr information, modify the above regex to [0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]
to match IP addresses. Apparently FINDSTR
really is that limited in regular expression interpretation.
In case the input of your findstr command should not be the result of ipconfig, you could use the following line of code to look for an ip-address in a text or csv file.
findstr /r "\<[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\>" filename.txt
It adds to metacharacters to the answer of user Joey. "\<" at the beginning and ">" at the end. Even though such a string might not be very common (or very unlikely to occur) this makes sure that no string in an ip-address-like format but with an alphabetic character at the end and/or the beginning (e.g. A198.252.206.16 or 198.252.206.16A) is returned.
Since findstr
's regex support is a bit ... dated, you usually can't use most regexes you find on the web. The following matches four runs of digits, separated by dots:
ipconfig | findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
However, if you're only interested in addresses and not the subnet masks, you might want to use
ipconfig | findstr /r "Address.*[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
And yes, if would also match things like Address: 232345.534.78678.345
which is obviously not an IP address. But usually ipconfig
doesn't spit out such strings.
FINDSTR is the only native batch utility that has any support for regular expressions. But the support is very limited and non-standard. The only repeat expression supported is *
. In addition, it is limited to a maximum of 15 character class terms (see What are the undocumented features and limitations of the Windows FINDSTR command?). So I don't think it is possible to develop a native batch regex that will precisely match an IP address.
You could stay within native windows utilities and use Power Shell, or you could use JScript or VBScript via the CSCRIPT command. All three have much better regex support.
Alternatively you could download any of a number of Windows ports of Unix utilities, many of them free. GnuWin32 is a good resource (includes grep): http://gnuwin32.sourceforge.net/