使用 NetSh 查找接口名称并将其定义为批处理文件中的变量

Using NetSh to find an interface name and define it as a variable in a batch file

我正在做一个批量设置静态IP的程序。我使用的是 local area network 的硬编码名称,但我希望能够使用 netsh interface show interface 找到 LAN 的名称,然后将变量设置为接口名称。

这是我使用 netsh interface show interface 时的样子。

下面的脚本是我正在使用的。我真的很想从 netsh 中提取的 netsh interface ipv4 set address name = %somekindofvariable% 搜索局域网适配器,但我不确定如何使用 netsh.

设置该变量

我需要比我擅长的人的帮助。

@echo off
CLS
:Menu
Echo.
Echo.......................................................................
Echo Press 1, 2, or 3 to Exit. 
echo.......................................................................
echo.
echo 1 - Set IP for HHS
echo 2 - Set IP for City
ech0 3 - Exit
echo.

Set /P M=Type 1, 2, or 3 then press ENTER:
IF %M%==1 GOTO HHS
IF %M%==2 GOTO CITY
IF %M%==3 GOTO EOF

:City
echo Input IP Address
set /P "address=type in address: " 
echo address is: %address%
@echo off
netsh interface IPv4 set Address name="Local Area Connection" static %address% 255.255.128.0 10.10.1.2
echo Set IP
@echo off
netsh interface IPv4 set dns "Local Area Connection" static 10.10.1.17 no
echo set dns
netsh interface IPv4 add dns "Local Area Connection" 10.10.1.19 index=2 no
echo Set alternate dns
echo .
echo . 
echo . 
ipconfig /all
GOTO Menu

:HHS
Echo This does not have anything associated with it yet.
GOTO Menu

ipconfig /all
GOTO Menu

感谢任何帮助。谢谢。

这个潜在解决方案背后的想法是,互联网连接的路由 table 应该显示目的地 0.0.0.0,掩码也为 0.0.0.0有可能不止一个接口可以做到这一点,但可能性要小得多,所以它并不完美,但比你之前的想法要好。

@Echo Off&SetLocal EnableExtensions DisableDelayedExpansion
Set "IntName="&For /F "Tokens=1,2,4" %%G In (
    '%__APPDIR__%ROUTE.EXE -4 PRINT^|%__APPDIR__%findstr.exe /RC:"0.0.0.0\ *0.0.0.0"'
) Do (Set "_=%%I"&For /F Tokens^=1-2Delims^=^" %%J In (
        '%__APPDIR__%cmd.exe /D/V/C "%__APPDIR__%netsh.exe interface IP show config|%__APPDIR__%findstr.exe "\" !_:.=\.!""'
    ) Do (Call Set "$=%%_%%"&Set "_=%%K"
        Echo(%%J|%__APPDIR__%find.exe "%%I">NUL&&(Call Set "IntName=%%$%%"&GoTo Next)))
:Next
If Not Defined IntName GoTo :EOF
Set IntName&Pause

在这种情况下,添加最后一行只是为了演示目的,(它应该输出一个变量名及其值字符串),如果您的代码达到这一点,变量 %IntName% 应包含您要查找的 'Interface Name' 字符串。

附带说明一下,这个潜在的解决方案不关心您的互联网连接是有线还是无线,它只会尝试检测互联网连接接口(希望只有一个)。