Rebex SFTP

SFTP and SCP client .NET library

Download 30-day free trial Buy from $349
More .NET libraries

Back to feature list...

Connecting

Connecting to SFTP/SSH servers 

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

SFTP is a file transfer protocol that runs over an SSH protocol session. Most modern SSH servers support SFTP.

// create SFTP client instance
var sftp = new Rebex.Net.Sftp();

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

// check sftp.Fingerprint here
' create SFTP client instance
Dim sftp = New Rebex.Net.Sftp

' connect to a server
sftp.Connect(hostname)

' check sftp.Fingerprint here

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

Setting connection options 

Various connection options are available through Sftp.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.
// create SFTP client instance
var sftp = new Rebex.Net.Sftp();

// use POSIX-style rename operation if available
sftp.Settings.UsePosixRename = true;

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

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

// connect to a server
sftp.Connect(hostname);
' create SFTP client instance
Dim sftp = New Rebex.Net.Sftp

' use POSIX-style rename operation if available
sftp.Settings.UsePosixRename = True

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

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

' connect to a server
sftp.Connect(hostname)

Getting info about existing connection 

Once connected (or authenticated), you can get useful information about the current session such as server/user name, server fingerprint, negotiated algorithms or SFTP protocol version. 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 Sftp.Session object's properties.
// create SFTP client instance
var sftp = new Rebex.Net.Sftp();

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

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

// verify fingerprint
// ...

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

// display info to the user
Console.WriteLine("Logged on as {0} (Protocol version: {1}).",
    sftp.UserName, sftp.ProtocolVersion);
' create SFTP client instance
Dim sftp = New Rebex.Net.Sftp()

' connect to a server
sftp.Connect(hostname)

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

' verify fingerprint
' ...

' authenticate
sftp.Login(username, password)

' display info to the user
Console.WriteLine("Logged on as {0} (Protocol version: {1}).",
    sftp.UserName, sftp.ProtocolVersion)

Reusing SSH connection in multiple SFTP/SCP/SSH sessions 

SSH makes it possible to share one connection between multiple protocol sessions. Once you have a connected and authenticated an Sftp 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.

// connect using the first SFTP client
Sftp sftp1 = new Rebex.Net.Sftp();
sftp1.Connect(hostname);
sftp1.Login(username, password);

// bind second SFTP client to the session
Sftp sftp2 = new Sftp();
sftp2.Bind(sftp1.Session);

// transfer files using both clients
sftp1.PutFile(@"C:\MyData\file1.txt", "file1.txt");
sftp2.PutFile(@"C:\MyData\file2.txt", "file2.txt");
' connect using the first SFTP client
Dim sftp1 = New Rebex.Net.Sftp()
sftp1.Connect(hostname)
sftp1.Login(username, password)

' bind second SFTP client to the session
Dim sftp2 = New Sftp()
sftp2.Bind(sftp1.Session)

' transfer files using both clients
sftp1.PutFile("C:\MyData\file1.txt", "file1.txt")
sftp2.PutFile("C:\MyData\file2.txt", "file2.txt")

You can even use one of the underlying SshSession object's methods to execute remote commands (if allowed by the server).

Tip: The underlying SSH session can be shared with a SSH Shell to run an SSH shell session.

Please note that it's not necessary to spawn multiple SFTP sessions to be able to transfer multiple files simultaneously - even a single Sftp object can do that.

Checking connection state 

To check whether a session is still alive, use GetConnectionState method. This will send a simple "read-only" command to the server to ensure the connection is still up and running.

// check connection state
if (!sftp.IsConnected)
{
    // notify the user and return
    return;
}
' check connection state
If Not sftp.IsConnected Then
    ' notify the user and return
    Return
End If

Back to feature list...