ssh_cmdx

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(f interface{}, v ...interface{})

Debug logs a message at debug level.

func Error

func Error(f interface{}, v ...interface{})

Error logs a message at error level.

func Fatal

func Fatal(f interface{}, v ...interface{})

Fatal logs a message at emergency level and exit.

func GetHostIPAndPortOrDefault

func GetHostIPAndPortOrDefault(host, Default string) (string, string)

func GetSSHHostIPAndPort

func GetSSHHostIPAndPort(host string) (string, string)

func GetTimeoutContext

func GetTimeoutContext() (context.Context, context.CancelFunc)

GetTimeoutContext create a context.Context with default timeout default execution timeout in sealos is just fine, if you want to customize the timeout setting, you must invoke the `RegisterFlags` function above.

func Info

func Info(f interface{}, v ...interface{})

Info logs a message at info level.

func IsDebugMode

func IsDebugMode() bool

IsDebugMode check DebugLevel enabled

func NewSudoSftpClient

func NewSudoSftpClient(conn *ssh.Client, sudopwd string, opts ...sftp.ClientOption) (*sftp.Client, error)

func Panic

func Panic(f interface{}, v ...interface{})

Panic logs a message at emergency level and exit.

func PathIsExist

func PathIsExist(fileName string) bool

func RegisterFlags

func RegisterFlags(fs *pflag.FlagSet)

func WaitReady

func WaitReady(client Interface, _ int, hosts ...string) error

WaitReady wait for ssh ready

func Warn

func Warn(f interface{}, v ...interface{})

Warn logs a message at warning level.

Types

type Client

type Client struct {
	*ssh.ClientConfig
	*Option
}

func New

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

func (*Client) Cmd

func (c *Client) Cmd(host, cmd string) ([]byte, error)

Cmd 同步的执行一个命令

func (*Client) CmdAsync

func (c *Client) CmdAsync(host string, cmds ...string) error

CmdAsync not actually asynchronously, just print output asynchronously

func (*Client) CmdAsyncWithContext

func (c *Client) CmdAsyncWithContext(ctx context.Context, host string, cmds ...string) error

CmdAsyncWithContext 异步执行cmd

func (*Client) CmdToString

func (c *Client) CmdToString(host, cmd, sep string) (string, error)

CmdToString execute command on host and replace output with sep to oneline call Cmd

func (*Client) Connect

func (c *Client) Connect(host string) (sshClient *ssh.Client, session *ssh.Session, err error)

func (*Client) Copy

func (c *Client) Copy(host, localPath, remotePath string) error

Copy is copy file or dir to remotePath, add md5 validate

func (*Client) Fetch

func (c *Client) Fetch(host, src, dst string) error

Fetch 从远端拉取一个文件

func (*Client) Ping

func (c *Client) Ping(host string) error

Ping 检查是否能够连接到远程机器

func (*Client) RemoteSha256Sum

func (c *Client) RemoteSha256Sum(host, remoteFilePath string) string

type HostClient

type HostClient struct {
	SSHClient  *ssh.Client
	SftpClient *sftp.Client
}

type HostClientMap

type HostClientMap struct {
	ClientMap map[string]HostClient
	Mux       sync.Mutex
}

type Interface

type Interface interface {
	// Copy copy local file to remote
	// scp -r /tmp root@192.168.0.2:/root/tmp => Copy("192.168.0.2","tmp","/root/tmp")
	// skip checksum if env DO_NOT_CHECKSUM=true
	Copy(host, src, dst string) error
	// Fetch fetch remote file to local
	// scp -r root@192.168.0.2:/remote/path/file /local/path/file => Fetch("192.168.0.2","/remote/path/file", "/local/path/file",)
	Fetch(host, src, dst string) error
	// CmdAsync exec commands on remote host asynchronously
	CmdAsync(host string, cmds ...string) error
	CmdAsyncWithContext(ctx context.Context, host string, cmds ...string) error
	// Cmd exec command on remote host, and return combined standard output and standard error
	Cmd(host, cmd string) ([]byte, error)
	// CmdToString exec command on remote host, and return spilt standard output by separator and standard error
	CmdToString(host, cmd, spilt string) (string, error)
	Ping(host string) error
}

func MustNewClient

func MustNewClient(ssh *SSH, isStdout bool) Interface

func NewFromSSH

func NewFromSSH(ssh *SSH, isStdout bool) (Interface, error)

type Option

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

func NewOption

func NewOption() *Option

type OptionFunc

type OptionFunc func(*Option)

func WithHostKeyCallback

func WithHostKeyCallback(fn ssh.HostKeyCallback) OptionFunc

func WithPassword

func WithPassword(p string) OptionFunc

func WithPrivateKeyAndPhrase

func WithPrivateKeyAndPhrase(pk, passphrase string) OptionFunc

func WithRawPrivateKeyDataAndPhrase

func WithRawPrivateKeyDataAndPhrase(raw, passphrase string) OptionFunc

func WithStdoutEnable

func WithStdoutEnable(b bool) OptionFunc

func WithSudoEnable

func WithSudoEnable(b bool) OptionFunc

func WithTimeout

func WithTimeout(timeout time.Duration) OptionFunc

func WithUsername

func WithUsername(u string) OptionFunc

type SSH

type SSH struct {
	User     string `json:"user,omitempty"`
	Passwd   string `json:"passwd,omitempty"`
	PkName   string `json:"pkName,omitempty"`
	PkData   string `json:"pkData,omitempty"`
	Pk       string `json:"pk,omitempty"`
	PkPasswd string `json:"pkPasswd,omitempty"`
	Port     uint16 `json:"port,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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