More .NET components

Connecting

Connecting to SSH servers #

To connect to an SSH server running on default port 22, use Connect method. Once connected, don't forget to check the server's fingerprint as a security measure.

CSharp

// create SSH client instance
var ssh = new Rebex.Net.Ssh();

// connect to a server
ssh.Connect(hostname);

// check ssh.Fingerprint here

VisualBasic

' create SSH client instance
Dim ssh = New Rebex.Net.Ssh()

' connect to a server
ssh.Connect(hostname)

' check ssh.Fingerprint here

Note: To connect to a server running on non-standard port, just pass the port number to the Connect method as an additional argument.

Setting SSH connection options #

Various connection options are available through Ssh.Settings object. Set these before connecting to a server if needed.

Tip: For more information about SSH encryption algorithms, see Security.
For information about proxy servers, see Proxies and custom sockets.

CSharp

// create SSH client instance
var ssh = new Rebex.Net.Ssh();

// only allow specific algorithms
ssh.Settings.SshParameters.HostKeyAlgorithms = SshHostKeyAlgorithm.RSA;
ssh.Settings.SshParameters.EncryptionAlgorithms = SshEncryptionAlgorithm.AES;

// enable ZLIB compression
ssh.Settings.SshParameters.Compression = true;

// connect to a server
ssh.Connect(hostname);

VisualBasic

' create SSH client instance
Dim ssh = New Rebex.Net.Ssh()

' only allow specific algorithms
ssh.Settings.SshParameters.HostKeyAlgorithms = SshHostKeyAlgorithm.RSA
ssh.Settings.SshParameters.EncryptionAlgorithms = SshEncryptionAlgorithm.AES

' enable ZLIB compression
ssh.Settings.SshParameters.Compression = True

' connect to a server
ssh.Connect(hostname)

Getting info about an SSH connection #

Once connected (or authenticated), you can get useful information about the current session such as server/user name, server fingerprint or negotiated algorithms. Don't be afraid - user's password or private key are not stored with the session.

Tip: Lot of information about the current session is accessible through Ssh.Session object's properties.

CSharp

// create SSH client instance
var ssh = new Rebex.Net.Ssh();

// connect to a server
ssh.Connect(hostname);

// display info
Console.WriteLine("Connected to {0}:{1}", ssh.ServerName, ssh.ServerPort);
Console.WriteLine("Server: {0}", ssh.Session.ServerIdentification);
Console.WriteLine("Server host key: {0} ({1}-bit), Fingerprint: {2}",
    ssh.ServerKey.KeyAlgorithm,
    ssh.ServerKey.KeySize,
    ssh.ServerKey.Fingerprint);

// verify fingerprint
// ...

// authenticate
ssh.Login(username, password);

// display info to the user
Console.WriteLine("Logged on as '{0}'.", ssh.UserName);

VisualBasic

' create SSH client instance
Dim ssh = New Rebex.Net.Ssh()

' connect to a server
ssh.Connect(hostname)

' display info
Console.WriteLine("Connected to {0}:{1}", ssh.ServerName, ssh.ServerPort)
Console.WriteLine("Server: {0}", ssh.Session.ServerIdentification)
Console.WriteLine("Server host key: {0} ({1}-bit), Fingerprint: {2}",
    ssh.ServerKey.KeyAlgorithm,
    ssh.ServerKey.KeySize,
    ssh.ServerKey.Fingerprint)

' verify fingerprint
' ...

' authenticate
ssh.Login(username, password)

' display info to the user
Console.WriteLine("Logged on as '{0}'.", ssh.UserName)

Reusing SSH connection in multiple SSH/SFTP/SCP sessions #

SSH makes it possible to share one connection between multiple protocol sessions. Once you have a connected and authenticated an Ssh object, you can bind additional Sftp or Scp objects to its underlying SshSession using the Bind method. This will spawn multiple SFTP or SCP sessions, all using a single underlying SSH connection.

Note: You can bing additional Ssh objects as well, but this is seldom needed because even a single Ssh object makes it possible to run multiple commands and shell sessions at the same time.

CSharp

// connect using the an SSH client
Ssh ssh = new Rebex.Net.Ssh();
ssh.Connect(hostname);
ssh.Login(username, password);

// bind an SFTP client (Rebex SFTP) to the session
Sftp sftp = new Sftp();
sftp.Bind(ssh.Session);

// run commands using SSH client
ssh.RunCommand("ls -lR > listing.txt");

// transfer files using SFTP client
sftp.GetFile("listing.txt", @"C:\MyData\listing.txt");

VisualBasic

' connect using the an SSH client
Dim ssh As Ssh = New Rebex.Net.Ssh()
ssh.Connect(hostname)
ssh.Login(username, password)

' bind an SFTP client (Rebex SFTP) to the session
Dim sftp As New Sftp()
sftp.Bind(ssh.Session)

' run commands using SSH client
ssh.RunCommand("ls -lR > listing.txt")

' transfer files using SFTP client
sftp.GetFile("listing.txt", "C:\MyData\listing.txt")

Connecting to Telnet servers #

Unlike most Rebex components, the Telnet class has no Connect, Login or Disconnect methods. A new connection is made each time a new instance of Scripting, Shell or VirtualTerminal object is requested or when the Bind method of the TerminalControl or VirtualTerminal is called.

Why? Unlike most other protocols, authentication is not a part of the Telnet protocol and there are no general-purpose commands to be called for a telnet session to achieve it.

To authenticate to a telnet server, you need to know whether the server you are connecting to requires any authentication and how it is performed. It is then up to you to authenticate properly - check out Telnet authentication for details.

