windows bat 获取特定ip地址对应的网络接口的名称

在windows cmd 输入 ipconfig /all 结果:

Windows IP 配置


   主机名  . . . . . . . . . . . . . : D1300001283
   主 DNS 后缀 . . . . . . . . . . . : united-imaging.com
   节点类型  . . . . . . . . . . . . : 混合
   IP 路由已启用 . . . . . . . . . . : 否
   WINS 代理已启用 . . . . . . . . . : 否
   DNS 后缀搜索列表  . . . . . . . . : united-imaging.com


以太网适配器 本地连接:


   连接特定的 DNS 后缀 . . . . . . . : united-imaging.com
   描述. . . . . . . . . . . . . . . : Intel(R) 82579LM Gigabit Network Conne
on
   物理地址. . . . . . . . . . . . . : 44-37-E6-8B-5D-4A
   DHCP 已启用 . . . . . . . . . . . : 是
   自动配置已启用. . . . . . . . . . : 是
   本地链接 IPv6 地址. . . . . . . . : fe80::9cb0:f55f:bb82:3b1f%10(首选)
   IPv4 地址 . . . . . . . . . . . . : 10.8.97.75(首选)
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   获得租约的时间  . . . . . . . . . : 2016-05-12 13:09:56
   租约过期的时间  . . . . . . . . . : 2016-05-20 13:09:55
   默认网关. . . . . . . . . . . . . : 10.8.97.254
   DHCP 服务器 . . . . . . . . . . . : 10.6.2.6
   DHCPv6 IAID . . . . . . . . . . . : 239351782
   DHCPv6 客户端 DUID  . . . . . . . : 00-01-00-01-1A-B9-52-BB-44-37-E6-8B-5D


   DNS 服务器  . . . . . . . . . . . : 10.6.2.6
                                       10.6.2.1
   TCPIP 上的 NetBIOS  . . . . . . . : 已启用


隧道适配器 isatap.united-imaging.com:


   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . : united-imaging.com
   描述. . . . . . . . . . . . . . . : Microsoft ISATAP Adapter
   物理地址. . . . . . . . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP 已启用 . . . . . . . . . . . : 否
   自动配置已启用. . . . . . . . . . : 是


隧道适配器 Teredo Tunneling Pseudo-Interface:


   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . :
   描述. . . . . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
   物理地址. . . . . . . . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP 已启用 . . . . . . . . . . . : 否
   自动配置已启用. . . . . . . . . . : 是


主机IP地址 10.8.97.75(首选)  , 对应的interface 名称: 本地连接。  要得到此名称,如下bat:


@echo off
setlocal enabledelayedexpansion
set name=
set matchip=no
echo matchip: %matchip%
for /f "tokens=*" %%i in ('netsh interface ipv4 show ipaddress ^| findstr ": 10.8.97"') do (
echo %%i 
set thisstr=%%i
echo "name: !name!"
if defined name ( 
echo !thisstr! | findstr "10.8.97" && ( echo yes && set matchip=yes)|| set matchip=no
echo "matchip: !matchip!"
if "!matchip!"=="yes" (
echo "found" 
goto :FOUND
)
)
@REM for /f "tokens=* delims=:" %%a in ("!thisstr!") do (
@REM set name=%%a
@REM )
echo !thisstr! | findstr ":" && set name=!thisstr!
)
:FOUND
set interface=
for /f "tokens=2 delims=:" %%i in ("!name!") do (
set interface=%%~i
)
@REM remove begining space(only the first one space is not name, other spaces may be in name) 
if "%interface:~0,1%"==" " set interface=%interface:~1% 
echo find interface name:"%interface%"
endlocal
pause


猜你喜欢

转载自blog.csdn.net/zztan/article/details/51384294