How To Automate Telnet Commands Using VB Script






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.
automate telnet
After I hit enter during this telnet session, the next
menu expects a numeric response, followed by Enter.
automate telnet
Sounds a bit impossible for a scripting job, doesn’t it? Well, never underestimate the power of Visual Basic.
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:
<job>
<script language="VBScript">
Option Explicit
On Error Resume Next
Dim WshShell
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
WScript.Sleep 1000
'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"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
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'
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
WshShell.SendKeys "5"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
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
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WScript.Quit
</script>
</job>
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!

No comments:

Post a Comment