0

我希望从 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

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

谢谢!

4

3 回答 3

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,并且通过使每个变量的名称组成您的接口列表int1等,您可以在选择命令之后int2简单地调用它们。!int%errorlevel%!

如果您可以假设永远只有一个界面,那么您可以简单地执行以下操作。

for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do set interface=%%B

echo %interface%
于 2015-04-09T04:02:19.257 回答
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
于 2016-11-05T03:24:13.923 回答
0

为什么不使用以下,然后使用此帖子选择正确的界面。

C:\Scripts>netsh interface show interface
Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Connected      Dedicated        Local Area Connection
于 2015-04-08T21:56:19.013 回答