I am novice in all this so please take it easy. :) Trying to make PortQry script, that checks several remote IP addresses from file for specific TCP port to be opened or closed. From that windows server is not allowed to run any 3rd party tools (none MS), so only option is to use some sort of batch script.
Background: We have devices running with several mobile operator sim cards. All IP SIM card addresses are internal (10.*) and devices use specific port. When devices are not working is needed to check not only ping but also if port is working as without it will not be possible to establish connection with them. So this script is not planned to use for monitoring but pure diagnostic.
In same folder where script will be executed are these files:
- "portqry.exe";
- "ip.txt" with IP addresses, where each address is in new row;
Expected result
On window should be showed result in format like: IP, port, listening (opened) or not listening (closed);
the IP addreses where port is opened should be saved in "alive.txt" where each IP is in new line;
the IP addreses where port is closed should be saved in "dead.txt" where each IP is in new line;
Result now
All IP addresses saved to "dead.txt"
On window is repeatedly for each Ip showing
Querying target system called: ip.txt Attempting to resolve now to IP address
Code in bat file:
@echo off
set file=portqry.exe
set iplist=ip.txt
:readfile
for /f "tokens=1-2 delims=:" %%V in (%iplist%) do call :chkport %%V
goto end
:chkport
set ip=%1
portqry -n %ip% -e 5432 -p TCP
if %ERRORLEVEL%==0 echo %IP% >>alive.txt
if NOT %ERRORLEVEL%==0 echo %IP% >>dead.txt
set ip=
goto end
:end