0

I have a batch script set to launch different servers, all listening on different ports but the same ip. In order to not lock up the computer running the servers, I want the servers to launch 1 at a time.

My solution to this is using telnet and checking to see if the port is open, then proceed to launching the next server.

I know that telnet uses a specific length of time to test the connection, am I able to set it to be indefinite so that it doesn't timeout, or at least several minutes, and on fail it could throw an error to be investigated?

I want my batch file to look something like:

:startserver1

Start server 1 on port 101

telnet ip 101

if connection is successful, goto :startserver2

etc.

knorberg
  • 462
  • 5
  • 19

1 Answers1

0

You can check if an connection on your port (101) is available

start telnet ip 101
call :waitForPort 101 
goto :end

:waitForPort %1 %2
set PORTTOWAITFOR=%1
set MAXWAITTIME=%2
set SLEEPTIME=3
set LOOPCOUNTER=0
if "%MAXWAITTIME%"=="" echo Waiting for port %PORTTOWAITFOR% ...
if not "%MAXWAITTIME%"=="" echo Waiting for port %PORTTOWAITFOR% for %MAXWAITTIME% seconds ...
:waitForPortLoop
set REPEATLOOP=TRUE
if not "%MAXWAITTIME%"=="" set REPEATLOOP=FALSE & call :checkWaitTime
if not "%REPEATLOOP%"=="TRUE" echo leave because timeout of %MAXWAITTIME% was reached & goto :end
call :sleep %SLEEPTIME%
netstat -n|%windir%\system32\find.exe "%PORTTOWAITFOR%" 2> nul 1> nul
if not %ERRORLEVEL%==0 goto :waitForPortLoop
if %ERRORLEVEL%==0 goto :end

:checkWaitTime
set /A ACTUAL_WAITTME=%LOOPCOUNTER%*%SLEEPTIME%
rem echo waited %ACTUAL_WAITTME% seconds
if %ACTUAL_WAITTME% lss %MAXWAITTIME% set REPEATLOOP=TRUE
set /A LOOPCOUNTER=%LOOPCOUNTER%+1
goto :end