modulepackage
0.0.0-20250211050843-7494b641bf16
Repository: https://github.com/vts0/sshclient.git
Documentation: pkg.go.dev
# README
SSHClient
SSHClient is a Go package that provides an easy-to-use interface for executing commands on a remote server via SSH and transferring files using SFTP.
Features
- Execute remote commands via SSH
- Upload and download files via SFTP
- Support for both password and SSH key authentication
- Configurable timeout settings
- Secure host key verification
Installation
go get github.com/vts0/sshclient
Usage
Password Authentication
client, err := sshclient.NewClient("example.com", "user",
sshclient.WithAuthType(sshclient.PasswordAuth),
sshclient.WithPassword("securepassword"),
sshclient.WithPort(22),
sshclient.WithTimeout(30*time.Second),
sshclient.WithUseSFTP(true),
)
if err != nil {
log.Fatalf("Failed to connect: %v", err)
}
defer client.Close()
Key-Based Authentication
client, err := sshclient.NewClient("example.com", "username",
sshclient.WithAuthType(sshclient.KeyAuth),
sshclient.WithKeyPath("/home/user/.ssh/id_rsa"),
sshclient.WithPassphrase("your_passphrase"),
)
if err != nil {
log.Fatalf("Failed to connect: %v", err)
}
defer client.Close()
Executing Commands
output, err := client.Execute("ls -l")
if err != nil {
log.Printf("Error executing command: %v", err)
}
fmt.Println("Command output:", output)
File Transfer with SFTP
Uploading a File
err := client.UploadFile(context.Background(), "local.txt", "/remote/path.txt")
if err != nil {
log.Printf("Upload failed: %v", err)
}
Downloading a File
err := client.DownloadFile(context.Background(), "/remote/file.txt", "local.txt")
if err != nil {
log.Printf("Download failed: %v", err)
}
License
This project is licensed under the MIT License.
# Functions
NewClient creates a new instance of Client with given host, user, and options.
WithAuthType sets the authentication method for the SSH connection.
WithKeyPath sets the path to the private key for key-based authentication.
WithPassphrase sets the passphrase for encrypted private keys.
WithPassword sets the password for password-based authentication.
WithPort sets the port number for the SSH connection.
WithTimeout sets the timeout duration for the SSH connection.
WithUseSFTP enables or disables SFTP support.
# Structs
Client represents a client for executing commands on a remote server via SSH.
ClientOptions contains parameters for creating a new client.
# Type aliases
AuthMethod defines the type of authentication.