假设这是Windows ipconfig命令的输出。
c:\>ipconfig
Windows IP Configuration
Wireless LAN adapter Wireless Network Connection:
Connection-specific DNS Suffix . :
IPv4 Address. . . . . . . . . . . : 192.168.1.10
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1
c:\>在Linux中,我可以使用grep和cut命令轻松地获得一个IP地址。
user@linux:~$ cat ip
c:\>ipconfig
Windows IP Configuration
Wireless LAN adapter Wireless Network Connection:
Connection-specific DNS Suffix . :
IPv4 Address. . . . . . . . . . . : 192.168.1.10
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1
c:\>
user@linux:~$
user@linux:~$ cat ip | grep IPv
IPv4 Address. . . . . . . . . . . : 192.168.1.10
user@linux:~$
user@linux:~$ cat ip | grep IPv | cut -d ':' -f 2
192.168.1.10
user@linux:~$ 然而,在Windows中,这是我使用findstr命令所能得到的最好结果。有没有一种方法可以让我们只从这个输出中cut IP部分?
c:\>ipconfig | findstr IPv4
IPv4 Address. . . . . . . . . . . : 192.168.1.10
c:\>我期待的是这样的东西,只使用本机windows命令
c:\>ipconfig | <some command here just to get an IP Address only>
192.168.1.10
c:\>发布于 2018-03-09 08:46:00
可以将ipconfig | cscript /Nologo script.js与包含以下内容的文件script.js一起使用:
var lines = WScript.Stdin.ReadAll().split('\n');
for(var i = 0; i < lines.length; ++i) {
var line = lines[i];
if(line.match(/IPv4 Address/)) {
WScript.echo(line.replace(/ *IPv4 Address[ .]*: /, ''));
}
}请注意,可能有多个网络适配器,导致打印多个IP。
发布于 2019-11-23 05:50:38
我无法让ipconfig命令按照我想要的方式工作,这就是隔离我所需要的特定网络适配器的IP。
我需要“以太网”,但你可以选择任何时候你想要的,例如“Wi”。
为了达到这个目的,我使用了netsh (注意: OP专门询问了ipconfig,但是这取得了相同的结果,所以我想我会分享):
for /f "tokens=3 delims=: " %i in ('netsh interface ip show config name^="Ethernet" ^| findstr "IP Address"') do echo Your IP Address is: %i有关如何工作和如何自定义它的说明,请参阅我在这里的相关文章:https://stackoverflow.com/a/59004409/2684661
发布于 2018-03-09 10:03:29
您可以使用以下指示来实现您的目标:
for /f "tokens=2 delims=:" %i in ('ipconfig ^| findstr "IPv4" ^| findstr [0-9]') do echo %ihttps://stackoverflow.com/questions/49189466
复制相似问题