ssh

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2023 License: Apache-2.0 Imports: 31 Imported by: 0

README

执行远程命令

执行远程ssh命令标准库

Documentation

Index

Constants

View Source
const DefaultSSHPort = "22"
View Source
const (
	Md5sumCmd = "md5sum %s | cut -d\" \" -f1"
)
View Source
const SUDO = "sudo "

Variables

This section is empty.

Functions

func ReadPipe

func ReadPipe(stdout, stderr io.Reader, alsoToStdout bool)

func WaitSSHReady

func WaitSSHReady(ssh Interface, tryTimes int, hosts ...net.IP) error

Types

type Client added in v0.9.0

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

type Interface

type Interface interface {
	// Copy local files to remote host
	// scp -r /tmp root@192.168.0.2:/root/tmp => Copy("192.168.0.2","tmp","/root/tmp")
	// need check md5sum
	Copy(host net.IP, srcFilePath, dstFilePath string) error
	// CopyR copy remote host files to localhost
	CopyR(host net.IP, srcFilePath, dstFilePath string) error
	// CmdAsync exec command on remote host, and asynchronous return logs
	CmdAsync(host net.IP, env map[string]string, cmd ...string) error
	// Cmd exec command on remote host, and return combined standard output and standard error
	Cmd(host net.IP, env map[string]string, cmd string) ([]byte, error)
	// CmdToString exec command on remote host, and return spilt standard output and standard error
	CmdToString(host net.IP, env map[string]string, cmd, spilt string) (string, error)
	// IsFileExist check remote file exist or not
	IsFileExist(host net.IP, remoteFilePath string) (bool, error)
	// RemoteDirExist Remote file existence returns true, nil
	RemoteDirExist(host net.IP, remoteDirpath string) (bool, error)
	// GetPlatform Get remote platform
	GetPlatform(host net.IP) (v1.Platform, error)
	// Ping Ping remote host
	Ping(host net.IP) error
}

func GetHostSSHClient

func GetHostSSHClient(hostIP net.IP, cluster *v2.Cluster) (Interface, error)

GetHostSSHClient is used to executed bash command and no std out to be printed.

func NewSSHClient

func NewSSHClient(ssh *v1.SSH, alsoToStdout bool) Interface

func NewStdoutSSHClient

func NewStdoutSSHClient(hostIP net.IP, cluster *v2.Cluster) (Interface, error)

NewStdoutSSHClient is used to show std out when execute bash command.

type SSH

type SSH struct {
	AlsoToStdout bool
	Encrypted    bool
	User         string
	Password     string
	Port         string
	PkFile       string
	PkPassword   string
	Timeout      *time.Duration
	LocalAddress []net.Addr
	Fs           fs.Interface
}

func (*SSH) Cmd

func (s *SSH) Cmd(host net.IP, hostEnv map[string]string, cmd string) ([]byte, error)

func (*SSH) CmdAsync

func (s *SSH) CmdAsync(host net.IP, hostEnv map[string]string, cmds ...string) error

func (*SSH) CmdToString

func (s *SSH) CmdToString(host net.IP, env map[string]string, cmd, split string) (string, error)

CmdToString is in host exec cmd and replace to spilt str

func (*SSH) Connect

func (s *SSH) Connect(host net.IP) (*ssh.Client, *ssh.Session, error)

func (*SSH) Copy

func (s *SSH) Copy(host net.IP, localPath, remotePath string) error

Copy file or dir to remotePath, add md5 validate

func (*SSH) CopyR added in v0.9.0

func (s *SSH) CopyR(host net.IP, localFilePath, remoteFilePath string) error

CopyR scp remote file to local

func (*SSH) GetPlatform added in v0.9.0

func (s *SSH) GetPlatform(host net.IP) (v1.Platform, error)

func (*SSH) IsFileExist

func (s *SSH) IsFileExist(host net.IP, remoteFilePath string) (bool, error)

func (*SSH) NewSudoSftpClient

func (s *SSH) NewSudoSftpClient(conn *ssh.Client, opts ...sftp.ClientOption) (*sftp.Client, error)

func (*SSH) Ping

func (s *SSH) Ping(host net.IP) error

func (*SSH) RemoteDirExist

func (s *SSH) RemoteDirExist(host net.IP, remoteDirPath string) (bool, error)

RemoteDirExist if remote file not exist return false and nil

Jump to

Keyboard shortcuts

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