Solved

Batch File to Telnet

Posted on 2014-03-20
10
3,278 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

27 Experts available now in Live!

Get 1:1 Help Now