本帖最后由 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
复制代码
|