Top Related Projects
The leading native Python SSHv2 protocol library.
Win32 port of OpenSSH
the SSH library
Portable OpenSSH
Quick Overview
SSH.NET is a Secure Shell (SSH) library for .NET, providing a comprehensive set of SSH functionalities. It enables developers to incorporate SSH capabilities into their .NET applications, supporting various SSH operations such as remote command execution, file transfers, and port forwarding.
Pros
- Cross-platform compatibility (Windows, Linux, macOS)
- Supports both SSH1 and SSH2 protocols
- Implements SFTP for secure file transfers
- Actively maintained with regular updates
Cons
- Limited documentation compared to some other SSH libraries
- Performance may be slower than native SSH implementations
- Some advanced SSH features might be missing or less developed
- Steeper learning curve for developers new to SSH concepts
Code Examples
- Connecting to an SSH server and executing a command:
using (var client = new SshClient("hostname", "username", "password"))
{
client.Connect();
var result = client.RunCommand("ls -l");
Console.WriteLine(result.Result);
client.Disconnect();
}
- Uploading a file using SFTP:
using (var sftp = new SftpClient("hostname", "username", "password"))
{
sftp.Connect();
using (var fileStream = new FileStream("localFile.txt", FileMode.Open))
{
sftp.UploadFile(fileStream, "/remote/path/remoteFile.txt");
}
sftp.Disconnect();
}
- Setting up port forwarding:
using (var client = new SshClient("hostname", "username", "password"))
{
client.Connect();
var forwardedPort = new ForwardedPortLocal("127.0.0.1", 8080, "remote.example.com", 80);
client.AddForwardedPort(forwardedPort);
forwardedPort.Start();
// Use the forwarded port...
forwardedPort.Stop();
client.Disconnect();
}
Getting Started
-
Install the SSH.NET NuGet package:
Install-Package SSH.NET
-
Add the necessary using statements to your code:
using Renci.SshNet;
-
Create an SSH client and connect:
var client = new SshClient("hostname", "username", "password"); client.Connect();
-
Perform SSH operations (e.g., run commands, transfer files)
-
Disconnect when finished:
client.Disconnect();
Competitor Comparisons
The leading native Python SSHv2 protocol library.
Pros of Paramiko
- Written in Python, offering seamless integration with Python projects
- Extensive documentation and large community support
- Supports both SSH1 and SSH2 protocols
Cons of Paramiko
- Performance may be slower compared to SSH.NET's C# implementation
- Limited cross-platform support (primarily for Python environments)
Code Comparison
Paramiko:
import paramiko
client = paramiko.SSHClient()
client.connect('hostname', username='user', password='pass')
stdin, stdout, stderr = client.exec_command('ls -l')
print(stdout.read().decode())
client.close()
SSH.NET:
using Renci.SshNet;
using (var client = new SshClient("hostname", "user", "pass"))
{
client.Connect();
var result = client.RunCommand("ls -l");
Console.WriteLine(result.Result);
client.Disconnect();
}
Both libraries provide similar functionality for establishing SSH connections and executing commands. Paramiko uses a more Pythonic approach, while SSH.NET follows C# conventions. The choice between them often depends on the primary programming language of the project and specific requirements for performance or cross-platform compatibility.
Win32 port of OpenSSH
Pros of Win32-OpenSSH
- Native Windows implementation of OpenSSH, providing better integration with Windows systems
- Supports more advanced OpenSSH features and protocols
- Actively maintained by Microsoft, ensuring regular updates and security patches
Cons of Win32-OpenSSH
- Primarily focused on Windows platforms, limiting cross-platform compatibility
- Requires more setup and configuration compared to SSH.NET
- May have a steeper learning curve for developers new to OpenSSH
Code Comparison
SSH.NET:
using (var client = new SshClient("host", "username", "password"))
{
client.Connect();
var result = client.RunCommand("ls -l");
Console.WriteLine(result.Result);
}
Win32-OpenSSH (PowerShell):
$session = New-PSSession -HostName "host" -UserName "username"
Invoke-Command -Session $session -ScriptBlock { Get-ChildItem | Format-Table }
Remove-PSSession $session
Both repositories offer SSH functionality, but SSH.NET provides a more straightforward .NET-centric approach, while Win32-OpenSSH offers a native Windows implementation with deeper system integration. SSH.NET may be easier for .NET developers to adopt, while Win32-OpenSSH provides more advanced features and better Windows compatibility at the cost of increased complexity.
the SSH library
Pros of libssh2
- Written in C, offering potential performance advantages
- Broader platform support, including embedded systems
- More mature project with a longer history
Cons of libssh2
- Requires more low-level programming knowledge
- Less user-friendly API compared to SSH.NET
- May require additional work for .NET integration
Code Comparison
SSH.NET (C#):
using (var client = new SshClient("host", "username", "password"))
{
client.Connect();
var result = client.RunCommand("ls -l");
Console.WriteLine(result.Result);
}
libssh2 (C):
#include <libssh2.h>
LIBSSH2_SESSION *session = libssh2_session_init();
libssh2_session_handshake(session, sock);
LIBSSH2_CHANNEL *channel = libssh2_channel_open_session(session);
libssh2_channel_exec(channel, "ls -l");
SSH.NET provides a more high-level, object-oriented approach, while libssh2 offers lower-level control but requires more manual management. SSH.NET is generally easier to use for .NET developers, while libssh2 offers broader platform support and potential performance benefits at the cost of increased complexity.
Portable OpenSSH
Pros of OpenSSH
- Industry-standard implementation with extensive features and security
- Cross-platform support for various Unix-like systems and Windows
- Regularly updated with security patches and new features
Cons of OpenSSH
- Primarily designed for command-line usage, less suitable for integration into .NET applications
- Requires external dependencies and system-level installation
- Steeper learning curve for developers unfamiliar with Unix-like systems
Code Comparison
OpenSSH (C):
#include <libssh/libssh.h>
ssh_session my_ssh_session;
int rc;
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
SSH.NET (C#):
using Renci.SshNet;
using (var client = new SshClient("host", "username", "password"))
{
client.Connect();
// Perform SSH operations
}
Key Differences
- OpenSSH is a complete SSH suite, while SSH.NET is a .NET library for SSH functionality
- SSH.NET offers easier integration for .NET developers, with a more object-oriented approach
- OpenSSH provides a wider range of features and is more suitable for system-level SSH implementations
- SSH.NET focuses on programmatic SSH access, making it ideal for .NET applications requiring SSH capabilities
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
SSH.NET
SSH.NET is a Secure Shell (SSH-2) library for .NET, optimized for parallelism.
Key Features
- Execution of SSH command 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 publickey, 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.ScpClient
- Renci.SshNet.PrivateKeyFile
- Renci.SshNet.SshCommand
- 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:
- 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 PEM ("BEGIN RSA PRIVATE KEY") and ssh.com ("BEGIN SSH2 ENCRYPTED PRIVATE KEY") format
- DSA in OpenSSL PEM ("BEGIN DSA PRIVATE KEY") and ssh.com ("BEGIN SSH2 ENCRYPTED PRIVATE KEY") format
- ECDSA 256/384/521 in OpenSSL PEM format ("BEGIN EC PRIVATE KEY")
- ECDSA 256/384/521, ED25519 and RSA in OpenSSH key format ("BEGIN OPENSSH PRIVATE KEY")
Private keys in OpenSSL PEM and ssh.com format can be encrypted using one of the following cipher methods:
- DES-EDE3-CBC
- DES-EDE3-CFB
- DES-CBC
- AES-128-CBC
- AES-192-CBC
- AES-256-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
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
- ssh-dss
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 and 2.1
- .NET 6 (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.
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.
Top Related Projects
The leading native Python SSHv2 protocol library.
Win32 port of OpenSSH
the SSH library
Portable OpenSSH
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot