Batch file 如何在Windows 8.1批处理脚本中检索以太网适配器名称
我希望从ipconfig中提取以太网适配器名称,以便在批处理脚本中使用,该脚本将使用netsh为该适配器名称创建静态ipBatch file 如何在Windows 8.1批处理脚本中检索以太网适配器名称,batch-file,networking,windows-8.1,static-ip-address,Batch File,Networking,Windows 8.1,Static Ip Address,我希望从ipconfig中提取以太网适配器名称,以便在批处理脚本中使用,该脚本将使用netsh为该适配器名称创建静态ip Ethernet adapter Ethernet0: Connection-specific DNS Suffix . : foo.bar.com IPv4 Address. . . . . . . . . . . : 10.0.0.123 Subnet Mask . . . . . . . . . . . : 255.255.255.0
Ethernet adapter Ethernet0:
Connection-specific DNS Suffix . : foo.bar.com
IPv4 Address. . . . . . . . . . . : 10.0.0.123
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.0.0.456
我试图做的是拉出Ethernet0并在下面的netsh命令中使用它(net_city和net_lab由用户输入)
netsh接口ip设置地址“”静态10.%net_city%.15%net_lab%.235 255.255.255.0 10.%net_city%.15%net_lab%.1
检索名称的最佳方法是什么?我已经开始研究regex,试图过滤掉这个名字
谢谢大家! 为什么不使用以下选项,然后使用来选择正确的界面
C:\Scripts>netsh interface show interface
Admin State State Type Interface Name
-------------------------------------------------------------------------
Enabled Connected Dedicated Local Area Connection
如前所述,您可以使用
netsh
收集接口列表,然后让用户使用choice
选择一个接口。以下是我对此的实施:
@echo off
setLocal enableDelayedExpansion
set c=0
set "choices="
echo Interfaces -
for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do (
set /a c+=1
set int!c!=%%B
set choices=!choices!!c!
echo [!c!] %%B
)
choice /c !choices! /m "Select Interface: " /n
set interface=!int%errorlevel%!
echo %interface%
choice命令更改errorlevel
的值,并通过设置包含接口列表int1
、int2
等的每个变量的名称。您只需使用调用它们即可!int%errorlevel%代码>在选择命令之后
如果您可以假设只有一个接口,那么您可以简单地执行以下操作
for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do set interface=%%B
echo %interface%
您是否假设只有一个接口?
for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do set interface=%%B
echo %interface%
for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do set interface=%%B
netsh interface ip set address name=%interface% static 192.168.1.10 255.255.255.0 192.168.1.1