问题描述
我正在寻找从ipconfig中拉出以太网适配器名称以在批处理脚本中使用的功能,该脚本将使用netsh为该适配器名称创建一个静态ip.
I'm looking to pull the Ethernet adapter name out of ipconfig to use in a batch script which will create a static ip to that adapter name using netsh.
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由用户输入).
What I am trying to do is pull out Ethernet0 and use that in the following netsh command (net_city and net_lab are inputted by the user).
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
检索名称的最佳方法是什么?我已经开始研究正则表达式,以尝试过滤出该名称.
What would be the best way to retrieve the name? I have begun looking into regex to try and filter out the name.
谢谢!
推荐答案
正如已经建议的那样,您可以使用netsh
来收集接口列表,然后让用户使用choice
选择一个接口.这是我的实现:
As already suggested, you can use netsh
to gather a list of interfaces, and then get the user to select one using choice
. Here is my implementation of that:
@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
等的每个变量的名称成为现实.您可以在choice命令之后简单地用!int%errorlevel%!
进行调用.
The choice command changes the value of errorlevel
, and by making the name of each variable comprising your list of interfaces int1
, int2
, etc. you can simply call them with !int%errorlevel%!
after the choice command.
如果您可以假设只有一个接口,那么您只需执行以下操作即可.
If you can assume there will only ever be one interface, then you can simply do the following.
for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do set interface=%%B
echo %interface%
这篇关于如何在Windows 8.1批处理脚本中检索以太网适配器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!