* Build the read-ahead mechanism into SftpFileStream This change unifies the SFTP download implementations that exist via DownloadFile and via SftpFileStream, by rewriting SftpFileStream to perform the same "read-aheads" as DownloadFile. This brings the performance of downloads via SftpFileStream in line with DownloadFile, such that the latter is now effectively SftpFileStream.CopyTo. It also brings the recently added DownloadFileAsync up to speed since that was implemented via SftpFileStream.CopyToAsync. The methodology is a mix of the previous one and that within OpenSSH: the first call to SftpFileStream.Read sends one read request to the server. The second sends two and when not interrupted by Write or similar, the number of in-flight read requests continues to scale up in this fashion. I have measured CopyTo to be 3-20x faster than before, depending on file size and server round-trip time. * Check CanSeek in ReadAllBytes * Squeeze out some performance
SSH.NET
SSH.NET is a Secure Shell (SSH-2) library for .NET, optimized for parallelism.
Key Features
- Execution of SSH commands using both synchronous and asynchronous methods
- SFTP functionality for both synchronous and asynchronous operations
- SCP functionality
- Remote, dynamic and local port forwarding
- Interactive shell/terminal implementation
- Authentication via public key, password and keyboard-interactive methods, including multi-factor
- Connection via SOCKS4, SOCKS5 or HTTP proxy
How to Use
Run a command
using (var client = new SshClient("sftp.foo.com", "guest", new PrivateKeyFile("path/to/my/key")))
{
client.Connect();
using SshCommand cmd = client.RunCommand("echo 'Hello World!'");
Console.WriteLine(cmd.Result); // "Hello World!\n"
}
Upload and list files using SFTP
using (var client = new SftpClient("sftp.foo.com", "guest", "pwd"))
{
client.Connect();
using (FileStream fs = File.OpenRead(@"C:\tmp\test-file.txt"))
{
client.UploadFile(fs, "/home/guest/test-file.txt");
}
foreach (ISftpFile file in client.ListDirectory("/home/guest/"))
{
Console.WriteLine($"{file.FullName} {file.LastWriteTime}");
}
}
Main Types
The main types provided by this library are:
- Renci.SshNet.SshClient
- Renci.SshNet.SftpClient
- Renci.SshNet.PrivateKeyFile
- Renci.SshNet.SshCommand
- Renci.SshNet.ForwardedPort
- Renci.SshNet.ShellStream
Additional Documentation
Encryption Methods
SSH.NET supports the following encryption methods:
- aes128-ctr
- aes192-ctr
- aes256-ctr
- aes128-gcm@openssh.com
- aes256-gcm@openssh.com
- chacha20-poly1305@openssh.com
- aes128-cbc
- aes192-cbc
- aes256-cbc
- 3des-cbc
Key Exchange Methods
SSH.NET supports the following key exchange methods:
- mlkem768x25519-sha256
- sntrup761x25519-sha512
- sntrup761x25519-sha512@openssh.com
- curve25519-sha256
- curve25519-sha256@libssh.org
- ecdh-sha2-nistp256
- ecdh-sha2-nistp384
- ecdh-sha2-nistp521
- diffie-hellman-group-exchange-sha256
- diffie-hellman-group-exchange-sha1
- diffie-hellman-group16-sha512
- diffie-hellman-group14-sha256
- diffie-hellman-group14-sha1
- diffie-hellman-group1-sha1
Public Key Authentication
SSH.NET supports the following private key formats:
- RSA in
- OpenSSL traditional PEM format ("BEGIN RSA PRIVATE KEY")
- OpenSSL PKCS#8 PEM format ("BEGIN PRIVATE KEY", "BEGIN ENCRYPTED PRIVATE KEY")
- ssh.com format ("BEGIN SSH2 ENCRYPTED PRIVATE KEY")
- OpenSSH key format ("BEGIN OPENSSH PRIVATE KEY")
- PuTTY private key format ("PuTTY-User-Key-File-2", "PuTTY-User-Key-File-3")
- ECDSA 256/384/521 in
- OpenSSL traditional PEM format ("BEGIN EC PRIVATE KEY")
- OpenSSL PKCS#8 PEM format ("BEGIN PRIVATE KEY", "BEGIN ENCRYPTED PRIVATE KEY")
- OpenSSH key format ("BEGIN OPENSSH PRIVATE KEY")
- PuTTY private key format ("PuTTY-User-Key-File-2", "PuTTY-User-Key-File-3")
- ED25519 in
- OpenSSL PKCS#8 PEM format ("BEGIN PRIVATE KEY", "BEGIN ENCRYPTED PRIVATE KEY")
- OpenSSH key format ("BEGIN OPENSSH PRIVATE KEY")
- PuTTY private key format ("PuTTY-User-Key-File-2", "PuTTY-User-Key-File-3")
Private keys in OpenSSL traditional PEM format can be encrypted using one of the following cipher methods:
- DES-EDE3-CBC
- DES-EDE3-CFB
- AES-128-CBC
- AES-192-CBC
- AES-256-CBC
Private keys in OpenSSL PKCS#8 PEM format can be encrypted using any cipher method BouncyCastle supports.
Private keys in ssh.com format can be encrypted using the following cipher method:
- 3des-cbc
Private keys in OpenSSH key format can be encrypted using one of the following cipher methods:
- 3des-cbc
- aes128-cbc
- aes192-cbc
- aes256-cbc
- aes128-ctr
- aes192-ctr
- aes256-ctr
- aes128-gcm@openssh.com
- aes256-gcm@openssh.com
- chacha20-poly1305@openssh.com
Private keys in PuTTY private key format can be encrypted using the following cipher method:
- aes256-cbc
Host Key Algorithms
SSH.NET supports the following host key algorithms:
- ssh-ed25519
- ecdsa-sha2-nistp256
- ecdsa-sha2-nistp384
- ecdsa-sha2-nistp521
- rsa-sha2-512
- rsa-sha2-256
- ssh-rsa
OpenSSH certificate authentication is supported for all of the above, e.g. ssh-ed25519-cert-v01@openssh.com.
Message Authentication Code
SSH.NET supports the following MAC algorithms:
- hmac-sha2-256
- hmac-sha2-512
- hmac-sha1
- hmac-sha2-256-etm@openssh.com
- hmac-sha2-512-etm@openssh.com
- hmac-sha1-etm@openssh.com
Compression
SSH.NET supports the following compression algorithms:
- none (default)
- zlib@openssh.com
Framework Support
SSH.NET supports the following target frameworks:
- .NETFramework 4.6.2 (and higher)
- .NET Standard 2.0
- .NET 8 (and higher)
Building the library
The library has no special requirements to build, other than an up-to-date .NET SDK. See also CONTRIBUTING.md.
Using Pre-Release NuGet Packages
Pre-release NuGet packages are published from the develop branch to the GitHub NuGet Registry.
In order to pull packages from the registry, create a Personal Access Token with the read:packages permissions. Then add a package source for SSH.NET:
dotnet nuget add source --name SSH.NET --username <username> --password <personalaccesstoken> https://nuget.pkg.github.com/sshnet/index.json
Note: you may have to add --store-password-in-clear-text on non-Windows platforms.
Then you can add the the package as described here.
Supporting SSH.NET
Do you or your company rely on SSH.NET in your projects? If you want to encourage us to keep on going and show us that you appreciate our work, please consider becoming a sponsor through GitHub Sponsors.