Windows batch & CMD: telnet command and export result

Luis Diaz
Luis Diaz used Ask the Experts™
on
Hello experts,
I need to run telnet through cmd to check the access of url related to 6000 till 6100 port.
I am looking for a .bat that:
1-Read the various IP reported in a txt file with port:
Example:
url 6000
url 6001
Export the result for each line export result (in the same file).
File should be located at %cd% folder.
If you have questions, please contact me.
Thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
@LD16,

When a telnet session succeeds, you will be left with it running.  Do you also want the script to delete those after noting that the connection was successful?  I think to do that would mean killing any active telnet tasks, will that work?


»bp
As Bill Prew pointed out, issuing the telnet command leaves the process running.  I'm hoping that piping a quit command into telnet will allow it to connect but then immediately exit.  Save the following as a bat/cmd script, invoke it with a filename parameter, and it will read each URL Port in the file, attempt the telnet, and place the results in another file (filename.out).
@echo off
cls
IF "%1"=="" (
	echo Usage:  %0 filename
	echo         where filename contains URL Port tuples
) else (
	echo.>%1.out
	FOR /F "tokens=1-2" %%a IN (%1) DO (
		echo quit | telnet %%a %%b
		IF ERRORLEVEL 0 (
			echo SUCCESS  %%a %%b >>%1.out
		) else (
			echo FAIL     %%a %%b >>%1.out
		)
	)
)

Open in new window

Luis DiazIT consultant

Author

Commented:
Thank you for your comments.
If connection success report url +port : connection success else report connection fails.
Each line of  log should be reported for every line reported in txt file. Pause of 3 second between each telnet.
Thank you again.
IT / Software Engineering Consultant
Top Expert 2016
Commented:
@LD16,

I don't think BAT with the built-in TELNET client is going to be useful for what you are describing.  It is not scriptable making it hard to manipulate from BAT.  And I'm pretty sure it doesn't set any useful errorlevel values on exit.

You would probably be better off posting this as a Powershell question, I think the capability there is much better, and using things like System.Net.Sockets.TcpClient you can test for connections to a port better.  But I haven't worked with that so can't quickly propose a solution here.

Something like this question might be a starting point...



»bp
Luis DiazIT consultant

Author

Commented:
Thank you very much for your advice and for your help.
I will be create a new question.
To contextualize the question: the goal is just to test that IT from my organization has properly opened port rules when we try to reach destination IP from organization network. so if telnet replies ok this will means that rules have been properly implemented.
Powershell is the unique approach for this?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial