我有一个我写的脚本,用来帮助配置部署后的计算机。该脚本可以设置计算机名称、enable的BitLocker等。我正在处理的一件事就是IP地址的设置。我使用的计算机(具体地说是服务器)有4个NIC端口,它们被命名为Local area connection和Local area connection 2-4。问题是,部署这些服务器的技术人员并不总是插入相同的端口,此外,部署映像并不总是将以太网局域网连接分配给NIC端口1。
这是我拥有的脚本的一个副本,如果只启用了一个NIC端口,它就可以完美地工作。我需要做的是将具有IP地址的Local Area Connection name的输出(因为DHCP已经存在)传输到一个变量,我可以将该变量放入我的netsh
命令中。
当前代码就位
:IPADDRESS
@echo Would you like static or DHCP?
@echo press 1 for static
@echo press 2 for dhcp
Choice /C:12 /N /M "?:"
IF ERRORLEVEL 2 GOTO IPDHCP
IF ERRORLEVEL 1 GOTO IPSTATIC
:IPSTATIC
set /P _IPADDR=Please enter IP address:
set /p _Subnet=Please enter Subnet:
set /p _DefaultGateway=Please Enter Default Gateway:
netsh interface ip set address name="Local Area Connection 2" static %_IPADDR% %_Subnet% %_DefaultGateway%
goto START
:IPDHCP
netsh interface ip set address "Local Area Connection 2" dhcp
goto START
:disipaddr
netsh interface ip show config name="Local Area Connection 2"
名称"Local Area Connection 2“是随构建而变化的部分。这就是我需要隔离的部分。我相当确定,对于/p
并针对ipconfig /all
或netsh interface ip show config使用它将是正确的方法。
首先要感谢你的帮助。
发布于 2015-02-05 00:07:53
这将在Windows7上运行和测试。
一个batch.bat文件:
@echo off
for /f "tokens=2 delims==" %%F in ('wmic nic where "NetConnectionStatus=2 and AdapterTypeId=0" get NetConnectionID /format:list') do set interfaceName=%%F
echo Your Interface is %interfaceName%
pause
发布于 2012-11-29 00:37:15
也许你可以使用wmic
来做这件事?大致是这样的:
for /f "tokens=2 delims==" %F in ('wmic nic where "NetConnectionStatus=2 and AdapterTypeId=0" get NetConnectionID /format:list') do set %activeNet%=%F
它应该返回所有已连接(NetConnectionStatus=2
)的Eth/802.3 (AdapterTypeID=0
)接口-即实例中已连接的接口。(出于某种原因,它还报告我的XP笔记本电脑上的WiFi为802.3,但这对于服务器来说应该不是问题)
您可以直接从命令行尝试它,看看它是否返回了它应该返回的内容。如果批量使用,请将%F
替换为%%F
。
请注意,可能有必要检查已连接接口的返回值,因为不同OSes的返回值不同。Full description of the Win32_NetworkAdapter class ( nic
是其别名)
如果你100%确定你只想检查那些以“本地连接”开头的连接,你可以使用where "netconnectionID like 'Local Area Connection%'"
(或者结合其他条件)。
发布于 2019-07-12 02:08:35
你的意思是连接,哪些正在使用?
:_InterfaceConnected_
FOR /F "tokens=3,*" %%A IN ('netsh interface show interface^|find "Connected"') DO (
echo %%B
echo connected : %%B
)
@echo:
:_InterfaceDisconnected_
FOR /F "tokens=3,*" %%A IN ('netsh interface show interface^|find "Disconnected"') DO (
echo %%B
echo disconnected : %%B
)
以输出形式给出:
Ethernet
connected : Ethernet
Ethernet 3
disconnected : Ethernet 3
完整的概述,通过命令
> netsh interface show interface
输出:
Admin State State Type Interface Name
-------------------------------------------------------------------------
Enabled Disconnected Dedicated Ethernet 3
Enabled Connected Dedicated Ethernet
https://stackoverflow.com/questions/13595499
复制相似问题