Pinterest Stumbleupon Whatsapp
Ads by Google

automate telnetWriting batch jobs (.bat How to Write a Simple Batch (BAT) File How to Write a Simple Batch (BAT) File Batch files can automate everyday computer tasks. We'll show you how batch files work and teach you the basics. By the end of this article, you'll have written your first batch file. Read More ) and more recently Windows scripts 3 Awesome Things You Can Do With Windows Scripting 3 Awesome Things You Can Do With Windows Scripting Whether you work in the IT field, as a web designer, or if you are just a student or regular office worker, Windows Scripting has something to offer you. Of course Applescript does as well,... Read More (.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.

automate telnet

Ads by Google

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.

Now, there are other methods to do this. Just check out Abhigyan’s article on Tst10.exe How to Automate Multiple Rapidshare Downloads How to Automate Multiple Rapidshare Downloads Read More 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:

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

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

  1. Lars
    August 29, 2016 at 7:07 am

    Hi,

    Perfect post. If you have multiple switches you would like to connect to. Can you use an ini file instead of having to write the same code over and over again. ?

  2. sampal
    June 14, 2016 at 2:31 pm

    How to send the username and password as commands to telnet
    for ex I want to login to telnet with below credentials
    telnet x.x.x.x
    username
    password
    boot action=reset

    wrote the below scripts to automate these commands. when I run it displays "login:" thrice thats is. I am doing something wrong in the scripts?

    Option Explicit
    On Error Resume Next
    Dim WshShell
    set WshShell=CreateObject("WScript.Shell")
    WshShell.run "cmd.exe"
    WScript.Sleep 1000
    WshShell.SendKeys "telnet 172.22.32.112"
    WshShell.SendKeys ("{Enter}")
    WScript.Sleep 3000
    WshShell.SendKeys ("{root}")
    WshShell.SendKeys ("{Enter}")
    WScript.Sleep 3000
    WshShell.SendKeys ("{dbps}")
    WshShell.SendKeys ("{Enter}")
    WScript.Sleep 2000
    WshShell.SendKeys ("{boot action=reset}")
    WshShell.SendKeys ("{Enter}")
    WScript.Sleep 6000

    • ellr0m
      August 22, 2016 at 5:34 pm

      Too much brackets :D

      WshShell.SendKeys "telnet xxx.xxx.xxx.xxx 23"
      WshShell.SendKeys "{Enter}"
      WScript.Sleep 1000
      WshShell.SendKeys "root"
      WshShell.SendKeys "{Enter}"
      WScript.Sleep 1000
      WshShell.SendKeys "dbps"
      WshShell.SendKeys "{Enter}"
      WScript.Sleep 1000

  3. illz
    June 7, 2016 at 9:49 pm

    YEAH!!!! Awesome Tutorial.

    I was tasked with automate emergency shutdown in case I'm not here one of our helpdesk tech can do it.

    I wrote a batch script to shutdown all VMs
    wait 60 ping then shutdown hosts,
    Then needed to telnet into the SAN and shut it down safely.
    Your tutorial worked like a charm.

    Pretty cool watching the computer automate the whole telnet session.

    Thank you!!

  4. EddyT
    March 21, 2016 at 11:10 am

    Thanks! Works Awesome!

  5. Ciaran Mooney
    November 3, 2015 at 7:31 pm

    Is it possible to get user input during the telnet session and use this in the vb script?

    I am trying to let the user select a port number and the call OBJECT.SendKey 'LO PO' userInputed_PortNumber to reset the port

    Been at this all day so any feedback appreciated!!

  6. payam
    April 29, 2015 at 12:06 am

    Wow!
    This is really good stuff, thanks!
    I didn't know about vbscript capability to do such magic. I'm officially a fan now.
    I searched the web for and hour for "sending remote commands through telnet" but had no chance, then started to look for "telnet automation" an got to here.

    BTW, I had to add another WshShell.SendKeys ("{Enter}") before WScript.Quit , because the script stucked in "Connection to host lost." and wouldn't return to the cmd prompt.

  7. Carpet cleaning Bethesda
    January 11, 2012 at 7:09 am

    I am a programmer myself but never thought VB could come that far even automating Telnet.

  8. Carl Lim
    December 11, 2011 at 11:41 pm

    Yes agree to that. It's better to know VB scrip to easily edit the program used on telnet.

  9. Ryan Dube
    December 7, 2011 at 4:47 pm

    Hey Peter, you do need some understanding of the programming language, but it's not all that complicated is it?

  10. Ryan Dube
    December 7, 2011 at 4:46 pm

    Hey Ryan - I'm not familiar with it, but I'd be interested to know if it works as well.

  11. tom bonds
    November 24, 2011 at 6:19 am

    It's not easy to monitor your remote PCs but I hope with this technology we could automate the Telnet even when you're not on site. 

    • Carpet cleaning Virginia Beach
      January 8, 2012 at 8:40 am

      Ts10 can be used to automate telnet commands in Windows. Expect can be used to automate telnet commands in Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *