How to use Excel VBA Telnet To Autorun Commands?

Excel VBA Telnet Client for Remote Connect

We use Telnet Client sessions to control or manage systems that does not have a keyboard or monitor attached to it.

A simple example is the Internet Broadband Router at our home. Similar to that there are lot of network routers, bridges and servers which has to be connected through a Telnet session and operate them.

Network Technicians connect to these devices from remote computer and resolve if there are any issues. Usually these servers will be Unix or Linux based and the commands that you can issue here are some of the basic Unix Commands.

Also Read: Make your Excel Talk

Telnet Commands from Command Prompt

If you have a broadband router at your home, you can try to connect to it with below steps.

  1. Open a Command Prompt.
  2. Type ‘Telnet 192.168.1.1’ or ‘Telnet 10.0.0.1’
  3. Login with user id ‘admin’ and password as ‘password’ or ‘admin’
  4. Type ‘ps’. This command will show the running processes in the router.
  5. Type ‘help’ to get a list of supported commands.

Also Read: Login to Facebook, Twitter, Google+ in IE from Excel

How to VBA Telnet from Excel?

When a set of Telnet commands need to be executed on a remote device or server through telnet client session, then this Excel VBA macro code can be used to automate the telnet session.

Download a Sample Excel VBA Telnet Session Downloaded 3,568 Times

This code tried to connect to Telnet server with IP 192.168.1.1. Replace this Server IP address with your Server details and other login credentials. Winsock component will be used to execute this Telnet session.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'          Visit our website for more Tips and Tricks                      '
'                ---- Officetricks.com ----                                '
'                                                                          '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Start_Telnet_Session()
    'Declare Variables
    Dim Data As String
    Dim Winsock1
    
    'Server IP, Port & Login Credentials
    TelnetServer = ThisWorkbook.Sheets(1).Cells(1, 1)
    TelnetPort = ThisWorkbook.Sheets(1).Cells(2, 1)
    TelnetUser = ThisWorkbook.Sheets(1).Cells(3, 1)
    TelnetPassword = ThisWorkbook.Sheets(1).Cells(4, 1)
    TelnetCommands = ThisWorkbook.Sheets(1).Cells(5, 1)
    
    'Create WinSock Object & Connect to Server
    Set Winsock1 = New MSWinsockLib.Winsock
    Winsock1.RemoteHost = TelnetServer
    Winsock1.RemotePort = TelnetPort
    Winsock1.Connect
    
    'Wait Till connection is Success
    Do Until Winsock1.State = 7
        DoEvents
        If Winsock1.State = sckError Then
            Exit Sub
        End If
    Loop
    
    'Send UserName
    Winsock1.SendData TelnetUser & vbCrLf
        DoEvents: Application.Wait (Now + TimeValue("0:00:05"))
    
    'Send Password if you Server Requires one
    Winsock1.SendData TelnetPassword & vbCrLf
        DoEvents: Application.Wait (Now + TimeValue("0:00:05"))
        
    'Send Commands One by one & Get response from Server
    i = 5
    While TelnetCommands <> ""
        DoEvents: Application.Wait (Now + TimeValue("0:00:01"))

        Winsock1.SendData TelnetCommands & vbCrLf
        DoEvents: Application.Wait (Now + TimeValue("0:00:05"))
        'Get Response
        Winsock1.GetData Data
        Data = VBA.Replace(Data, vbLf, "")
        Data = VBA.Replace(Data, vbCr, vbNewLine)
        MsgBox Data

        i = i + 1
        TelnetCommands = ThisWorkbook.Sheets(1).Cells(i, 1)
    Wend
    
    MsgBox "Process Completed - Closing Telnet Connection"
    Winsock1.Close
End Sub

The above code waits for certain time interval (1 second to 5 Seconds) for different operations. Based on the speed of your server, modify this ‘wait time’.

This code, not only issue telnet commands to server, but also read data or messages returned by Telnet server or device. This way we can add more automation option by processing the return message and perform different operations accordingly.

More Tips: How to get Folders List and Size

10 thoughts on “How to use Excel VBA Telnet To Autorun Commands?”

        1. #1: I guess it is problem with MSWinsock.ocx installation. After copying the ocx file did you try to register it? please check whether OCX is working fine with any other applications.
          #2: Is this Code working fine with Windows 7?

Leave a Reply