easyshell

package module
v0.0.0-...-4efc8f3 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: BSD-3-Clause Imports: 21 Imported by: 0

README

EasyShell

  • 支持本地执行命令(windows/linux)
  • 支持通过SSH/TELNET协议在主机、网络设备上远程执行交互式命令
  • 支持自定义提示符匹配规则,大多数情况下使用默认提示符规则即可,使用默认提示符规则时可开启自动纠正(基于默认规则首次匹配结果,默认关闭)
  • 支持自定义解码器,默认自动识别GB18030编码并转换成UTF8
  • 支持自定义字符过滤器,默认自动处理退格、CRLF自动转换为LF,并剔除CSI控制字符(部分情况未处理,如:ISO 8613-3和ISO 8613-6中24位前景色和背景色设置)
  • 支持自定义内容拦截器,内置拦截器包括密码交互(Password)、选项交互(Yes/No)、网络设备自动翻页(More)、网络设备继续执行(Continue)
  • 支持延迟返回输出内容,可指定超过一定时间 或 内容大小 后返回
  • 支持记录原始输出内容和回放,用于调试

代码片段

  • 本地执行命令
    s := NewCmdShell("ping www.baidu.com")
    if err := s.ReadAll(time.Minute, func(lines []string) {
        // handle lines
    }); err != nil {
        return err
    }
    
    ...
    
    s2 := NewCmdShell("cmd /K")
    for _, cmd := range []string{"c:", "dir"} {
        s2.Write(cmd)
        if err := s2.ReadToEndLine(time.Minute, func(lines []string) {
            // handle lines
        }); err != nil {
            return err
        }
    }
  • 远程(SSH)执行命令
    cred := SshCredential{
        Host:       "192.168.1.2",
        Port:       22,
        User:       "zhangsan",
        Password:   "123456",
        PrivateKey: "",
        InsecureAlgorithms: true,
        Timeout:    5,
    }
    s, err := NewSshShell(&SshShellConfig{
        Credential: &cred,
    })
    if err != nil {
        return
    }
    defer s.Close()
    
    for _, line := range s.HeadLine() {
        fmt.Println(line)
    }
	
    // match 'password' prompt and enter the password automatically
    s.Write("su root")
    if err := s.ReadToEndLine(time.Minute, func(lines []string) {
        // handle lines
    }, interceptor.Password("password:", "123456", true)); err != nil {
        return err
    }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSshClient

func NewSshClient(cred *SshCredential) (*ssh.Client, error)

NewSshClient 创建一个新的 SshClient

func NewTelnetClient

func NewTelnetClient(cred *TelnetCredential) (*telnet.Client, error)

Types

type CmdShell

type CmdShell struct {
	*core.ReadWriter
	// contains filtered or unexported fields
}

func NewCmdShell

func NewCmdShell(cmdAndArgs string, config ...*CmdShellConfig) *CmdShell

func (*CmdShell) Close

func (s *CmdShell) Close() error

func (*CmdShell) Cmd

func (s *CmdShell) Cmd() *exec.Cmd

type CmdShellConfig

type CmdShellConfig struct {
	core.Config
	Context context.Context
	Prepare func(c *exec.Cmd)
}

func (*CmdShellConfig) EnsureInit

func (c *CmdShellConfig) EnsureInit()

type SshCredential

type SshCredential struct {
	Host               string `json:"host"`                          // IP地址
	Port               int    `json:"port,omitempty"`                // 端口,默认22
	User               string `json:"user,omitempty"`                // 用户名
	Password           string `json:"password,omitempty"`            // 密码。当密钥与密码同时存在时,优先使用密钥。
	PrivateKey         string `json:"private_key,omitempty"`         // 密钥。当密钥与密码同时存在时,优先使用密钥。
	Timeout            int    `json:"timeout,omitempty"`             // 连接超时时间(秒),默认15秒
	InsecureAlgorithms bool   `json:"insecure_algorithms,omitempty"` // 是否允许不安全的算法
	Fingerprint        string `json:"fingerprint,omitempty"`         // 公钥指纹,用于验证服务器身份
}

type SshShell

type SshShell struct {
	*core.ReadWriter
	// contains filtered or unexported fields
}

func NewSshShell

func NewSshShell(config ...*SshShellConfig) (*SshShell, error)

func NewSshShellFromClient

func NewSshShellFromClient(client *ssh.Client, config ...*SshShellConfig) (*SshShell, error)

func (*SshShell) Client

func (this *SshShell) Client() *ssh.Client

func (*SshShell) Close

func (this *SshShell) Close() (err error)

func (*SshShell) HeadLine

func (this *SshShell) HeadLine() []string

func (*SshShell) Session

func (this *SshShell) Session() *ssh.Session

func (*SshShell) Sftp

func (this *SshShell) Sftp(opt ...sftp.ClientOption) (*sftp.Client, error)

Sftp 获取sftp客户端,调用方无需Close

func (*SshShell) SftpDown

func (this *SshShell) SftpDown(remotePath, localPath string, force bool) error

func (*SshShell) SftpRemove

func (this *SshShell) SftpRemove(path string) error

SftpRemove 删除远程文件、目录,如果是目录,则递归删除目录及子目录下的所有文件

func (*SshShell) SftpUpload

func (this *SshShell) SftpUpload(localPath, remotePath string, force bool) error

type SshShellConfig

type SshShellConfig struct {
	core.Config
	Credential *SshCredential // 凭证
	Echo       bool           // 模拟终端回显,默认值 false,部分网络设备上无效(总是回显)
	Term       string         // 模拟终端类型,默认值 VT100
	TermHeight int            // 模拟终端高度,默认值 200
	TermWidth  int            // 模拟终端宽度,默认值 80
}

func (*SshShellConfig) EnsureInit

func (c *SshShellConfig) EnsureInit()

type TelnetCredential

type TelnetCredential struct {
	Host     string `json:"host"`               // IP地址
	Port     int    `json:"port,omitempty"`     // 端口,默认23
	User     string `json:"user,omitempty"`     // 用户名,可选
	Password string `json:"password,omitempty"` // 密码,可选
	Timeout  int    `json:"timeout,omitempty"`  // 连接超时时间(秒),默认15秒
}

type TelnetShell

type TelnetShell struct {
	*core.ReadWriter
	// contains filtered or unexported fields
}

func NewTelnetShell

func NewTelnetShell(config ...*TelnetShellConfig) (*TelnetShell, error)

func NewTelnetShellFromClient

func NewTelnetShellFromClient(client *telnet.Client, config ...*TelnetShellConfig) (*TelnetShell, error)

func (*TelnetShell) Client

func (this *TelnetShell) Client() *telnet.Client

func (*TelnetShell) Close

func (this *TelnetShell) Close() (err error)

func (*TelnetShell) HeadLine

func (this *TelnetShell) HeadLine() []string

type TelnetShellConfig

type TelnetShellConfig struct {
	core.Config
	Credential *TelnetCredential
}

func (*TelnetShellConfig) EnsureInit

func (c *TelnetShellConfig) EnsureInit()

Directories

Path Synopsis
internal
pkg

Jump to

Keyboard shortcuts

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