Documentation
¶
Index ¶
- type Client
- type FileEntry
- type Option
- func WithDialTimeout(d time.Duration) Option
- func WithHost(addr string, port int) Option
- func WithHostKeyCallback(files ...string) Option
- func WithKeepAlive(interval time.Duration) Option
- func WithPassword(password string) Option
- func WithPrivateKey(privateKey, passPhrase string) Option
- func WithUser(user string) Option
- type SFTP
- func (s *SFTP) Client() (*gosftp.Client, error)
- func (s *SFTP) Close()
- func (s *SFTP) Download(remotePath, localPath string) error
- func (s *SFTP) DownloadDir(remoteDir, localDir string) error
- func (s *SFTP) Exists(remotePath string) bool
- func (s *SFTP) ListDir(remoteDir, pattern string) ([]FileEntry, error)
- func (s *SFTP) RemoveOldFiles(remoteDir string, expireTime time.Time) error
- func (s *SFTP) Stat(remotePath string) (FileEntry, error)
- func (s *SFTP) Upload(localPath, remotePath string) error
- func (s *SFTP) UploadDir(localDir, remoteDir string) error
- type Session
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 (*Client) NewSession ¶
NewSession 创建远程交互式终端会话
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 WithHostKeyCallback ¶ added in v1.2.5
WithHostKeyCallback 使用一个或多个 OpenSSH known_hosts 文件启用主机 密钥校验。文件由 New 统一解析,读取或格式错误由 New 返回。
func WithKeepAlive ¶
WithKeepAlive 设置心跳间隔,<=0 表示禁用(默认禁用)。 启用后客户端会按此间隔向服务端发送 keepalive 探活, 防止被服务端 idle 策略踢出连接。
func WithPrivateKey ¶
WithPrivateKey 设置私钥认证,passPhrase 为私钥密码可为空
type SFTP ¶
type SFTP struct {
// contains filtered or unexported fields
}
SFTP 文件传输客户端
通过 Client.NewSFTP 创建,支持文件列表、上传下载、目录递归传输和过期文件清理。 使用完毕后须调用 Close 释放资源。
func (*SFTP) DownloadDir ¶
DownloadDir 递归下载远程目录到本地
func (*SFTP) ListDir ¶
ListDir 列出远程目录下的文件
pattern 为 glob 匹配模式(如 "*.log"、"smf*"),为空列出所有。 结果按修改时间倒序排列。
func (*SFTP) RemoveOldFiles ¶
RemoveOldFiles 删除远程目录下修改时间早于 expireTime 的文件
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session 远程交互式终端会话
通过 Client.NewSession 创建,提供 Write/Read/Close/WindowChange 四个基础方法。
func (*Session) WindowChange ¶
WindowChange 调整终端窗口大小