批处理查看本机IP配置信息及本机端口号占用情况

保存以下内容到chkLocalip.bat,可以查看本机IP配置情况

复制代码
@echo off&echo.

setlocal

if /i "%*" == "/?" goto :HelpPrompt
if /i "%*" == "-Help" goto :HelpPrompt

set "nicstr=Index^,Name^,NetConnectionID^,Speed^"
set "netstr=DefaultIPGateway^,DHCPEnabled^,DNSServerSearchOrder^,IPAddress^,IPSubnet^,MACAddress^"

for /f "tokens=1,3-5 delims=,:" %%i in ('^
        wmic nic where "Manufacturer<>'Microsoft' and NetConnectionStatus='2'" get %nicstr% /format:csv ^|^
        findstr /v "^$ %nicstr:^=%" ^|^
        findstr /n .*^
        ') do (
    echo.%%i. %%l, %%k
    setlocal EnableDelayedExpansion
    for /f "tokens=1-3 delims={}," %%a in ('wmic nicconfig where "Index='%%j'" get %netstr% /value ^| findstr /v "^$"') do (
        set "%%a%%~b"
        for /f %%c in (%%c) do if /i "%%a" == "DNSServerSearchOrder=" set "DNSServerSearchOrder=!DNSServerSearchOrder!, %%~c"
    )
    if "!DHCPEnabled:TRUE=!" == "!DHCPEnabled!" (echo.IPv4地址: !IPAddress! ^(手动^)) else echo.IPv4地址: !IPAddress! ^(自动^)
    echo.子网掩码: !IPSubnet!
    echo.默认网关: !DefaultIPGateway!
    echo.MAC 地址: !MACAddress!
    echo.DNS 地址: !DNSServerSearchOrder!
    endlocal
    echo.
)

endlocal

pause
goto :eof


:HelpPrompt
echo.CheckIP [Version 1.0]  -  by Sonar
echo.A command-line script to check network details
goto :eof
View Code
复制代码

 

保存以下内容到chkLocalPort.bat,可以查看本机端口号占用情况

复制代码
@echo off&echo.

setlocal

set TCP_PID=<nul
set UDP_PID=<nul
set PID=<NUL

if /i "%*" == "" goto :HelpPrompt
if /i "%*" == "/?" goto :HelpPrompt
if /i "%*" == "-Help" goto :HelpPrompt

:TCP
echo.-- TCP 端口 --
for /f "tokens=2-5" %%1 in ('netstat -ano ^| findstr /v "]:" ^| findstr "TCP" ^| findstr /r /c:":%1  *[0-9]"') do (
    set "TCP_PID=%%4"
    echo.内^<--^>外: %%1 ^<==^> %%2  ^(%%3^)
)
if not defined TCP_PID echo.TCP端口 %1 未被占用
echo.

:UDP
echo.-- UDP 端口 --
for /f "tokens=2-4" %%1 in ('netstat -ano ^| findstr /v "]:" ^| findstr "UDP" ^| findstr /c:":%1 "') do (
    set "PID=%%3"
    echo.内^<--^>外: %%1 ^<==^> %%2
)
if not defined PID (
    echo.UDP端口 %1 未被占用
    if not defined TCP_PID goto :Quit
    set "PID=%TCP_PID%"
)
echo.

:Process
echo.-- 进程服务 --
for /f "tokens=1-2,*" %%1 in ('tasklist -svc ^| findstr /c:" %PID% "') do (
    echo.占用服务: %%3
    echo.进程名称: %%1 ^(PID: %%2^)
)

:Quit
endlocal
goto :eof

:HelpPrompt
echo.CheckPort [Version 1.0]  -  by Sonar
echo.A command-line script to check local port status
echo.
echo.    Usage: chkport ^<port^>
goto :Quit
View Code
复制代码

 

posted on   jack_Meng  阅读(150)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2018-05-04 ASP.NET 5 & MVC6系列教程
2014-05-04 C# 控制台程序(命令行程序)设置字体颜色,窗口宽高,光标行数
2014-05-04 C# 控制台程序设置字体颜色
2014-05-04 C#内存释放(垃圾回收)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