Click here to Skip to main content
Click here to Skip to main content

Establishing Telnet and SSH Connections with Remote UNIX Terminals

, 11 Jan 2014
Rate this:
Please Sign up or sign in to vote.
This tip demonstrates how to create an application that establishes a Telnet or SSH connection with, executes commands on, and receives responses from, a remote UNIX terminal.

Overview

This tip demonstrates how to create an application that establishes a Telnet or SSH connection with, executes commands on, and receives responses from, a remote UNIX terminal.

Contents

  • Introduction
  • Getting Plink
  • Creating a Connection
  • Sending Commands
  • Reading Responses
  • Closing Connections
  • Conclusion

Introduction

Having been involved in the development of software applications since 2007, I have frequently noted the same questions being raised concerning establishment of connections with remote UNIX terminals. In the hope of answering these questions once and for all, this tip demonstrates how to establish a Telnet or SSH connection with a UNIX terminal using Plink, an open-source, network connection tool. It then outlines how to execute commands on, and receive responses from, such a terminal. While the tip employs easy-to-use code which should be readily understandable to readers with an intermediate level of programming experience, a familiarity with the creation of new Windows processes, and the StreamWriter and StreamReader classes, would also be beneficial.

Getting Plink

Click here to download a copy of Plink from the official Plink website.

Creating a Connection

Establishing a connection to a remote UNIX terminal requires the creation of a new Plink process. In the following code, this new process is defined as “conProcess”.

'Create Process
 Dim conProcess As New Process

Having defined conProcess, the next step is to set a number of variables for conProcess to use on start-up. The defined properties are illustrated in the following code. A comment in each line explains what each property seeks to achieve.

Dim conInfo As New System.Diagnostics.ProcessStartInfo()

conInfo.FileName = "C:\Data\plink.exe" 'Plinks location
conInfo.RedirectStandardInput = True 'Set to true so commands can be passed to Plink.
conInfo.RedirectStandardOutput = True 'Set to true so data can be read from Plink.
conInfo.UseShellExecute = False 'Must be set to false to redirect input/output.
conInfo.CreateNoWindow = True 'Start the process in a new window.
conInfo.WindowStyle = ProcessWindowStyle.Hidden 'Hide the new window.

conProcess.StartInfo = conInfo

As the arguments string for conProcess contains our connection information, it is important to provide a brief outline of how a plink connection string is structured. The following diagram details two basic plink connection strings, the first for a SSH connection and the second a telnet connection.

In the sample application accompanying this tip, the user is prompted to provide the connection type, terminal name, username, and password values for the process to use. In providing these inputs, the arguments string, and the code used to gather the variables needed for the arguments string should resemble the following:

'Telnet or SSH ?
Dim strConn As String
Select Case comConType.SelectedIndex
    Case 0
        strConn = "-ssh"
    Case 1
        strConn = "-telnet"
    Case Else
        MsgBox("Choose a connection type", MsgBoxStyle.OkOnly, "Oh dear!")
    End Select

'Prompt For Connection Details
Dim strServ As String = InputBox("Enter A Server Name", "Server Login")
Dim strUser As String = InputBox("Enter Your Username", "Server Login")
Dim strPass As String = InputBox("Enter Your Password", "Server Login")

conInfo.Arguments = strConn & " " & strServ & " -l " & strUser & " -pw " & strPass

The final step is to initiate the process. By starting the process, the application will take all of the defined variables, pass them to Plink, which will establish a connection with the remote terminal.

conProcess.Start()

Sending Commands

Once conProcess has been started and a connection has been established, it is possible to pass commands to the remote terminal using the StreamWriter class. The code illustrates the passing of a command to Plink for execution on the remote terminal.

'Execute Commands
Dim Writer As System.IO.StreamWriter = conProcess.StandardInput
Writer.WriteLine("ifconfig")
Writer.WriteLine("exit")
Writer.Close()

Reading Responses

To read responses from Plink, we use the StreamReader class. The following code starts by reading the output of our process. The outputted data is then appended to a textbox.

'Read Response
Dim Reader As System.IO.StreamReader = conProcess.StandardOutput
txtOutput.AppendText(Reader.ReadToEnd())
Reader.Close()

Closing Connections

To close a connection to the remote terminal, simply close the process.

'Close Process & Writer
conProcess.Close()

Conclusion

Once you have put all of this code together, it should look something like this:

Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click

    'Telnet or SSH ?
    Dim strConn As String
    Select Case comConType.SelectedIndex
        Case 0
            strConn = "-ssh"
        Case 1
            strConn = "-telnet"
        Case Else
            MsgBox("Choose a connection type", MsgBoxStyle.OkOnly, "Oh dear!")
        End Select

    'Prompt For Connection Details
    Dim strServ As String = InputBox("Enter A Server Name", "Server Login")
    Dim strUser As String = InputBox("Enter Your Username", "Server Login")
    Dim strPass As String = InputBox("Enter Your Password", "Server Login")

    Try

        'Create Process
        Dim conProcess As New Process
        Dim conInfo As New System.Diagnostics.ProcessStartInfo()

        conInfo.FileName = "C:\Data\plink.exe"
        conInfo.Arguments = strConn & " " & strServ & _
        " -l " & strUser & " -pw " & strPass
        conInfo.RedirectStandardInput = True
        conInfo.RedirectStandardOutput = True
        conInfo.UseShellExecute = False
        conInfo.CreateNoWindow = True
        conInfo.WindowStyle = ProcessWindowStyle.Hidden

        conProcess.StartInfo = conInfo
        conProcess.Start()

        'Execute Commands
        Dim Writer As System.IO.StreamWriter = conProcess.StandardInput
        Writer.WriteLine("ifconfig")
        Writer.WriteLine("exit")
        Writer.Close()

        'Read Response
        Dim Reader As System.IO.StreamReader = conProcess.StandardOutput
        txtOutput.AppendText(Reader.ReadToEnd())
        Reader.Close()

        'Disconnect
        conProcess.Close()

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Luke Millanta

Australia Australia
No Biography provided
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 12 Jan 2014
Article Copyright 2014 by Luke Millanta
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid