Here you go. Pay it forward. :) To answer your question directly, just use a for
loop to loop through your servers and perform a portqry
on each. Edit: That PowerShell snippet you found is useful for getting rid of the PortQry dependency.
@echo off
setlocal
set "servers=dev1 dev2 dev3 test1 test2 test2:8080 prod prod:443"
for %%I in (%servers%) do (
for /f "tokens=1,2 delims=:" %%a in ("%%I") do (
set "port=%%~b"
if not defined port set "port=80"
setlocal enabledelayedexpansion
call :handshake "%%~a" "!port!" && (
echo %%a port !port!: OK
) || (
echo %%a port !port!: Error
)
endlocal
)
)
goto :EOF
:handshake <server> <port>
powershell "$t=new-object Net.Sockets.TcpClient;$c=$t.BeginConnect('%~1',%~2,{},{});if($c.AsyncWaitHandle.WaitOne(1000)){$t.EndConnect($c);exit 0};exit 1"
exit /b %ERRORLEVEL%
Here's the original solution using PortQry 2.0:
@echo off
setlocal
set "servers=dev1 dev2 dev3 test1 test2 test2:8080 prod prod:443"
for %%I in (%servers%) do (
for /f "tokens=1,2 delims=:" %%a in ("%%I") do (
set "port=%%~b"
if not defined port set "port=80"
setlocal enabledelayedexpansion
portqry -n "%%~a" -e "!port!" >NUL 2>NUL && (
echo %%a port !port!: OK
) || (
echo %%a port !port!: Error
)
endlocal
)
)
If all you are testing are web services, it might make more sense to go about this in a different way. You can use the Microsoft.XMLHTTP
COM object to get rid of that portqry
dependency; and the responses acquired thusly will be more relevant to HTTP services. (For example, if you've got a VNC server running on port 8080 where you expect a web service to be listening instead, portqry
would probably return success when you'd need it to return fail.)
Anyway, save this as a .bat script and salt to taste.
@if (@CodeSection == @Batch) @then
@echo off
setlocal
set "servers=dev1 dev2 dev3 test1 test2 test2:8080 prod prod:443"
for %%I in (%servers%) do (
for /f "tokens=1,2 delims=:" %%a in ("%%I") do (
set "port=%%~b"
if not defined port set "port=80"
setlocal enabledelayedexpansion
cscript /nologo /e:JScript "%~f0" "%%~a" "!port!" && (
echo %%a port !port!: OK
) || (
echo %%a port !port!: Error
)
endlocal
)
)
goto :EOF
@end // end batch / begin JScript chimera
var server = WSH.Arguments(0),
port = WSH.Arguments(1),
protocol = port == 443 ? 'https' : 'http',
URL = protocol + '://' + server + ':' + port + '/',
XHR = WSH.CreateObject('Microsoft.XMLHTTP');
XHR.open('GET', URL);
XHR.setRequestHeader('User-Agent','XMLHTTP/1.0');
XHR.send('');
while (XHR.readyState != 4) WSH.Sleep(25);
WSH.Quit(XHR.status - 200);