Writing batch jobs (.bat) and more recently Windows scripts (.wsf) is an activity that really makes up the bulk of how the large majority of network and system administrators do their jobs faster, and simplify what would otherwise be time-consuming, complex tasks. Using batch jobs, you can automate installing and uninstalling applications, doing an inventory of software and OS settings of all PCs on your network, and a multitude of other queries and jobs. However, there are certain tasks that sometimes go through multiple layers of authentication, such as telnet.
Many network administrators have to telnet into network switches in order to query or set up ports, monitor the health of systems, or even reboot network devices that accept commands via telnet. Wouldn’t it be sweet if you could automate telnet jobs just like you would script regular batch jobs?
The truth is that if you are accustomed to using VB script to create your Windows scripts (or even if you aren’t), VB scripts provides a very useful feature where you can establish the Windows Shell script as an object, and then issue that “object” carefully timed commands. Essentially, this is exactly like you are sitting at a command window and typing in commands. The only difference is that your Windows script is sending the window the commands for you.
Automate Your Telnet Job
There are basically two parts to this task. You need to establish the sequence of commands that you want to go through during the typical telnet session.
Here’s what I want to do. I have 5 devices throughout the network that can be remotely rebooted via telnet by issuing 4 simple commands. I have to first telnet to it using the IP address and a specific port. Next, a menu appears, and I have to first press enter.
After I hit enter during this telnet session, the next menu expects a numeric response, followed by Enter.
Sounds a bit impossible for a scripting job, doesn’t it? Well, never underestimate the power of Visual Basic.
Now, there are other methods to do this. Just check out Abhigyan’s article on Tst10.exe to see how some people like to use the Tst scripting method to automate telnet sessions. Unfortunately, you’ll also see that it can be slightly complex for someone not accustomed to writing many scripts. Tcl is also another similar scripting language programmers have used for years for the same task.
However, I’m going to show you how a VB script file will do the same tasks in a fraction of the time and using a script that is monumentally easier to understand.
So here’s what we’re going to do. I’m going to break up the script into sections. Put all of these into a text file called something like Autotelnet.wsf, double click, and it’ll run.
First – establish the telnet session:
On Error Resume Next
'Send commands to the window as needed - IP and commands need to be customized
'Step 1 - Telnet to remote IP'
WshShell.SendKeys "telnet xx.xx.xx.73 9999"
The section of code above will automatically open a command window and then telnet to the device on whatever specific port you need to connect. Replace “x’s” with your own IP.
The sleep command will wait long enough for the device to respond and prompt your script for the next command. Make sure this wait time is long enough for that action to take place.
Secondly, you need to send each command, one at a time, providing enough wait time between them for the telnet session to respond.
'Step 2 - Issue Commands with pauses'
In this example, I’ve issued the two commands that I noted above. First, I have the script send the “Enter” command, wait a second, then send a “5” and press “Enter” again. This short series of actions will perform exactly as though you were sitting in front of the telnet command window doing them yourself. You’ll just need to customize this script to perform the exact responses that your telnet session requires.
Finally, don’t forget to close the command window and end the script.
'Step 3 - Exit Command Window
That’s all there is to automate telnet – three easy steps inside a very uncomplicated script. Just take the three sections above and customize them to your heart’s desire. You’ll be automating all of your support tasks to manage network switches, time clocks or other remote systems that communicate via telnet.
If you have repetitive tasks that you have to do often, make your life a lot simpler by creating automated Windows scripts that’ll do those tasks for you. You’ll be more productive, and your boss will be really impressed!
Do you have any other ideas for cool tasks you could automate using this kind of Windows script? How have you been automating your own telnet tasks? Share your thoughts and insight in the comments section below.
Image Credit: Shutterstock