sshx

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2025 License: BSD-3-Clause Imports: 6 Imported by: 0

README

sshx

sshx is a go package that provides a client wrapping SSH and SFTP clients. It provides additional surface methods Run, Upload, and, Download to simplify common use cases.

Additionally it provides an option for setting up a server using a password as the first time login, adding signer keys along with any additional keys for future method calls and logins.

Please note that while this package provides commonly used methods, you can always use the SSH or SFTP clients directly for more fine grained control. (e.g. Modifying file permissions)

Example

package main

import (
    "github.com/pedramktb/go-sshx"
    "golang.org/x/crypto/ssh"
    // other imports...
)

func main() {
    // ...

    _, err = sshx.NewClient(
        addr, // host(:port) e.g. 127.0.0.1:1234 or github.com
        "username", // e.g. root
        sshx.WithPassword("password"),
        sshx.WithSigner(signer), // ssh private key of type ssh.Signer
        sshx.WithFirstTimeSetup(myBestFriendKey, myNotSoBestFriendKey), // ssh public keys of type ssh.PublicKey
        // since password is set, this will add the signer's public key as well as your friends keys to the server and use that for future method calls
        // (if you don't use a password it is assumed the signer was already set up given there needs to be at least one authentication method set)
    )
    if err != nil {
        // ...
    }

    // ...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAuthMethod = errors.New("at least one auth method (signer or password) must be provided")

Functions

This section is empty.

Types

type Client

type Client struct {
	SSHClient  *ssh.Client
	SFTPClient *sftp.Client
}

sshx Client is a wrapper that includes an SSH client and an SFTP client. It also provides simplified Run, Upload and Download methods.

func NewClient

func NewClient(addr string, username string, options ...ClientOption) (*Client, error)

NewClient creates a new sshx Client with the given IP, username, and options. At least one of the signer or password must be set.

func (*Client) Close

func (c *Client) Close() error

Close closes the SSH and SFTP clients.

func (*Client) Download

func (c *Client) Download(dst io.Writer, remotePath string) error

Download downloads a file from the remote server to the dst writer.

func (*Client) Run

func (c *Client) Run(cmd string) (string, error)

Run runs the given command on the remote server in a new session.

func (*Client) Upload

func (c *Client) Upload(src io.Reader, remotePath string, append bool) error

Upload uploads the src reader to the remote path on the server.

type ClientOption

type ClientOption func(*clientOptions)

func WithFirstTimeSetup

func WithFirstTimeSetup(additionalKeys ...ssh.PublicKey) ClientOption

WithFirstTimeSetup uses the password (if set) to authenticate and the signer's public key along with any additional SSH keys. After this, the password is removed and the signer is used to authenticate. If the password is not set, the signer is used to authenticate and only the additional SSH keys are added to the server. Using this method multiple times will result in duplicate keys on the server.

func WithNetwork

func WithNetwork(network string) ClientOption

WithNetwork sets the network to use for SSH connections. Defaults to "tcp".

func WithPassword

func WithPassword(password string) ClientOption

WithPassword sets the password on the sshxClient. Please consider using WithSigner instead. If you are using the password for a first time setup, use WithFirstTimeSetup as well.

func WithSigner

func WithSigner(signer ssh.Signer) ClientOption

WithSigner sets the SSH key on the sshxClient.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL