批处理查看本机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 2023-05-04 15:37  jack_Meng  阅读(149)  评论(0编辑  收藏  举报

导航