Solved

Batch File to Telnet

Posted on 2014-03-20
10
3,731 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 69

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

 

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 69

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 69

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 69

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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