A high-level async SSH library for Nim, providing SSH, SCP, and SFTP functionality. Built on top of libssh2.
- β¨ Asynchronous API using
async
/await
- π Secure SSH connections with password and key authentication
- π SCP file transfers (upload/download)
- π SFTP operations (file transfer, directory management)
- π οΈ Command execution on remote servers
- π Support for various authentication methods
- π Comprehensive error handling
- First, ensure you have libssh2 installed on your system:
# Ubuntu/Debian
apt-get install libssh2-1-dev
# macOS
brew install libssh2
# Windows (using vcpkg)
vcpkg install libssh2:x64-windows
- Install using Nimble:
nimble install ssh2
import asyncdispatch
import ssh2
proc main() {.async.} =
let ssh = newSSHClient()
try:
# Connect to remote server
await ssh.connect("example.com", "username", password = "password")
# Execute a command
let (output, exitCode) = await ssh.execute("ls -la")
echo "Command output: ", output
# Upload a file using SCP
let scp = initSCPClient(ssh)
await scp.uploadFile("local.txt", "/remote/path/file.txt")
# SFTP operations
var sftp = initSFTPClient(ssh)
defer: sftp.close()
# Create directory
sftp.mkdir("/remote/new_dir")
# List directory contents
let files = await sftp.dir("/remote/new_dir")
for file in files:
echo "File: ", file.name`
finally:
ssh.disconnect()
waitFor main()
The library provides three main components:
- Connect to remote servers
- Execute commands
- Handle authentication
- Upload files
- Download files
- Preserve file permissions
- File transfers (upload/download)
- Directory operations (create, remove, list)
- File management (rename, delete)
For detailed API documentation, see the documentation.
The library supports multiple authentication methods:
# Password authentication
await ssh.connect("host", "user", password = "pass")
# Private key authentication
await ssh.connect("host", "user",
privateKeyPath = "~/.ssh/id_rsa",
passphrase = "optional-passphrase"
)
# Agent authentication
await ssh.connect("host", "user", useAgent = true)
The library uses Nim's exception system for error handling:
try:
await ssh.connect("host", "user", password = "pass")
let (output, code) = await ssh.execute("command")
except SSHException as e:
echo "SSH error: ", e.msg
except IOError as e:
echo "IO error: ", e.msg
To run tests, you can use a Docker instance with sshd installed:
# Host: 127.0.0.1
# Port: 2222
# Username: root
# Password: root
docker run -d --name test_sshd -p 2222:22 rastasheep/ubuntu-sshd:16.04
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.