Copy Code code as follows:
@echo off
Color 1f
Title XP Port-Process query
Setlocal enabledelayedexpansion
echo╔--╗
Echo native open ports and processes that use this port
echo╚--╝
Echo------------------------------------
Echo Port number Process name
ECHO TCP protocol:
:: Use the netstat command to find the port that uses TCP protocol communication, and divide the result;
:: Pass the second parameter (IP plus port) to%%i, and the fifth parameter (PID number) is passed to%%j;
for/f "Usebackq skip=4 tokens=2,5"%%i in (' Netstat-ano-p TCP "') does (
Call:assoc%%i TCP%%j
Echo! Tcp_port!! tcp_proc_name!
)
ECHO UDP Protocol:
for/f "Usebackq skip=4 tokens=2,4"%%i in (' Netstat-ano-p UDP "') does (
Call:assoc%%i UDP%%j
Echo! Udp_port!! udp_proc_name!
)
echo presses any key to exit
Pause>nul
: Assoc
:: Partition%1 (first parameter) to pass the second argument to%%e. In this program,%1 is the%%i above (in the form of: IP: Port number)
for/f "tokens=2 delims=:"%%e in ("%1") does (
Set%2_port=%%e
)
:: Query pid equals%3 (third parameter) process, and pass the result to variable? _proc_name,? represents UDP or TCP;
for/f "skip=2 usebackq delims=, Tokens=1"%%a in (' Tasklist/fi "PID eq.%3"/fo CSV ") Do (
::%%~a means to remove the quotation marks outside the%%a, as the result of the above command is enclosed in parentheses.
Set%2_proc_name=%%~a
)