Documentation
ΒΆ
Index ΒΆ
- Constants
- func AddKnownHost(host string, remote net.Addr, key ssh.PublicKey, knownFile string) (err error)
- func CheckKnownHost(host string, remote net.Addr, key ssh.PublicKey, knownFile string) (found bool, err error)
- func DefaultKnownHosts() (ssh.HostKeyCallback, error)
- func DefaultKnownHostsPath() (string, error)
- func Dial(c *Client, config *ssh.ClientConfig) error
- func EnsureKnownHosts(file string) (ssh.HostKeyCallback, error)
- func HasAgent() bool
- func KnownHosts(file string) (ssh.HostKeyCallback, error)
- func ParseKey(privateKey []byte, passphrase string) (signer ssh.Signer, err error)
- func ParseKeyFile(prvFile string, passphrase string) (ssh.Signer, error)
- type Client
- func (c *Client) Close() error
- func (c *Client) Command(name string, args ...string) (*Cmd, error)
- func (c *Client) CommandContext(ctx context.Context, name string, args ...string) (*Cmd, error)
- func (c *Client) Download(remotePath string, localPath string) (err error)
- func (c *Client) NewSftp(opts ...sftp.ClientOption) (*sftp.Client, error)
- func (c *Client) Run(cmd string) ([]byte, error)
- func (c *Client) RunContext(ctx context.Context, name string) ([]byte, error)
- func (c *Client) Script(ctx context.Context, r io.Reader, opts ...CmdOption) (cmd *Cmd, err error)
- func (c *Client) ScriptFile(ctx context.Context, localPath string, opts ...CmdOption) (*Cmd, error)
- func (c *Client) Upload(localPath string, remotePath string) (err error)
- type Cmd
- type CmdOption
- type Dialer
- type Option
- func WithAgent(conn net.Conn) Option
- func WithAgentSocket(socket string) Option
- func WithAuth(method ssh.AuthMethod) Option
- func WithBannerCallback(cb ssh.BannerCallback) Option
- func WithConfig(fn func(*ssh.ClientConfig) error) Option
- func WithDefaultAgent() Option
- func WithHostKeyCallback(cb ssh.HostKeyCallback) Option
- func WithInsecureIgnoreHostKey() Option
- func WithJump(jumpClient *Client) Option
- func WithKey(pemBytes []byte, passphrase string) Option
- func WithKeyFile(keyFile string, passphrase string) Option
- func WithKeyboardInteractive(handler func(user, instruction, question string, echo bool) (string, error)) Option
- func WithKnownHosts(path string) Option
- func WithPassword(password string) Option
- func WithPort(port uint) Option
- func WithProxy(socks5URL string) Option
- func WithSigner(signer ssh.Signer) Option
- func WithTimeout(d time.Duration) Option
Constants ΒΆ
const DefaultClientVersion = "SSH-2.0-Goph"
DefaultClientVersion is the SSH client version sent during handshake.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func AddKnownHost ΒΆ
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 a host key callback from the default known hosts path. It ensures an empty known_hosts file exists, so first-time users get a "host not found" result instead of a file read error.
func DefaultKnownHostsPath ΒΆ
DefaultKnownHostsPath returns default user knows hosts file.
func Dial ΒΆ
func Dial(c *Client, config *ssh.ClientConfig) error
Dial establishes the SSH connection described by c and config. It honors c.User, c.ProxyURL, c.Jump, and c.Port, and applies the default known hosts callback if HostKeyCallback is nil.
If config.User is empty, it is set from c.User. If c.User is also empty, the current OS user is used, matching OpenSSH default behavior.
func EnsureKnownHosts ΒΆ
func EnsureKnownHosts(file string) (ssh.HostKeyCallback, error)
EnsureKnownHosts returns a host key callback from a custom known hosts path, Creating the file (and its parent directory) if it does not exist.
func HasAgent ΒΆ
func HasAgent() bool
HasAgent checks if ssh agent exists via the SSH_AUTH_SOCK env variable.
func KnownHosts ΒΆ
func KnownHosts(file string) (ssh.HostKeyCallback, error)
KnownHosts returns a host key callback from a custom known hosts path. The file must already exist; if it is or may be missing, use EnsureKnownHosts.
Types ΒΆ
type Client ΒΆ
Client represents a Goph SSH client.
func New ΒΆ
New starts a new SSH connection. By default it uses the default known_hosts file for host key verification, port 22, and a 20 second timeout. Override with With* options.
func (*Client) CommandContext ΒΆ
CommandContext returns new Cmd with context and error, if any.
func (*Client) Run ΒΆ
Run starts a new SSH session and runs the cmd, it returns CombinedOutput and err if any.
func (*Client) RunContext ΒΆ
RunContext starts a new SSH session with context and runs the cmd.
func (*Client) Script ΒΆ
Script runs a script from an io.Reader on the remote host via /bin/sh you can override (with WithPath).
func (*Client) ScriptFile ΒΆ
ScriptFile reads the local script file into memory and executes it on the remote. Warning: the entire file is loaded into memory. Use Script with an io.Reader directly for large files.
type Cmd ΒΆ
type Cmd struct {
// SSH session.
*ssh.Session
// Path to command executable filename
Path string
// Command args.
Args []string
// Session env vars.
Env []string
// Cancel is called when Context is done.
// If non-nil, it replaces the default ssh.SIGINT signal.
// If nil, ssh.SIGINT is sent on cancellation.
Cancel func() error
// contains filtered or unexported fields
}
Cmd it's like os/exec.Cmd but for ssh session.
func (*Cmd) CombinedOutput ΒΆ
CombinedOutput runs cmd on the remote host and returns its combined stdout and stderr.
func (*Cmd) String ΒΆ
String returns the command line string.
WARNING: Path and Args are joined as is without any shell escaping. If Path or Args contain untrusted input, remote command injection is possible. Sanitize or shell quote untrusted values yourself before building the Cmd, or use a shell quoting helper from your application.
type CmdOption ΒΆ
type CmdOption func(*Cmd)
CmdOption configures a Cmd after creation.
func WithStderr ΒΆ
WithStderr sets the remote command's stderr writer.
func WithStdout ΒΆ
WithStdout sets the remote command's stdout writer.
type Dialer ΒΆ
type Dialer struct {
// contains filtered or unexported fields
}
Dialer holds reusable connection options for establishing SSH connections.
d := goph.NewDialer(goph.WithKeyFile("id_rsa", ""))
c1, _ := d.New("root", "host1")
c2, _ := d.New("root", "host2", goph.WithPort(2222))
type Option ΒΆ
type Option func(*Client, *ssh.ClientConfig) error
Option is a functional option for configuring a Client.
func WithAgent ΒΆ
WithAgent sets SSH agent authentication from an existing agent net.Conn. The conn must be connected to an SSH agent (Unix socket, Windows pipe, etc.). Silently skips if conn is nil.
func WithAgentSocket ΒΆ
WithAgentSocket sets SSH agent authentication from a Unix socket path. Silently skips if the agent socket is unavailable. The socket is dialed lazily during the SSH handshake, so this option is safe to apply multiple times (e.g. for connection key calculation).
func WithAuth ΒΆ
func WithAuth(method ssh.AuthMethod) Option
WithAuth appends a custom ssh.AuthMethod.
func WithBannerCallback ΒΆ
func WithBannerCallback(cb ssh.BannerCallback) Option
WithBannerCallback sets a banner callback.
func WithConfig ΒΆ
func WithConfig(fn func(*ssh.ClientConfig) error) Option
WithConfig applies a callback to the underlying *ssh.ClientConfig before dial.
func WithDefaultAgent ΒΆ
func WithDefaultAgent() Option
WithDefaultAgent sets SSH agent authentication using the SSH_AUTH_SOCK environment variable.
func WithHostKeyCallback ΒΆ
func WithHostKeyCallback(cb ssh.HostKeyCallback) Option
WithHostKeyCallback sets a custom host key callback.
func WithInsecureIgnoreHostKey ΒΆ
func WithInsecureIgnoreHostKey() Option
WithInsecureIgnoreHostKey disables host key verification.
func WithKeyFile ΒΆ
WithKeyFile sets public key authentication from a private key file.
func WithKeyboardInteractive ΒΆ
func WithKeyboardInteractive(handler func(user, instruction, question string, echo bool) (string, error)) Option
WithKeyboardInteractive sets keyboard interactive authentication using a user provided handler, the handler is called once for each prompt the server sends, and returns the answer or an error.
func WithKnownHosts ΒΆ
WithKnownHosts uses the known hosts file for host key verification.
func WithPassword ΒΆ
WithPassword sets password authentication.
func WithProxy ΒΆ
WithProxy routes the SSH connection through a SOCKS5 proxy. eg like socks5://127.0.0.1:1080
func WithSigner ΒΆ
WithSigner appends public key authentication from an ssh.Signer.
func WithTimeout ΒΆ
WithTimeout sets the connection timeout.