Windows batch & CMD: telnet command and export result

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:
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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:

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?

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
IF "%1"=="" (
	echo Usage:  %0 filename
	echo         where filename contains URL Port tuples
) else (
	FOR /F "tokens=1-2" %%a IN (%1) DO (
		echo quit | telnet %%a %%b
			echo SUCCESS  %%a %%b >>%1.out
		) else (
			echo FAIL     %%a %%b >>%1.out

Open in new window

LD16Author 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.
Bill PrewIT / Software Engineering ConsultantCommented:

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...


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author 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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.