Bind a TerminalControl or VirtualTerminal object to telnet session:

CSharp

// create an instance of telnet client
telnet = new Telnet(hostname);

// bind the TerminalControl object (that was added
// to the Form using Visual Studio designer)
// or VirtualTerminal object to the
// telnet channel (creates a telnet shell session)
this.Terminal.Bind(telnet);

VisualBasic

' create the telnet client
telnet = New Telnet(hostname)

' bind the TerminalControl object (that was added
' to the Form using Visual Studio designer)
' or VirtualTerminal object to the
' telnet channel (creates a telnet shell session)
Me.Terminal.Bind(telnet)

Start scripting the telnet session:

CSharp

// create an instance of telnet client
Telnet client = new Telnet(hostname);

// connect and get a scripting object
Scripting scripting = client.StartScripting();

// authenticate, send command and read responses
// ...

// close the telnet session
scripting.Close();

VisualBasic

' create an instance of telnet client
Dim client As New Telnet(hostname)

' connect and get a scripting object
Dim scripting As Scripting = client.StartScripting()

' authenticate, send command and read responses
' ...

' close the telnet session
scripting.Close()

Tip: Check out Telnet authentication for an actual authentication sample.

Connecting to Telnet/SSL servers #

To connect to a telnet server that supports TLS/SSL encryption, specify the desired SslMode when calling the Connect method:

CSharp

// create an instance of telnet client (explicit TLS/SSL)
var client = new Telnet(hostname, SslMode.Explicit);

// create an instance of telnet client (implicit TLS/SSL)
//var client = new Telnet(hostname, SslMode.Implicit);

// connect and get a scripting object
Scripting scripting = client.StartScripting();

VisualBasic

' create an instance of telnet client (explicit TLS/SSL)
Dim client As New Telnet(hostname, SslMode.Explicit)

' create an instance of telnet client (implicit TLS/SSL)
'Dim client As New Telnet(hostname, SslMode.Implicit)

' connect and get a scripting object
Dim scripting As Scripting = client.StartScripting()

Tip: For more information about SslMode, see differences between Explicit and Implicit modes.

Server certificate is validated automatically by Rebex Telnet, but you can customize the validation process as well.

Tip: For advanced security options, see TLS/SSL core section.

The Telnet object can also be used to communicate with servers implementing different protocols secured by implicit TLS/SSL. HTTPS is one such example:

CSharp

// create an instance of telnet client (implicit TLS/SSL)
var client = new Telnet("test.rebex.net", 443, SslMode.Implicit);

// prepare options necessary for HTTP access
var options = new TerminalOptions();
options.LocalEcho = true;
options.NewLineSequence = NewLineSequence.CRLF;

// connect and get a scripting object
Scripting scripting = client.StartScripting(options);

// send HTTP request to get a web page
scripting.Send("GET / HTTP/1.1\n");
scripting.Send("Host: test.rebex.net\n");
scripting.Send("Connection: close\n");
scripting.Send("\n");

// read the response
string response = scripting.ReadUntil(ScriptEvent.Closed);

// close the session
scripting.Close();

VisualBasic

' create an instance of telnet client (implicit TLS/SSL)
Dim client As New Telnet("test.rebex.net", 443, SslMode.Implicit)

' prepare options necessary for HTTP access
Dim options As New TerminalOptions()
options.LocalEcho = True
options.NewLineSequence = NewLineSequence.CRLF

' connect and get a scripting object
Dim scripting As Scripting = client.StartScripting(options)

' send HTTP request to get a web page
scripting.Send("GET / HTTP/1.1" & vbLf)
scripting.Send("Host: test.rebex.net" & vbLf)
scripting.Send("Connection: close" & vbLf)
scripting.Send(vbLf)

' read the response
Dim response As String = scripting.ReadUntil(ScriptEvent.Closed)

' close the session
scripting.Close()

Connecting via serial port #

To communicate with a terminal-capable device connected using a RS-232 serial cable, use SerialPortChannel class. Unlike other classes, it has no Connect, Login or Disconnect methods.

To authenticate to a device connected over a serial port, you need to know whether it requires any authentication and how it is performed. It is then up to you to authenticate properly - check out Serial port authentication for details.

Bind a TerminalControl or VirtualTerminal object to a serial port:

CSharp

// create an instance of Serial Port communication channel
SerialPortChannel serialPort = new SerialPortChannel("COM1");

// bind the TerminalControl object (that was added
// to the Form using Visual Studio designer)
// or VirtualTerminal object to the
// Serial Port channel (creates a SerialPort channel)
this.Terminal.Bind(serialPort);

VisualBasic

' create an instance of Serial Port communication channel
Dim serialPort As SerialPortChannel = New SerialPortChannel("COM1")

' bind the TerminalControl object (that was added
' to the Form using Visual Studio designer)
' or VirtualTerminal object to the
' Serial Port channel (creates a SerialPort channel)
Me.Terminal.Bind(serialPort)

Start scripting using serial port:

CSharp

// open serial port
SerialPortChannel client = new SerialPortChannel("COM1");

// connect and get a scripting object
Scripting scripting = client.StartScripting();

// authenticate, send command and read responses
// ...

// close the telnet session
scripting.Close();

VisualBasic

' open serial port
Dim client As SerialPortChannel = New SerialPortChannel("COM1")

' connect and get a scripting object
Dim scripting As Scripting = client.StartScripting()

' authenticate, send command and read responses
' ...

' close the telnet session
scripting.Close()

An instance of SerialPortChannel represents a serial port, not a session. When you simply close it and open it again, the terminal resumes where it previously ended.