SSH Shell Tutorial

Applies to: Rebex Total Pack, Rebex SSH Pack, Rebex SSH Shell

About Rebex SSH shell 

Rebex SSH Shell library is an SSH shell, Telnet and ANSI terminal emulation library for .NET languages (such as C# or VB.NET). It makes it easy to execute commands on Unix/Windows SSH or telnet servers and add terminal emulation capabilities to your applications.

SSH is nearly ubiquitous on Unix and Unix-like systems, with OpenSSH being the most common implementation. There is also a growing number of SSH servers for Windows as well.

Most SSH servers also implement SFTP - a powerful secure file transfer protocol. To take advantage of it, use our SFTP library. An SFTP + SSH bundle is also available.

Namespaces and assemblies 

To use Rebex SSH Shell, you have to reference the Rebex.Common.dll, Rebex.Networking.dll, Rebex.Telnet.dll, Rebex.SshShell.dll and Rebex.Terminal.dll assemblies in your project. These contain Ssh, Shell, TerminalControl/SshTerminalControl, VirtualTerminal and other classes in Rebex.Net and Rebex.TerminalEmulation namespaces.

In your source files, import the following namespace:

using Rebex.Net;
using Rebex.TerminalEmulation;
Imports Rebex.Net
Imports Rebex.TerminalEmulation

Executing simple commands 

Executing simple commands using SSH is very easy - just connect to the server, authenticate and call RunCommand method.

using (var ssh = new Rebex.Net.Ssh())
{
    // connect and log in
    ssh.Connect(serverName);
    ssh.Login(username, password);

    // execute a simple command
    string response = ssh.RunCommand("echo Hello world!");

    // display the response
    Console.WriteLine(response);
}
Using ssh = New Rebex.Net.Ssh()
    ' connect and log in
    ssh.Connect(serverName)
    ssh.Login(username, password)

    ' execute a simple command
    Dim response As String = ssh.RunCommand("echo Hello world!")

    ' display the response
    Console.WriteLine(response)
End Using

Tip: RunCommand can only be used to execute commands that don't require any user input. To script advanced commands, use Scripting object instead.

Scripting complex commands 

To execute more commands using a single shell, or to execute commands that actually need some kind of user input, use the powerful Scripting object:

using (var ssh = new Rebex.Net.Ssh())
{
    // connect and log in
    ssh.Connect(serverName);
    ssh.Login(username, password);

    // start a scripting session
    Scripting scripting = ssh.StartScripting();

    // automatically detect remote prompt
    scripting.DetectPrompt();

    // execute command
    scripting.SendCommand("echo Hello world!");

    // read its response
    string response = scripting.ReadUntilPrompt();

    // execute more commands
    // ...

    // display the response
    Console.WriteLine(response);
}
Using ssh = New Rebex.Net.Ssh()
    ' connect and log in
    ssh.Connect(serverName)
    ssh.Login(username, password)

    ' start a scripting session
    Dim scripting As Scripting = ssh.StartScripting()

    ' automatically detect remote prompt
    scripting.DetectPrompt()

    ' execute command
    scripting.SendCommand("echo Hello world!")

    ' read its response
    Dim response As String = scripting.ReadUntilPrompt()

    ' execute more commands
    ' ...

    ' display the response
    Console.WriteLine(response)
End Using

Tip: Check out the list of Scripting features.

Tip: The Scripting object can be used to script TerminalControl or VirtualTerminal objects as well.

More sample code 

Back to tutorial list...