ssh

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartKeepAlive

func StartKeepAlive(client *ssh.Client, interval time.Duration, fallback func(err error))

StartKeepAlive 开启一个协程,定期向 SSH Server 发送心跳 interval: 心跳间隔 (建议 15s - 60s) fallback: 可选的回调函数,用于在心跳失败后执行,心跳失败时会关闭连接

Types

type AuthMethod

type AuthMethod interface {
	GetMethod() (ssh.AuthMethod, error)
}

AuthMethod 定义获取 SSH 认证方法的接口

type Client

type Client struct {
	// contains filtered or unexported fields
}

func (*Client) Close

func (c *Client) Close() error

Close 关闭连接

func (*Client) Node

func (c *Client) Node() models.Node

Node 返回当前连接对应的节点配置

func (*Client) Run

func (c *Client) Run(ctx context.Context, cmd string) (string, error)

func (*Client) RunScript

func (c *Client) RunScript(ctx context.Context, scriptContent string) (string, error)

RunScript 执行 Shell 脚本内容

func (*Client) RunScriptWithSudo

func (c *Client) RunScriptWithSudo(ctx context.Context, scriptContent string) (string, error)

RunScriptWithSudo 提权执行脚本

func (*Client) RunWithSudo

func (c *Client) RunWithSudo(ctx context.Context, command string) (string, error)

func (*Client) RunWithoutLogin

func (c *Client) RunWithoutLogin(ctx context.Context, cmd string) (string, error)

RunWithoutLogin 执行命令并在非登录 Shell 中运行,避免加载 profile 脚本产生干扰输出

func (*Client) SSHClient

func (c *Client) SSHClient() *ssh.Client

SSHClient 暴露底层的 ssh.Client (供高级操作使用,如 SCP)

func (*Client) Shell

func (c *Client) Shell(ctx context.Context) error

func (*Client) ShellWithSudo

func (c *Client) ShellWithSudo(ctx context.Context) error

type Connector

type Connector struct {
	Config config.ConfigProvider
	// contains filtered or unexported fields
}

Connector 负责创建 SSH 连接

func NewConnector

func NewConnector(cfg config.ConfigProvider) *Connector

NewConnector 创建一个新的 Connector

func (*Connector) CloseAll

func (c *Connector) CloseAll()

CloseAll 关闭所有缓存的连接 (在程序退出前调用)

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context, nodeName string) (*Client, error)

Connect 根据节点名称建立 SSH 连接 自动处理跳板机逻辑:如果节点配置了 ProxyJump,会递归建立连接

type Dialer

type Dialer interface {
	Dial(network, addr string) (net.Conn, error)
	DialContext(ctx context.Context, network, addr string) (net.Conn, error)
}

Dialer 定义网络连接行为的接口 用于统一 "直连" 和 "通过 SSH 跳板机连接" 的行为

type KeyAuth

type KeyAuth struct {
	Path       string
	Passphrase string
}

KeyAuth 实现私钥认证

func (*KeyAuth) GetMethod

func (k *KeyAuth) GetMethod() (ssh.AuthMethod, error)

type PasswordAuth

type PasswordAuth struct {
	Password string
}

PasswordAuth 实现密码认证

func (*PasswordAuth) GetMethod

func (p *PasswordAuth) GetMethod() (ssh.AuthMethod, error)

type SSHProxyDialer

type SSHProxyDialer struct {
	Client *ssh.Client
}

SSHProxyDialer 实现了 Dialer 接口,通过 SSH 隧道转发流量

func (*SSHProxyDialer) Dial

func (s *SSHProxyDialer) Dial(network, addr string) (net.Conn, error)

func (*SSHProxyDialer) DialContext

func (s *SSHProxyDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

Jump to

Keyboard shortcuts

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