Automate Telnet Commands with VB Script

Automating System Administration Tasks with Scripting
The creation of batch jobs, utilizing the .bat extension, and more contemporary Windows scripts (.wsf) constitutes a significant portion of the daily workflow for most network and system administrators. These scripting methods enable faster task completion and streamline processes that would otherwise be lengthy and intricate.
Through the implementation of batch jobs, administrators can automate the installation and removal of software applications. They can also perform comprehensive inventories of software and operating system configurations across all computers on a network, alongside numerous other inquiries and automated procedures.
Challenges with Multi-Layered Authentication
Certain operations, however, frequently necessitate navigating multiple authentication protocols, a common example being the use of telnet. This can complicate automation efforts.
A substantial number of network administrators rely on telnet to access network switches. This access is crucial for querying and configuring ports, monitoring system status, and even remotely rebooting network devices that respond to telnet commands.
The ability to automate these telnet-based tasks, mirroring the scripting capabilities of standard batch jobs, would represent a considerable efficiency gain.
Leveraging VB Script for Automation
VB script offers a powerful solution for automating tasks that require interaction with the command line. Even for those less familiar with VB script, its features are readily applicable.
Specifically, VB script allows you to instantiate the Windows Shell script as an object. Subsequently, precisely timed commands can be issued to this object.
This process effectively simulates direct interaction with a command window, where commands are entered manually. The key distinction is that the Windows script transmits these commands to the window automatically.
This method provides a robust framework for automating complex administrative tasks, including those requiring telnet access and multi-layered authentication.
Automating Telnet Operations
The process of automating telnet tasks generally involves two key stages. First, a defined sequence of commands for a typical telnet session must be established.
Consider this scenario: I need to remotely reboot five devices on a network using telnet, requiring just four straightforward commands. This begins with initiating a telnet connection to each device using its IP address and a designated port.
Upon connection, a menu is presented, necessitating an initial press of the Enter key.

Following the initial Enter key press, the subsequent menu prompts for a numerical input, again followed by pressing Enter.

Such a process might appear challenging to automate through scripting, however, the capabilities of Visual Basic should not be underestimated.
Alternative methods exist for achieving this. Abhigyan's article on Tst10.exe details the use of Tst scripting to automate telnet sessions. However, it's worth noting that this approach can be somewhat complex for those unfamiliar with extensive scripting.
I will demonstrate how a VB script can accomplish the same tasks in a significantly reduced timeframe, utilizing a script that is considerably easier to comprehend.
The script will be divided into sections for clarity. Save all of these components into a text file, such as Autotelnet.wsf, and execute it by double-clicking.
First, let's 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
This code segment will automatically launch a command window and then initiate a telnet connection to the specified device on the required port. Remember to replace the "x's" with the appropriate IP address.
The sleep command introduces a delay, allowing sufficient time for the device to respond and prompt the script for the next command. Ensure this delay is adequate for the action to complete.
Next, each command must be sent individually, with appropriate pauses between them to allow for telnet session responses.
'Step 2 - Issue Commands with pauses'
WshShell.SendKeys ("{Enter}")WScript.Sleep 1000
WshShell.SendKeys "5"
WshShell.SendKeys ("{Enter}")WScript.Sleep 1000
In this instance, the two commands previously mentioned are issued. The script first sends the "Enter" command, waits for one second, then sends "5" and presses "Enter" again. This sequence replicates the actions of manually interacting with the telnet command window.
You will need to adapt this script to match the specific responses required by your telnet session.
Finally, the command window should be closed and the script terminated.
'Step 3 - Exit Command Window
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")WScript.Quit
</script>
</job>
Automating telnet involves these three straightforward steps within a simple script. Customize the three sections to suit your specific needs. This will enable you to automate tasks related to managing network switches, time clocks, or other remote systems that utilize telnet.
If you encounter repetitive tasks, streamline your workflow by creating automated Windows scripts to handle them. This will enhance your productivity and impress your superiors!
Are there other automation ideas you'd like to explore using Windows scripting? How have you been automating your own telnet processes? Share your insights and experiences in the comments below.
Image Credit: Shutterstock
Related Posts

Touchscreen on Windows PC: Do You Need It?

Find Lost Windows or Office Product Keys - Easy Guide

Windows 10 Resetting Settings: Why It Happens & How to Fix

Monitor FPS in UWP Games on Windows 10 - A Simple Guide
Remove 'Get Windows 10' Icon & Stop Upgrade Notifications
