How to Automate Telnet Commands Using VB Script

Ryan Dube 13-10-2011

automate telnetWriting batch jobs (.bat How to Create a Batch (BAT) File in Five Simple Steps This article covers how to create a batch file using five simple steps for the Windows operating system. Read More ) and more recently Windows scripts 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


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

<script language="VBScript">
Option Explicit
On Error Resume Next
Dim WshShell
set WshShell=CreateObject("WScript.Shell") "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}")

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

Related topics: Computer Automation, Programming.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

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

  1. Rakantoh
    July 27, 2017 at 5:18 am

    Hi. I have a question. This script in which format must i save it? .vbs, .wsf or .bat? Thanks.

  2. Gavin Edwards
    June 14, 2017 at 10:24 pm

    GREAT tutorial! I am using it to automatically restart 6 digital audio servers in a remote location. Is there any way the results could be output to a text file? I want to know if the reboot command successfully executed. I get verification if I am present (because I can see the terminal window), but I wanted to run this in the wee hours when I am not here. I set up Task Scheduler to execute it at 4 AM every morning. It would be nice if it printed to a text file that I can check once I arrive at the office.

  3. Doug
    June 11, 2017 at 2:18 pm

    Hola. Buen día

    Cómo saber si simplemente la sesión Telnet abrió correctamente?

    Existe algún comando o instrucción que informe si se conectó o hubo un error como lo hay con la instrucción ping?

    De antemano. Gracias

  4. Sukumar
    April 4, 2017 at 1:33 pm

    Hi Ryan,

    Could you please explain how can I parameterise the values or commands in the .wsf file you've mentioned in this article

  5. Atul
    December 26, 2016 at 10:47 am

    This is a beauty...I knew it was possible and yet, all this while I had webpages misguiding me to non-working DOS Batch scripts or superfluous keystroke macro recording software, while this little thing worked like a charm. Thanks!

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


    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. ?

  7. 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
    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") "cmd.exe"
    WScript.Sleep 1000
    WshShell.SendKeys "telnet"
    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 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

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

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

    Thanks! Works Awesome!

  10. Anonymous
    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!!

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

    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.

  12. 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.

  13. 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.

  14. 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?

  15. 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.

  16. 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.