Solved

Batch File to Telnet

Posted on 2014-03-20
10
3,161 Views
Last Modified: 2014-03-21
Can you please provide me a batch script or VB script that will telnet IPs and ports and store the result in a text file.

Destination IP      Destination Port
192.168.x.01               8080
192.168.x.10                9001
192.168.x.30               1494

Telnet result will be stored in a text file like-
192.168.x.01               8080 --> OK
192.168.x.10                9001 ---> PROBLEM
192.168.x.30               1494 --> OK
0
Comment
Question by:hchabria
  • 5
  • 4
10 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 39941820
You will have to do with this:

Set telnetsession = CreateObject("WScript.Shell")
 telnetsession.Run "cmd.exe"
 WScript.Sleep 1000
 telnetsession.SendKeys"telnet 127.0.0.1 21 -f c:\out.txt"
 telnetsession.SendKeys("{Enter}")
 WScript.Sleep 1000
 telnetsession.SendKeys("quit") 
 telnetsession.SendKeys("{Enter}")

Open in new window


As you can see I was testing on port 21 (FTP), and output EVERYTHING to c:\out.txt

You will have to adjust a few things. That's because the output will be overwritten, so you have to run 3 seperate script with 3 output files.

Second, you have to interpret those 3 files, and combine them into one result file, and delete the 3 output files.

You will have to do that yourself, as don't know what you mean with OK or PROBLEM. I don't know if you have a specific return being ok, or a different response is already a problem. Or that you mean that a timeout is also a problem or not.
Also port 9100 (printer) will be a bit problematic, as it doesn't really respond in text!
0
 

Author Comment

by:hchabria
ID: 39941860
I have not mentioned 9100. It's 9001.

I am not that much expert in scripting. To be simple, I want an output in a .txt file that will reflect the following result-

192.168.x.1               8080 --> Telnet is OK
192.168.x.10               9001 ---> PROBLEM with Telnet
192.168.x.30               1494 --> Telnet is OK

Here, "Telnet is OK" means --> telnet is successful to port 8080 on 192.168.x.1
Here, "PROBLEM with Telnet" means --> unable to connect port 9001 on 192.168.x.10

Please help me in providing then entire script.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39942025
Your verbage is wrong. You want to just test the connection to specific ports on specific machines. But what makes a success? Telnet includes some application level protocols (negotiating terminal capabilities and settings like "echo input") and more, so testing for that would fail on ports different from 23 (telnet).

Using psping from www.sysinternals.com makes it easy (put the following into a .cmd file):
@echo off
for /F "tokens=1-2" %%A in (c:\temp\EE\portlist.txt) do (
  psping -4 -n 1 -w 0 -q %%A:%%B 2>nul | find "Lost = 1" >nul && echo %%A %%B -- PROBLEM || echo %%A %%B -- ok
) >> C:\temp\EE\portlist.log

Open in new window

0
 

Author Comment

by:hchabria
ID: 39942432
Thanks for your clarification. Do you mean it is not possible to store the telnet result in a text file?

Is it possible to develop any batch file that will telnet multiple IPs with multiple ports at a time?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39942489
What is wrong with my approach? It should do exactly what you want, without further ado.
Why are you insisting on using telnet?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:hchabria
ID: 39942580
Okay. What shall I mention in "c:\temp\EE\portlist.txt"? Shall I mention Destination IP and Destination Port both?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39942706
Yes. Separate them by one or several spaces or tabs, like:
192.168.x.01   8080
192.168.x.10   9001
192.168.x.30   1494

Open in new window

0
 

Author Comment

by:hchabria
ID: 39942882
Thanks. It is working fine. Is it possible to overwite the result instead of appending because everytime I run the script the output is getting added to the .log file instead of overwriting.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39944679
This writes the log file at once, so you won't see anything until it is finished:
@echo off
(for /F "tokens=1-2" %%A in (c:\temp\EE\portlist.txt) do (
  psping -4 -n 1 -w 0 -q %%A:%%B 2>nul | find "Lost = 1" >nul && echo %%A %%B -- PROBLEM || echo %%A %%B -- ok
)) > C:\temp\EE\portlist.log

Open in new window

The following will write intermediate results:
del C:\temp\EE\portlist.log >nul 2>nul
for /F "tokens=1-2" %%A in (c:\temp\EE\portlist.txt) do (
  psping -4 -n 1 -w 0 -q %%A:%%B 2>nul | find "Lost = 1" >nul && echo %%A %%B -- PROBLEM || echo %%A %%B -- ok
) >> C:\temp\EE\portlist.log

Open in new window

The only difference to the code I provided first is the deletion of the log file prior writing into it.
0
 

Author Closing Comment

by:hchabria
ID: 39945146
Thank you so much. It resolved by purpose.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now