ssh

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client SSH客户端

支持密码和私钥认证,提供远程命令执行、文件列表、交互式会话和SFTP文件传输。

示例:

client, err := ssh.New(
    ssh.WithHost("192.168.1.1", 22),
    ssh.WithUser("root"),
    ssh.WithPassword("password"),
)
defer client.Close()
output, err := client.RunCMD("ls -la")

func New

func New(opts ...Option) (*Client, error)

New 创建SSH客户端并建立连接

func (*Client) Addr

func (c *Client) Addr() string

Addr 返回主机地址

func (*Client) Close

func (c *Client) Close()

Close 关闭SSH连接

func (*Client) Exec

func (c *Client) Exec(cmdStr string) (string, error)

Exec 执行远程命令,返回标准输出和错误输出的合并结果

func (*Client) NewSFTP

func (c *Client) NewSFTP() (*SFTP, error)

NewSFTP 创建SFTP文件传输客户端

func (*Client) NewSession

func (c *Client) NewSession(cols, rows int) (*Session, error)

NewSession 创建远程交互式终端会话

func (*Client) User

func (c *Client) User() string

User 返回登录用户名

type FileEntry

type FileEntry struct {
	FileName     string `json:"fileName"`     // 文件名
	FileType     string `json:"fileType"`     // 类型: dir, file, symlink
	FileMode     string `json:"fileMode"`     // 权限字符串如 "rwxr-xr-x"
	FileSize     int64  `json:"fileSize"`     // 文件大小(字节)
	ModifiedTime int64  `json:"modifiedTime"` // 最后修改时间(Unix毫秒)
}

FileEntry 文件信息

type Option

type Option func(*Client)

Option SSH客户端配置选项

func WithDialTimeout

func WithDialTimeout(d time.Duration) Option

WithDialTimeout 设置连接超时,默认 5s

func WithHost

func WithHost(addr string, port int) Option

WithHost 设置主机地址和端口,默认端口 22

func WithHostKeyCallback added in v1.2.5

func WithHostKeyCallback(files ...string) Option

WithHostKeyCallback 使用一个或多个 OpenSSH known_hosts 文件启用主机 密钥校验。文件由 New 统一解析,读取或格式错误由 New 返回。

func WithKeepAlive

func WithKeepAlive(interval time.Duration) Option

WithKeepAlive 设置心跳间隔,<=0 表示禁用(默认禁用)。 启用后客户端会按此间隔向服务端发送 keepalive 探活, 防止被服务端 idle 策略踢出连接。

func WithPassword

func WithPassword(password string) Option

WithPassword 设置密码认证

func WithPrivateKey

func WithPrivateKey(privateKey, passPhrase string) Option

WithPrivateKey 设置私钥认证,passPhrase 为私钥密码可为空

func WithUser

func WithUser(user string) Option

WithUser 设置登录用户名,默认 "root"

type SFTP

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

SFTP 文件传输客户端

通过 Client.NewSFTP 创建,支持文件列表、上传下载、目录递归传输和过期文件清理。 使用完毕后须调用 Close 释放资源。

func (*SFTP) Client

func (s *SFTP) Client() (*gosftp.Client, error)

Client 获取SFTP客户端

func (*SFTP) Close

func (s *SFTP) Close()

Close 关闭SFTP客户端

func (*SFTP) Download

func (s *SFTP) Download(remotePath, localPath string) error

Download 下载远程文件到本地

func (*SFTP) DownloadDir

func (s *SFTP) DownloadDir(remoteDir, localDir string) error

DownloadDir 递归下载远程目录到本地

func (*SFTP) Exists

func (s *SFTP) Exists(remotePath string) bool

Exists 检查远程文件或目录是否存在

func (*SFTP) ListDir

func (s *SFTP) ListDir(remoteDir, pattern string) ([]FileEntry, error)

ListDir 列出远程目录下的文件

pattern 为 glob 匹配模式(如 "*.log"、"smf*"),为空列出所有。 结果按修改时间倒序排列。

func (*SFTP) RemoveOldFiles

func (s *SFTP) RemoveOldFiles(remoteDir string, expireTime time.Time) error

RemoveOldFiles 删除远程目录下修改时间早于 expireTime 的文件

func (*SFTP) Stat

func (s *SFTP) Stat(remotePath string) (FileEntry, error)

Stat 获取单个远程文件的详细信息

func (*SFTP) Upload

func (s *SFTP) Upload(localPath, remotePath string) error

Upload 上传本地文件到远程

func (*SFTP) UploadDir

func (s *SFTP) UploadDir(localDir, remoteDir string) error

UploadDir 递归上传本地目录到远程

type Session

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

Session 远程交互式终端会话

通过 Client.NewSession 创建,提供 Write/Read/Close/WindowChange 四个基础方法。

func (*Session) Close

func (s *Session) Close()

Close 关闭会话

func (*Session) Read

func (s *Session) Read() []byte

Read 读取输出,阻塞直到有数据到达

func (*Session) WindowChange

func (s *Session) WindowChange(cols, rows int) error

WindowChange 调整终端窗口大小

func (*Session) Write

func (s *Session) Write(cmd string) (int, error)

Write 写入命令(回车 \n 才会执行)

Jump to

Keyboard shortcuts

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