4

How can I find a free port with a batch file?
I tried to run a loop and using the netstat -o -n -a it will increment a variable until the port is not found in the netstat list

But I'm also not sure if this is the best way to find a free port.

set freePort=
set startPort=80

:SEARCHPORT 
netstat -o -n -a | findstr ":%startPort%" 
if %ERRORLEVEL% equ 0 
( echo "port unavailable %ERRORLEVEL%"
  set /a startPort +=1
  GOTO :SEARCHPORT 
) ELSE (
    echo "port available %ERRORLEVEL%"
    set freePort=%startPort%
    GOTO :FOUNDPORT 
   )

:FOUNDPORT
echo free %freePort%
3
  • use netstat in a batch may help u Commented Oct 8, 2013 at 11:16
  • findstr ":%startPort%" is not going to work because it also matches :8085 (which is open here) You should also clean up the code and insert some returns. (And use the code-button in the editor instead of >)
    – Rik
    Commented Oct 8, 2013 at 11:22
  • You could change findstr ":%startPort%" in findstr ":%startPort% ", note the space after the last % but you'll also get outgoing connections on :80 and I expect you only want to skip :80 for incoming. With -a you also get all outgoing connections.
    – Rik
    Commented Oct 8, 2013 at 11:31

2 Answers 2

5

You'll need to change your

netstat -o -n -a | findstr ":%startPort%"

in

netstat -o -n -a | find "LISTENING" | find ":%startPort% "

The find "LISTENING" limits your search to only incoming listening ports and you need the space after the lat % because else you'll match :8085 too.

You also had some other errors in your .bat.

  • In the if statement you needed to wrap the %ERRORLEVEL% around ".
  • You need the ( on the same line as the if statement.
  • I changed the echo from %ERRORLEVEL% to echo the %startPort%.

Here is a correct working one:

@echo off
set freePort=
set startPort=80

:SEARCHPORT
netstat -o -n -a | find "LISTENING" | find ":%startPort% " > NUL
if "%ERRORLEVEL%" equ "0" (
  echo "port unavailable %startPort%"
  set /a startPort +=1
  GOTO :SEARCHPORT
) ELSE (
  echo "port available %startPort%"
  set freePort=%startPort%
  GOTO :FOUNDPORT
)

:FOUNDPORT
echo free %freePort%
2
  • the ports in listening are not already associated with some application? I'm searching for a port that I can associate to a web service.
    – Navy Seal
    Commented Oct 8, 2013 at 12:00
  • You want to associate a port with your a web service on your own computer then you'll only need to check the LISTENING ports and loop them up until you find one that's not assigned. Theoretically an application could have taken a local port 81 for outgoing connection but this shouldn't happen because applications need to start from 1024 for outgoing ports. The connections to remote ports 80 doesn't matter. the connection is from your i.e. local_address:59206 to remote_address:80. So your local_address:80 is still available. With this batch-file you can get the correct port.
    – Rik
    Commented Oct 8, 2013 at 12:17
1

Usually net stat command of windows would help you to find port statistics

You could try like this using conditional statements

@echo off
netstat -o -n -a | findstr ZXCZXCZCZX  
if %ERRORLEVEL% equ 0
(@echo "port is available") ELSE (@echo "port is unavailable")
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .