Created
July 20, 2025 15:29
-
-
Save msenturk/bf061bb27952cee3c1b8d8923e529547 to your computer and use it in GitHub Desktop.
Get listening port of process OR listening port to process.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal EnableDelayedExpansion | |
call :InitColors | |
if "%~1"=="" goto usage | |
if /i "%~1"=="--help" goto usage | |
if /i "%~1"=="-h" goto usage | |
if /i "%~1"=="/?" goto usage | |
set "arg1=%~1" | |
rem Check if argument is a port number (digits only) | |
echo %arg1%| findstr /r "^[1-9][0-9]*$" >nul | |
if not errorlevel 1 ( | |
if %arg1% GTR 65535 ( | |
echo %RED%Error: Port number must be between 1 and 65535%RST% | |
exit /b 1 | |
) | |
call :FindProcessByPort %arg1% | |
exit /b | |
) | |
call :ValidateEnvironment | |
if errorlevel 1 exit /b 1 | |
rem Sanitize and normalize process name | |
set "procname=%arg1%" | |
call :SanitizeInput "!procname!" procname | |
call :NormalizeProcessName "!procname!" procname | |
echo %BLU%Searching for process: %procname%...%RST% | |
call :CreateTempFiles | |
if errorlevel 1 ( | |
echo %RED%Error: Cannot create temporary files%RST% | |
exit /b 1 | |
) | |
tasklist /fi "imagename eq %procname%" /fo csv /nh > "%tmp_pids%" 2>nul | |
if errorlevel 1 ( | |
echo %RED%Error: Failed to query process list%RST% | |
call :Cleanup | |
exit /b 1 | |
) | |
rem Check if any processes found | |
>nul 2>&1 findstr /r "." "%tmp_pids%" | |
if errorlevel 1 ( | |
echo %YEL%No process found with name: %procname%%RST% | |
call :Cleanup | |
exit /b 0 | |
) | |
rem Get network information once | |
echo %BLU%Gathering network information...%RST% | |
netstat -ano > "%tmp_netstat%" 2>nul | |
if errorlevel 1 ( | |
echo %RED%Error: Failed to get network information%RST% | |
call :Cleanup | |
exit /b 1 | |
) | |
rem Process results | |
call :ProcessResults | |
call :Cleanup | |
exit /b 0 | |
rem Initialize color codes | |
:InitColors | |
set "COLOR_ENABLED=0" | |
if not "%WT_SESSION%"=="" set "COLOR_ENABLED=1" | |
if "%TERM%"=="xterm" set "COLOR_ENABLED=1" | |
if "%TERM%"=="xterm-256color" set "COLOR_ENABLED=1" | |
if "%ConEmuANSI%"=="ON" set "COLOR_ENABLED=1" | |
if "%COLOR_ENABLED%"=="1" ( | |
for /f "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "ESC=%%E" | |
set "RED=!ESC![1;31m" | |
set "GRN=!ESC![1;32m" | |
set "YEL=!ESC![1;33m" | |
set "BLU=!ESC![1;34m" | |
set "CYN=!ESC![1;36m" | |
set "WHT=!ESC![1;37m" | |
set "RST=!ESC![0m" | |
) else ( | |
set "RED=" & set "GRN=" & set "YEL=" & set "BLU=" & set "CYN=" & set "WHT=" & set "RST=" | |
) | |
exit /b 0 | |
rem Validate system environment | |
:ValidateEnvironment | |
where tasklist >nul 2>&1 | |
if errorlevel 1 ( | |
echo %RED%Error: tasklist command not available%RST% | |
exit /b 1 | |
) | |
where netstat >nul 2>&1 | |
if errorlevel 1 ( | |
echo %RED%Error: netstat command not available%RST% | |
exit /b 1 | |
) | |
if not exist "%TEMP%" ( | |
echo %RED%Error: Temporary directory not accessible%RST% | |
exit /b 1 | |
) | |
exit /b 0 | |
rem Sanitize input to prevent injection | |
:SanitizeInput | |
set "input=%~1" | |
set "input=%input:"=%" | |
set "input=%input:<=_%" | |
set "input=%input:>=_%" | |
set "input=%input:|=_%" | |
set "input=%input:&=_%" | |
set "input=%input:^=_%" | |
set "%~2=%input%" | |
exit /b 0 | |
rem Normalize process name (add .exe, convert to lowercase) | |
:NormalizeProcessName | |
set "name=%~1" | |
if /i not "!name:~-4!"==".exe" set "name=!name!.exe" | |
call :FastToLower "!name!" name | |
set "%~2=%name%" | |
exit /b 0 | |
rem Fast lowercase conversion using substitution | |
:FastToLower | |
set "str=%~1" | |
set "str=%str:A=a%" & set "str=%str:B=b%" & set "str=%str:C=c%" & set "str=%str:D=d%" | |
set "str=%str:E=e%" & set "str=%str:F=f%" & set "str=%str:G=g%" & set "str=%str:H=h%" | |
set "str=%str:I=i%" & set "str=%str:J=j%" & set "str=%str:K=k%" & set "str=%str:L=l%" | |
set "str=%str:M=m%" & set "str=%str:N=n%" & set "str=%str:O=o%" & set "str=%str:P=p%" | |
set "str=%str:Q=q%" & set "str=%str:R=r%" & set "str=%str:S=s%" & set "str=%str:T=t%" | |
set "str=%str:U=u%" & set "str=%str:V=v%" & set "str=%str:W=w%" & set "str=%str:X=x%" | |
set "str=%str:Y=y%" & set "str=%str:Z=z%" | |
set "%~2=%str%" | |
exit /b 0 | |
:CreateTempFiles | |
set "session_id=%RANDOM%_%RANDOM%" | |
set "tmp_pids=%TEMP%\pids_%session_id%.txt" | |
set "tmp_netstat=%TEMP%\netstat_%session_id%.txt" | |
rem Test if we can create files | |
echo test > "%tmp_pids%" 2>nul | |
if errorlevel 1 exit /b 1 | |
del /f /q "%tmp_pids%" >nul 2>&1 | |
exit /b 0 | |
:ProcessResults | |
setlocal EnableDelayedExpansion | |
set "proc_count=0" | |
echo. | |
for /f "usebackq tokens=1,2,5 delims=," %%a in ("%tmp_pids%") do ( | |
set "pname=%%~a" | |
set "pid=%%~b" | |
set "memory=%%~c" | |
set /a proc_count+=1 | |
echo %GRN%[!proc_count!] Process: !pname! ^(PID: !pid!, Memory: !memory!^)%RST% | |
echo %CYN% Listening ports:%RST% | |
echo Proto Local Address Foreign Address State PID | |
echo ----- ------------- --------------- ----- --- | |
set "found_ports=0" | |
for /f "usebackq tokens=*" %%l in (`findstr /r /c:"LISTENING" "%tmp_netstat%" ^| findstr /r /c:" !pid!$" ^| findstr /v /c:"["`) do ( | |
echo %WHT%%%l%RST% | |
set "found_ports=1" | |
) | |
if "!found_ports!"=="0" ( | |
echo %YEL%No listening ports found%RST% | |
) | |
echo. | |
) | |
if %proc_count%==0 ( | |
echo %RED%No running processes found matching: %procname%%RST% | |
endlocal | |
exit /b 1 | |
) else ( | |
echo %CYN%Summary: Found %proc_count% instance^(s^) of "%procname%"%RST% | |
endlocal | |
exit /b 0 | |
) | |
:FindProcessByPort | |
setlocal EnableDelayedExpansion | |
set "port=%~1" | |
echo %CYN%Searching for processes listening on port %port%...%RST% | |
echo. | |
call :CreateTempFiles | |
if errorlevel 1 ( | |
echo %RED%Error: Cannot create temporary files%RST% | |
exit /b 1 | |
) | |
netstat -ano > "%tmp_netstat%" 2>nul | |
if errorlevel 1 ( | |
echo %RED%Error: Failed to get network information%RST% | |
call :Cleanup | |
exit /b 1 | |
) | |
echo Proto Local Address Foreign Address State PID Process | |
echo ----- ------------- --------------- ----- --- ------- | |
set "found_connections=0" | |
for /f "usebackq tokens=1,2,3,4,5" %%a in (`findstr /r /c:"LISTENING" "%tmp_netstat%" ^| findstr /r /c:":%port% " ^| findstr /v /c:"["`) do ( | |
set "proto=%%a" & set "local=%%b" & set "foreign=%%c" & set "state=%%d" & set "pid=%%e" | |
rem Get process name for this PID | |
set "process_name=Unknown" | |
for /f "usebackq skip=1 tokens=1 delims=," %%p in (`tasklist /fi "PID eq !pid!" /fo csv /nh 2^>nul`) do ( | |
set "process_name=%%~p" | |
set "found_connections=1" | |
) | |
echo !proto! !local! !foreign! !state! !pid! !process_name! | |
) | |
call :Cleanup | |
if "!found_connections!"=="0" ( | |
echo %YEL%No processes found listening on port %port%%RST% | |
endlocal | |
exit /b 0 | |
) else ( | |
echo. | |
echo %GRN%Found processes listening on port %port%%RST% | |
endlocal | |
exit /b 0 | |
) | |
:Cleanup | |
if defined tmp_pids if exist "%tmp_pids%" del /f /q "%tmp_pids%" >nul 2>&1 | |
if defined tmp_netstat if exist "%tmp_netstat%" del /f /q "%tmp_netstat%" >nul 2>&1 | |
exit /b 0 | |
:usage | |
echo. | |
echo %CYN%Network Process Port Finder v2.0%RST% | |
echo %WHT%================================%RST% | |
echo. | |
echo %YEL%Usage:%RST% | |
echo. | |
echo %~nx0 ^<process_name^> - Find listening ports for a process | |
echo %~nx0 ^<port_number^> - Find processes listening on a port | |
echo. | |
echo %YEL%Examples:%RST% | |
echo. | |
echo %~nx0 chrome - Find ports used by Chrome | |
echo %~nx0 ollama - Find ports used by everything | |
echo %~nx0 80 - Find processes on port 80 | |
echo %~nx0 11434 - Find processes on port 443 | |
echo. | |
echo. | |
echo %BLU%Note: Run as Administrator for complete process information%RST% | |
echo. | |
exit /b 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment