mssh

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

README

mssh - SSH/SFTP 客户端组件

SSH 远程命令执行和 SFTP 文件传输工具,支持带进度条的文件上传下载。

特性

  • 🔐 SSH 连接:支持密码和密钥认证
  • 📁 SFTP 传输:上传下载文件
  • 📊 进度显示:文件传输带实时进度条
  • 🖥️ 远程执行:在远程服务器执行命令并获取输出

快速开始

SSH 连接并执行命令
package main

import (
    "fmt"
    "github.com/quietking0312/component/mssh"
)

func main() {
    cli := &mssh.Cli{
        Host:     "192.168.1.100",
        Port:     22,
        User:     "root",
        Password: "password",
    }

    if err := cli.Connect(); err != nil {
        panic(err)
    }
    defer cli.Close()

    output, err := cli.Run("uname -a")
    if err != nil {
        panic(err)
    }
    fmt.Println(output)
}
上传文件
err := cli.UploadFile("/local/path/file.txt", "/remote/path/file.txt")
if err != nil {
    panic(err)
}
带进度条上传
ch := make(chan int64)
go func() {
    for progress := range ch {
        fmt.Printf("已上传: %d bytes\n", progress)
    }
}()

file, _ := os.Open("/local/bigfile.zip")
err := cli.UploadFileAndProgress(file, "/remote/bigfile.zip", ch)
下载文件
err := cli.DownloadFile("/remote/path/file.txt", "/local/dir", "file.txt")
if err != nil {
    panic(err)
}

API 文档

类型
type Cli struct {
    Host       string
    Port       int
    User       string
    Password   string
    PrivateKey string // 私钥路径或内容
}
方法
方法 说明
(c *Cli) Connect() error 建立 SSH 连接
(c *Cli) Close() error 关闭连接
(c *Cli) Run(command string) (string, error) 执行远程命令
(c *Cli) UploadFile(localFilePath, remotePath string) error 上传文件
(c *Cli) UploadFileAndProgress(srcFile io.Reader, remoteFile string, ch chan<- int64) error 带进度上传
(c *Cli) DownloadFile(remotePath, localDir, localFileName string) error 下载文件

测试

cd mssh
go test -v

注意事项

  1. 首次连接新服务器时,需要处理主机密钥确认
  2. 大文件传输建议使用带进度条的方法,便于监控
  3. 私钥认证时,请确保证书格式正确(PEM)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client added in v1.0.10

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

Client SSH 客户端

func New added in v1.0.10

func New(cfg *Config) (*Client, error)

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

func (*Client) Close added in v1.0.10

func (c *Client) Close() error

Close 关闭连接。

func (*Client) Download added in v1.0.10

func (c *Client) Download(remotePath, localPath string, opt *UploadOption) error

Download 下载远程文件到本地路径。 localPath 可以是目录(自动取远程文件名)或完整文件路径。

func (*Client) DownloadWriter added in v1.0.10

func (c *Client) DownloadWriter(remotePath string, dst io.Writer, opt *UploadOption) error

DownloadWriter 下载远程文件写入 io.Writer。

func (*Client) Mkdir added in v1.0.10

func (c *Client) Mkdir(remotePath string) error

Mkdir 在远程创建目录(含父目录)。

func (*Client) NewSession added in v1.0.10

func (c *Client) NewSession(opts *ShellOptions) (*Session, error)

NewSession 创建可手动控制的交互会话。 适合需要动态 resize(如 WebSocket 终端代理)的场景。

func (*Client) ReadDir added in v1.0.10

func (c *Client) ReadDir(remotePath string) ([]os.FileInfo, error)

ReadDir 列出远程目录内容。

func (*Client) Remove added in v1.0.10

func (c *Client) Remove(remotePath string) error

Remove 删除远程文件。

func (*Client) Run added in v1.0.10

func (c *Client) Run(command string) (string, error)

Run 执行非交互命令,返回合并后的 stdout+stderr 输出。 不支持 vim/top 等需要 PTY 的程序,请使用 Shell。

func (*Client) Shell added in v1.0.10

func (c *Client) Shell(opts *ShellOptions) error

Shell 开启交互式 Shell,支持 vim/top 等需要 PTY 的程序。 调用方负责将本地终端切换为 raw 模式(见示例)。 函数阻塞直到 Shell 退出。

func (*Client) Stat added in v1.0.10

func (c *Client) Stat(remotePath string) (os.FileInfo, error)

Stat 获取远程文件信息。

func (*Client) Upload added in v1.0.10

func (c *Client) Upload(localPath, remotePath string, opt *UploadOption) error

Upload 上传本地文件到远程路径。 remotePath 可以是目录(自动取本地文件名)或完整文件路径。

func (*Client) UploadReader added in v1.0.10

func (c *Client) UploadReader(src io.Reader, remotePath string, opt *UploadOption) error

UploadReader 从 io.Reader 上传到远程完整文件路径。

type Config added in v1.0.10

type Config struct {
	// 用户名
	User string
	// 密码(与 KeyPath/KeyPEM 三选一)
	Password string
	// 私钥文件路径
	KeyPath string
	// 私钥 PEM 内容(字符串形式,与 KeyPath 二选一)
	KeyPEM string
	// 服务器地址,格式 host:port
	Addr string
	// 连接超时,默认 10s
	Timeout time.Duration
}

Config SSH 连接配置

type PTYOptions added in v1.0.10

type PTYOptions struct {
	// 终端类型,默认 xterm-256color
	Term string
	// 宽度(列),默认 220
	Width int
	// 高度(行),默认 50
	Height int
}

PTYOptions PTY 终端参数

type ResizeOption added in v1.0.10

type ResizeOption struct {
	Width  int
	Height int
}

ResizeOption 用于 WindowChange

type Session added in v1.0.10

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

Session 可控制的交互会话(支持动态调整窗口大小)

func (*Session) Close added in v1.0.10

func (s *Session) Close() error

Close 关闭会话。

func (*Session) Resize added in v1.0.10

func (s *Session) Resize(width, height int) error

Resize 动态调整终端窗口大小(用于 WebSocket 终端 resize 事件)。

func (*Session) Wait added in v1.0.10

func (s *Session) Wait() error

Wait 等待会话结束。

type ShellOptions added in v1.0.10

type ShellOptions struct {
	PTY PTYOptions
	// 标准输入,默认 os.Stdin
	Stdin io.Reader
	// 标准输出,默认 os.Stdout
	Stdout io.Writer
	// 标准错误,默认 os.Stderr
	Stderr io.Writer
}

ShellOptions Shell 会话选项

type UploadOption added in v1.0.10

type UploadOption struct {
	// 上传进度回调,参数为本次写入字节数;为 nil 时不回调
	OnProgress func(written int64)
	// 进度回调间隔,默认 500ms
	ProgressInterval time.Duration
}

UploadOption 上传选项

Jump to

Keyboard shortcuts

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