ssh

package module
v0.7.7 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2020 License: MIT, Apache-2.0 Imports: 28 Imported by: 0

README

Origin README see here

This ssh package contains helpers for working with ssh in go. The client.go file is a modified version of docker/machine/libmachine/ssh/client.go that only uses golang's native ssh client. It has also been improved to resize the tty as needed. The key functions are meant to be used by either client or server and will generate/store keys if not found.

Usage:

package main

import (
	"fmt"

	"github.com/nanobox-io/golang-ssh"
)

func main() {
	err := connect()
	if err != nil {
		fmt.Printf("Failed to connect - %s\n", err)
	}
}

func connect() error {
    client, err := ssh.NewNativeClient("user", "localhost", "SSH-2.0-MyCustomClient-1.0", 2222, nil, ssh.AuthPassword("pass"))
	if err != nil {
		return fmt.Errorf("Failed to create new client - %s", err)
	}

	err = client.Shell()
	if err != nil && err.Error() != "exit status 255" {
		return fmt.Errorf("Failed to request shell - %s", err)
	}

	return nil
}

Compile for Windows:

If you get this error:

go: github.com/Sirupsen/logrus@v1.2.0: parsing go.mod: unexpected module path "github.com/sirupsen/logrus"

when compile for Windows with go mod, see this issue for some hints and there was a walkaround:

go mod init
go get github.com/docker/docker@v0.0.0-20180422163414-57142e89befe
GOOS=windows GOARCH=amd64 go build

Documentation

Overview

Package ssh is a helper for working with ssh in go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthCert

func AuthCert(keyReader, certReader io.Reader) (ssh.AuthMethod, error)

AuthCert creates an AuthMethod for SSH certificate authentication from the key and certificate bytes.

func AuthCertFile

func AuthCertFile(keyFilename, certFilename string) (ssh.AuthMethod, error)

AuthCertFile creates an AuthMethod for SSH certificate authentication from the key and certicate files.

func AuthKey

func AuthKey(r io.Reader) (ssh.AuthMethod, error)

AuthKey creates an AuthMethod for SSH key authentication.

func AuthKeyFile

func AuthKeyFile(keyFilename string) (ssh.AuthMethod, error)

AuthKey creates an AuthMethod for SSH key authentication from a key file.

func AuthKeyboardInteractive added in v0.7.3

func AuthKeyboardInteractive(challenge ssh.KeyboardInteractiveChallenge) ssh.AuthMethod

AuthKeyboardInteractive creates an AuthMethod for keyboard-interactive authentication.

func AuthPassword

func AuthPassword(password string) ssh.AuthMethod

AuthPassword creates an AuthMethod for password authentication.

func Dial added in v0.6.2

func Dial(ctx context.Context, config Config) (*ssh.Client, error)

func GenKeyPair

func GenKeyPair() (string, string, error)

GenKeyPair make a pair of public and private keys for SSH access. Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file. Private Key generated is PEM encoded

func MakeHostKeyCallback added in v0.6.0

func MakeHostKeyCallback(insecure bool, l Logger) (ssh.HostKeyCallback, error)

func Output added in v0.5.0

func Output(ctx context.Context, config Config, command string, stdout, stderr io.Writer) error

Output returns the output of the command run on the remote host.

func OutputWithPty added in v0.5.0

func OutputWithPty(ctx context.Context, config Config, command string, stdout, stderr io.Writer) error

Output returns the output of the command run on the remote host as well as a pty.

func ParseCertificate

func ParseCertificate(cert []byte) (*ssh.Certificate, error)

func SFTP added in v0.6.1

func SFTP(ctx context.Context, cfg Config) (*sftp.Client, error)

func Shell added in v0.5.0

func Shell(ctx context.Context, config Config, stdin io.Reader, stdout, stderr io.Writer, args ...string) error

Shell requests a shell from the remote. If an arg is passed, it tries to exec them on the server.

Types

type Client

type Client struct {
	Cfg     Config
	Session *ssh.Session
	Conn    *ssh.Client
	Stdin   io.WriteCloser
	Stdout  io.Reader
	Stderr  io.Reader
	sync.Mutex
	// contains filtered or unexported fields
}

func StartCommand added in v0.6.0

func StartCommand(ctx context.Context, cfg Config, command string) (*Client, error)

StartCommand starts the specified command without waiting for it to finish. You have to call the Wait function for that.

func (*Client) Wait

func (client *Client) Wait() (err error)

Wait waits for the command started by the Start function to exit. The returned error follows the same logic as in the exec.Cmd.Wait function.

type Config

type Config struct {
	User          string              // username to connect as, required
	Host          string              // hostname to connect to, required
	ClientVersion string              // ssh client version, "SSH-2.0-Go" by default
	Port          int                 // port to connect to, 22 by default
	Auth          []ssh.AuthMethod    // authentication methods to use
	HostKey       ssh.HostKeyCallback // callback for verifying server keys, ssh.InsecureIgnoreHostKey by default
	HTTPProxy     *url.URL
}

func (Config) GetAddr added in v0.6.0

func (cfg Config) GetAddr() string

func (Config) GetHostKeyCallback added in v0.6.0

func (cfg Config) GetHostKeyCallback() ssh.HostKeyCallback

func (Config) GetPort added in v0.6.0

func (cfg Config) GetPort() int

func (Config) ToNatives added in v0.6.3

func (cfg Config) ToNatives() []*ssh.ClientConfig

func (Config) Version

func (cfg Config) Version() string

type ExitError

type ExitError struct {
	Err      error
	ExitCode int
}

ExitError is a convenience wrapper for (crypto/ssh).ExitError type.

func (*ExitError) Cause

func (err *ExitError) Cause() error

Cause implements errors.Causer interface.

func (*ExitError) Error

func (err *ExitError) Error() string

Error implements error interface.

type HTTPConnectProxy added in v0.7.0

type HTTPConnectProxy struct {
	HTTPS    bool
	Host     string
	HaveAuth bool
	Username string
	Password string
}

func NewHTTPConnectProxy added in v0.7.0

func NewHTTPConnectProxy(uri *url.URL) *HTTPConnectProxy

func (*HTTPConnectProxy) DialContext added in v0.7.0

func (proxy *HTTPConnectProxy) DialContext(ctx context.Context, network, addr string) (c net.Conn, err error)

type Logger added in v0.6.0

type Logger interface {
	Debugw(msg string, kv ...interface{})
}

Jump to

Keyboard shortcuts

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