简体   繁体   English

如何使用'findstr'匹配IP地址? 或任何其他 Windows 批处理方法

[英]How to match IP address by using 'findstr'? Or any other method of batch in windows

As the title said, I want to match ip address with batch in windows, please tell me how I can do it?正如标题所说,我想在windows中批量匹配ip地址,请告诉我该怎么做?
I see that " findstr " can match with regex like " [0-9] ", but how can "findstr" matches it appears one to three times ?我看到“ findstr ”可以与“ [0-9] ”这样的正则表达式匹配,但是“findstr”如何匹配它出现一到三次

Since findstr 's regex support is a bit ... dated, you usually can't use most regexes you find on the web.由于findstr的正则表达式支持有点......过时,您通常无法使用在网络上找到的大多数正则表达式。 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.是的,如果也匹配Address: 232345.534.78678.345 ,这显然不是IP地址。 But usually ipconfig doesn't spit out such strings.但通常ipconfig不会吐出这样的字符串。

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 命令的输入不应该是 ipconfig 的结果,您可以使用以下代码行在文本或 csv 文件中查找 ip 地址。

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.它将元字符添加到用户 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 (eg A198.252.206.16 or 198.252.206.16A) is returned.即使这样的字符串可能不是很常见(或不太可能发生),这也可以确保没有类似 ip 地址格式的字符串,但在结尾和/或开头带有字母字符(例如 A198.252.206. 16 或 198.252.206.16A) 被返回。

FINDSTR is the only native batch utility that has any support for regular expressions. FINDSTR 是唯一支持正则表达式的本地批处理实用程序。 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? ).此外,它被限制为最多 15 个字符的类术语(请参阅Windows FINDSTR 命令的未记录功能和限制是什么? )。 So I don't think it is possible to develop a native batch regex that will precisely match an IP address.因此,我认为不可能开发出与 IP 地址精确匹配的本机批处理正则表达式。

You could stay within native windows utilities and use Power Shell, or you could use JScript or VBScript via the CSCRIPT command.您可以留在本机 Windows 实用程序中并使用 Power Shell,或者您可以通过 CSCRIPT 命令使用 JScript 或 VBScript。 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.或者,您可以下载 Unix 实用程序的许多 Windows 端口中的任何一个,其中许多是免费的。 GnuWin32 is a good resource (includes grep): http://gnuwin32.sourceforge.net/ GnuWin32 是一个很好的资源(包括 grep): http ://gnuwin32.sourceforge.net/

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.以与允许的 IP 地址范围完全匹配。

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.编辑:根据下面的注释和findstr信息中的正则表达式,将上面的正则表达式修改为[0-2][0-9][0-9]\\.[0-2][0-9][0-9]\\.[0-2][0-9][0-9]\\.[0-2][0-9][0-9]匹配 IP 地址。 Apparently FINDSTR really is that limited in regular expression interpretation.显然FINDSTR确实在正则表达式解释方面受到限制。

tldr; tldr;

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.这属于标题的“任何其他方法”部分,但我在批处理命令中使用了 for 循环和 findstr 来获得我想要的 IP。 In my case I set the IP to an environment variable called IP.就我而言,我将 IP 设置为名为 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

For those who are curious to know what all that means, read on对于那些想知道这一切意味着什么的人,请继续阅读

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.例如,大多数使用ipconfig命令只是打印出您所有的 IP 地址,而我需要一个特定的地址,在我的情况下是用于我的以太网网络适配器。

You can see your list of network adapters by using the netsh interface ipv4 show interfaces command.您可以使用netsh interface ipv4 show interfaces命令查看网络适配器列表。 Most people need Wi-Fi or Ethernet.大多数人需要 Wi-Fi 或以太网。

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 (ie Ethernet, Wi-Fi etc.).在名称列中,您应该找到所需的网络适配器(即以太网、Wi-Fi 等)。

As mentioned, I was interested in Ethernet in my case.如前所述,就我而言,我对Ethernet感兴趣。

To get the IP for that adapter we can use the netsh command:要获取该适配器的 IP,我们可以使用 netsh 命令:

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 😉) (出于安全原因,我伪造了上面的实际 IP 号码😉)

I can further specify which line I want using the findstr command in the ms-dos command prompt.我可以在 ms-dos 命令提示符中使用findstr命令进一步指定我想要的行。 Here I want the line containing the string IP Address .这里我想要包含字符串IP Address

netsh interface ip show config name="Ethernet" | findstr "IP Address"

This gives the following output:这给出了以下输出:

 IP Address:                           169.252.27.59

I can then use the for command that allows me to parse files (or multiline strings in this case) and split out the strings' contents based on a delimiter and the item number that I'm interested in.然后我可以使用for命令来解析文件(或在这种情况下的多行字符串)并根据分隔符和我感兴趣的项目编号拆分字符串的内容。

Note that I am looking for the third item (tokens=3) and that I am using the space character and : as my delimiters ( delims=: ).请注意,我正在寻找第三项(tokens=3)并且我使用空格字符和:作为我的分隔符( delims=: )。

for /f "tokens=3 delims=: " %i  in ('netsh interface ip show config name^="Ethernet" ^| findstr "IP Address"') do set IP=%i

Each value or token in the loop is printed off as the variable %i but I'm only interested in the third "token" or item (hence tokens=3 ).循环中的每个值或标记都作为变量 %i 打印出来,但我只对第三个“标记”或项目感兴趣(因此tokens=3 )。 Note that I had to escape the |请注意,我必须逃避| and = using a ^=使用^

At the end of the for command you can specify a command to run with the content that is returned.for命令的末尾,您可以指定要使用返回的内容运行的命令。 In this case I am using set to assign the value to an environment variable called IP .在这种情况下,我使用set将值分配给名为IP的环境变量。 If you want you could also just echo the value or what ever you like.如果您愿意,您也可以只回显该值或您喜欢的任何内容。

With that you get an environment variable with the IP Address of your preferred network adapter assigned to an environment variable.有了它,您将获得一个环境变量,其中您的首选网络适配器的 IP 地址分配给环境变量。 Pretty neat, huh?很整洁吧?

If you have any ideas for improving please leave a comment.如果您有任何改进的想法,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM