?
Solved

Batch File to Telnet

Posted on 2014-03-20
10
Medium Priority
?
4,329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 36

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 70

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 70

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
 

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 70

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 70

Accepted Solution

by:
Qlemo earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 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