如何在Windows 8.1批处理脚本中检索以太网适配器名称

时间:2015-04-08 19:23:54

标签: 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
    Default Gateway . . . . . . . . . : 10.0.0.456

我要做的是拔出Ethernet0并在以下netsh命令中使用它(net_city和net_lab由用户输入)。

netsh interface ip set address "<adapter name>" static 10.%net_city%.15%net_lab%.235 255.255.255.0 10.%net_city%.15%net_lab%.1 1

检索名称的最佳方法是什么?我已经开始研究正则表达式,试图过滤出名称。

谢谢!

3 个答案:

答案 0 :(得分:1)

如前所述,您可以使用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%

选择命令会更改errorlevel的值,并通过使每个变量的名称包含您的接口列表int1int2等,您可以使用{ {1}}在选择命令之后。

如果您可以假设只有一个界面,那么您只需执行以下操作即可。

!int%errorlevel%!

答案 1 :(得分:1)

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

答案 2 :(得分:0)

为什么不使用以下内容,然后使用this post选择正确的界面。

C:\Scripts>netsh interface show interface
Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Connected      Dedicated        Local Area Connection