Solved

Batch File to Telnet

Posted on 2014-03-20
10
4,017 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 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 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
Industry Leaders: 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!

 

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

Independent Software Vendors: 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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

724 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