Native open ports and batch processing of processes using this port _dos/bat

Source: Internet
Author: User
Tags port number
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
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.