goph

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2022 License: MIT Imports: 15 Imported by: 0

README

goph

This is a fork of https://github.com/melbahja/goph

Except I have added a function that lets you separate stdout from stderr when running a remote command.

If https://github.com/melbahja/goph/pull/41 ever gets merged then this repo can go away and we can simply use the original goph again.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTimeout = 20 * time.Second

DefaultTimeout is the timeout of ssh client connection.

Functions

func AddKnownHost

func AddKnownHost(host string, remote net.Addr, key ssh.PublicKey, knownFile string) (err error)

AddKnownHost add a a host to known hosts file.

func CheckKnownHost

func CheckKnownHost(host string, remote net.Addr, key ssh.PublicKey, knownFile string) (found bool, err error)

CheckKnownHost checks is host in known hosts file. it returns is the host found in known_hosts file and error, if the host found in known_hosts file and error not nil that means public key mismatch, maybe MAN IN THE MIDDLE ATTACK! you should not handshake.

func DefaultKnownHosts

func DefaultKnownHosts() (ssh.HostKeyCallback, error)

DefaultKnownHosts returns host key callback from default known hosts path, and error if any.

func DefaultKnownHostsPath

func DefaultKnownHostsPath() (string, error)

DefaultKnownHostsPath returns default user knows hosts file.

func Dial

func Dial(proto string, c *Config) (*ssh.Client, error)

Dial starts a client connection to SSH server based on config.

func GetSigner

func GetSigner(prvFile string, passphrase string) (ssh.Signer, error)

GetSigner returns ssh signer from private key file.

func HasAgent

func HasAgent() bool

HasAgent checks if ssh agent exists.

func KnownHosts

func KnownHosts(file string) (ssh.HostKeyCallback, error)

KnownHosts returns host key callback from a custom known hosts path.

Types

type Auth

type Auth []ssh.AuthMethod

Auth represents ssh auth methods.

func Key

func Key(prvFile string, passphrase string) (Auth, error)

Key returns auth method from private key with or without passphrase.

func Password

func Password(pass string) Auth

Password returns password auth method.

func UseAgent

func UseAgent() (Auth, error)

UseAgent auth via ssh agent, (Unix systems only)

type Client

type Client struct {
	*ssh.Client
	Config *Config
}

Client represents Goph client.

func New

func New(user string, addr string, auth Auth) (c *Client, err error)

New starts a new ssh connection, the host public key must be in known hosts.

func NewConn

func NewConn(config *Config) (c *Client, err error)

NewConn returns new client and error if any.

func NewUnknown

func NewUnknown(user string, addr string, auth Auth) (*Client, error)

NewUnknown starts a ssh connection get client without cheking knownhosts. PLEASE AVOID USING THIS, UNLESS YOU KNOW WHAT ARE YOU DOING! if there a "man in the middle proxy", this can harm you! You can add the key to know hosts and use New() func instead!

func (Client) Close

func (c Client) Close() error

Close client net connection.

func (Client) Command

func (c Client) Command(name string, args ...string) (*Cmd, error)

Command returns new Cmd and error if any.

func (Client) CommandContext

func (c Client) CommandContext(ctx context.Context, name string, args ...string) (*Cmd, error)

Command returns new Cmd with context and error, if any.

func (Client) Download

func (c Client) Download(remotePath string, localPath string) (err error)

Download file from remote server!

func (Client) NewSftp

func (c Client) NewSftp(opts ...sftp.ClientOption) (*sftp.Client, error)

NewSftp returns new sftp client and error if any.

func (Client) Run

func (c Client) Run(cmd string) ([]byte, error)

Run starts a new SSH session and runs the cmd, it returns CombinedOutput and err if any.

func (Client) RunContext

func (c Client) RunContext(ctx context.Context, name string) ([]byte, error)

Run starts a new SSH session with context and runs the cmd. It returns CombinedOutput and err if any.

func (Client) Upload

func (c Client) Upload(localPath string, remotePath string) (err error)

Upload a local file to remote server!

type Cmd

type Cmd struct {

	// Path to command executable filename
	Path string

	// Command args.
	Args []string

	// Session env vars.
	Env []string

	// SSH session.
	*ssh.Session

	// Context for cancellation
	Context context.Context
}

Cmd it's like os/exec.Cmd but for ssh session.

func (*Cmd) CombinedOutput

func (c *Cmd) CombinedOutput() ([]byte, error)

CombinedOutput runs cmd on the remote host and returns its combined stdout and stderr.

func (*Cmd) Output

func (c *Cmd) Output() ([]byte, error)

Output runs cmd on the remote host and returns its stdout.

func (*Cmd) Run

func (c *Cmd) Run() error

Run runs cmd on the remote host.

func (*Cmd) SeparatedOutput

func (c *Cmd) SeparatedOutput() ([]byte, []byte, error)

SeparatedOutput runs cmd on the remote host and returns its stdout and stderr.

func (*Cmd) Start

func (c *Cmd) Start() error

Start runs the command on the remote host.

func (*Cmd) String

func (c *Cmd) String() string

String return the command line string.

type Config

type Config struct {
	Auth           Auth
	User           string
	Addr           string
	Port           uint
	Timeout        time.Duration
	Callback       ssh.HostKeyCallback
	BannerCallback ssh.BannerCallback
	KeyExchanges   []string
}

Config for Client.

Jump to

Keyboard shortcuts

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