标题: [网络连接] 【已解决】遍历所有网卡后选择其中一张网卡显示对应的IPv4地址和网关 [打印本页]
作者: semiuel 时间: 2024-2-7 21:53 标题: 【已解决】遍历所有网卡后选择其中一张网卡显示对应的IPv4地址和网关
本帖最后由 semiuel 于 2024-2-11 14:14 编辑
首先参考了Batcher老大的这个帖子:
http://www.bathome.net/viewthread.php?tid=67519
经过大佬的指点,修改后的代码在11楼,欢迎大家测试。
运行批处理前请提前准备好EchoX.exe
下载地址:https://wwf.lanzn.com/iKmg608z89ub
用netsh interface ipv4 show addresses获取到网卡对应的接口名称,IPv4地址和默认网关。
我的电脑上运行后输出内容如下:- 接口 "Radmin VPN" 的配置
- DHCP 已启用: 否
- IP 地址: 26.13.134.145
- 子网前缀: 26.0.0.0/8 (掩码 255.0.0.0)
- 默认网关: 26.0.0.1
- 网关跃点数: 9256
- InterfaceMetric: 1
-
- 接口 "本地连接" 的配置
- DHCP 已启用: 否
- IP 地址: 192.168.1.240
- 子网前缀: 192.168.1.0/24 (掩码 255.255.255.0)
- 默认网关: 192.168.1.1
- 网关跃点数: 256
- InterfaceMetric: 10
-
- 接口 "Loopback Pseudo-Interface 1" 的配置
- DHCP 已启用: 否
- IP 地址: 127.0.0.1
- 子网前缀: 127.0.0.0/8 (掩码 255.0.0.0)
- InterfaceMetric: 50
复制代码
写了下面的代码用来获取所有网卡- @echo off
- setlocal enabledelayedexpansion
- echo 请选择一个网卡:
- set "choice="
- set "count=1"
- for /f "tokens=1 delims=:" %%a in ('netsh interface ipv4 show addresses ^| findstr "接口"') do (
- set adapter=%%a
- echo !count!.!adapter:~4,-5!
- set /a count+=1
- )
- set /p choice=请输入序号:
复制代码
接下来不知道该怎么写来获取每张网卡的IPv4地址和默认网关了。
我的本意是想跟这个帖子里的IP扫描结合起来
http://www.bathome.net/thread-68407-1-1.html
用这个帖子里获取到的IPv4地址和默认网关来确定IP扫描的网段。
请求大神们帮忙看看。
作者: semiuel 时间: 2024-2-8 10:49
大佬们都回家过年了啊!
祝大家新春快乐!
作者: holley 时间: 2024-2-8 10:54
本帖最后由 holley 于 2024-2-8 11:11 编辑
回复 1# semiuel
先把网卡名称读出来:- @echo off
- setlocal enabledelayedexpansion
-
- echo 请选择一个网卡:
- set "choice="
- set "count=1"
- for /f "tokens=1 delims=:" %%a in ('netsh interface ipv4 show addresses ^| findstr "接口"') do (
- set "adapter=%%a"
- set "adapter=!adapter:~4,-5!"
- echo !count!. !adapter!
- set "interface!count!=!adapter!"
- set /a "count+=1"
- )
- set /p "choice=请输入序号:"
-
- rem 获取选定网卡的IP及网关信息
- set "interfaceName=!interface%choice%!"
- rem echo 已选网卡:!interfaceName!
-
- for /f "tokens=1-2 delims=:" %%i in ('netsh interface ipv4 show config name^="!interfaceName!" ^| findstr /C:"IP 地址"') do (
- set "ip_line=%%j"
- for /f "tokens=*" %%f in ("!ip_line!") do set "ipAddress=%%f"
- echo !interfaceName!-[IP 地址]:!ipAddress!
- )
-
- rem 提取已选网卡的默认网关
- for /f "tokens=1-2 delims=:" %%i in ('netsh interface ipv4 show config name^="!interfaceName!" ^| findstr /C:"默认网关"') do (
- set "gateway=%%j"
- for /f "tokens=*" %%f in ("!gateway!") do set "gateway=%%f"
- if not "!gateway!"=="" (
- echo !interfaceName!-[默认网关]:!gateway!
- ) else (
- echo 该网卡没有配置默认网关。
- )
- )
-
- pause
复制代码
作者: hnfeng 时间: 2024-2-8 10:57
本帖最后由 hnfeng 于 2024-2-8 10:58 编辑
你只是想获取正在使用的网卡的地址吧,CMD里面:- for /f "tokens=3,4" %i in ('route print -4 ^| findstr /rc:" 0\.0\.0\.0 *0\.0\.0\.0 "') do @echo IP地址: %j, 默认网关:%i
复制代码
作者: holley 时间: 2024-2-8 11:23
回复 2# semiuel
试了一下,结果存疑:- @echo off & mode con cols=40 lines=31 & color f0 & setlocal enabledelayedexpansion
- echo 请选择一个网卡:
- set "choice="
- set "count=1"
- for /f "tokens=1 delims=:" %%a in ('netsh interface ipv4 show addresses ^| findstr "接口"') do (
- set "adapter=%%a"
- set "adapter=!adapter:~4,-5!"
- echo !count!. !adapter!
- set "interface!count!=!adapter!"
- set /a "count+=1"
- )
- set /p "choice=请输入序号:"
- set "interfaceName=!interface%choice%!"
- for /f "tokens=1-2 delims=:" %%i in ('netsh interface ipv4 show config name^="!interfaceName!" ^| findstr /C:"IP 地址"') do (
- set "ip_line=%%j"
- for /f "tokens=*" %%f in ("!ip_line!") do set "IP=%%f"
- REM echo !interfaceName!-[IP 地址]:!IP!
- )
- for /f "tokens=1-2 delims=:" %%i in ('netsh interface ipv4 show config name^="!interfaceName!" ^| findstr /C:"默认网关"') do (
- set "gateway=%%j"
- for /f "tokens=*" %%f in ("!gateway!") do set "GW=%%f"
- if not "!GW!"=="" (
- REM echo !interfaceName!-[默认网关]:!GW!
- ) else (
- echo 该网卡没有配置默认网关。
- )
- )
- echox -n "本机IP:" & echox -w 25 -n -c fc "%IP%" & echox -n -c 9f " " & echox "在线"
- echox -n "网 关:" & echox -w 25 -n -c fc "%GW%" & echox -n -c 80 " " & echox "空闲"
- echo.
- set on=0&set off=0
- for /f "tokens=2-4 delims=.: " %%a in ('ipconfig/all^|findstr "默认网关"') do (
- for /l %%i in (1,1,254) do (
- title IP扫描:%%a.%%b.%%c.%%i
- ping -n 1 -l 1 -w 20 -i 10 -s 1 %%a.%%b.%%c.%%i>nul 2>nul && (set /a on=on+1 & echox -w 4 -e -n -c 9f %%i) || (set /a off=off+1 & echox -w 4 -e -n -c 80 %%i)
- )
- )
- echo.
- title IP扫描:结束
- echox -n -c f0 "扫描结束,在线IP:"&echox -w 4 -r -n -c 9f %on%&echox -n -c f0 " 空闲IP:"&echox -w 4 -r -c 80 %off%
- pause>nul 2>nul
复制代码
作者: newswan 时间: 2024-2-8 11:36
powershell- $ip = Get-NetIPAddress -AddressFamily "IPv4"
- $rt = Get-NetRoute
-
- $ip | add-member -NotePropertyName "NextHop" -NotePropertyValue ""
-
- foreach ( $x in $ip ) {
- $y = $rt | Where-Object { ($_.InterfaceAlias -eq $x.InterfaceAlias) -and ($_.DestinationPrefix -eq '0.0.0.0/0') }
- $x.NextHop = $y.NextHop
- }
-
- $ip | Select-Object interfacealias,nexthop
复制代码
作者: semiuel 时间: 2024-2-8 13:23
回复 5# holley
好的,感谢大佬,我试试看。
作者: semiuel 时间: 2024-2-8 13:25
回复 4# hnfeng
谢谢
作者: semiuel 时间: 2024-2-8 13:26
回复 6# newswan
我想做一个WIN7/10/11通用的脚本,WIN7没办法用POWERSHELL
作者: ppll2030 时间: 2024-2-8 13:53
回复 1# semiuel
根据你的第一部分网卡读取做了一些修改,
完善第二部分地址读取,并设置了变量,
第三部分自己修改一下变量设置,就可拼接上了。- @echo off
- setlocal enabledelayedexpansion
-
- netsh interface ipv4 show addresses>temp.txt
- set "count=1"
- for /f "tokens=1 delims=:" %%a in ('findstr "接口" "temp.txt"') do (
- set adapter=%%a
- echo !count!. !adapter:~4,-5!
- set _!count!=!adapter:~4,-5!
- set /a count+=1
- )
-
- echo 请选择一个网卡:
- set /p choice=请输入序号:
- set AdapterName=!_%choice%!
-
- for /f "delims=:" %%i in ('findstr /n /c:"!AdapterName!" "temp.txt"') do (
- set /a a=%%i+2
- set /a b=%%i+4
- )
-
- :a
- set /a a+=1
- for /f "skip=%a% tokens=2,4 delims= " %%i in ('find /v /n "" temp.txt') do (
- set IP=%%j
- goto b
- )
- :b
- set /a b+=1
- for /f "skip=%b% tokens=2-3 delims= " %%i in ('find /v /n "" temp.txt') do (
- set GW=%%j
- goto end
- )
- :end
- cls
- echo 网卡: %AdapterName%
- echo IP地址: %IP%
- echo 网关地址:%GW%
- del /q temp.txt
- pause
复制代码
作者: semiuel 时间: 2024-2-8 13:56
本帖最后由 semiuel 于 2024-2-8 14:16 编辑
回复 5# holley
稍微修改了一下大佬的代码,试了两台电脑暂时没问题了。- @echo off & mode con cols=40 lines=31 & color f0 & setlocal enabledelayedexpansion
- title IP扫描:选择网卡
- echo 请选择一个网卡:
- set "choice="
- set "count=1"
- for /f "tokens=1 delims=:" %%a in ('netsh interface ipv4 show addresses ^| findstr "接口"') do (
- set "adapter=%%a"
- set "adapter=!adapter:~4,-5!"
- echo !count!. !adapter!
- set "interface!count!=!adapter!"
- set /a "count+=1"
- )
- set /p "choice=请输入序号:"
- set "interfaceName=!interface%choice%!"
- for /f "tokens=1-2 delims=:" %%i in ('netsh interface ipv4 show config name^="!interfaceName!" ^| findstr /C:"IP 地址"') do (
- set "ip_line=%%j"
- for /f "tokens=*" %%f in ("!ip_line!") do set "IP=%%f"
- REM echo !interfaceName!-[IP 地址]:!IP!
- )
- for /f "tokens=1-2 delims=:" %%i in ('netsh interface ipv4 show config name^="!interfaceName!" ^| findstr /C:"默认网关"') do (
- set "gateway=%%j"
- for /f "tokens=*" %%f in ("!gateway!") do set "GW=%%f"
- if not "!GW!"=="" (
- REM echo !interfaceName!-[默认网关]:!GW!
- ) else (
- set "GW=没有配置默认网关"
- )
- )
- cls
- echox -n "本机IP:" & echox -w 25 -n -c fc "%IP%" & echox -n -c 9f " " & echox "在线"
- echox -n "网 关:" & echox -w 25 -n -c fc "%GW%" & echox -n -c 80 " " & echox "空闲"
- echo.
- set on=0&set off=0
- REM 下面这一句修改了一下,改成直接从IP地址获取需要扫描的网段,而不是从网关获取
- for /f "tokens=1-3 delims=.: " %%a in ("%IP%") do (
- for /l %%i in (1,1,254) do (
- title IP扫描:%%a.%%b.%%c.%%i
- ping -n 1 -l 1 -w 20 -i 10 -s 1 %%a.%%b.%%c.%%i>nul 2>nul && (set /a on=on+1 & echox -w 4 -e -n -c 9f %%i) || (set /a off=off+1 & echox -w 4 -e -n -c 80 %%i)
- )
- )
- echo.
- title IP扫描:结束
- echox -n -c f0 "扫描结束,在线IP:"&echox -w 4 -r -n -c 9f %on%&echox -n -c f0 " 空闲IP:"&echox -w 4 -r -c 80 %off%
- pause>nul 2>nul
复制代码
作者: semiuel 时间: 2024-2-8 14:14
回复 10# ppll2030
谢谢大佬
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |