ssh

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: Apache-2.0 Imports: 25 Imported by: 0

README

执行远程命令

执行远程ssh命令标准库

Documentation

Index

Constants

View Source
const KByte = 1024
View Source
const MByte = 1024 * 1024
View Source
const (
	Md5sumCmd = "md5sum %s | cut -d\" \" -f1"
)

Variables

View Source
var DebugMode bool

Functions

func LocalMd5Sum added in v0.1.5

func LocalMd5Sum(localPath string) string

func WaitSSHReady

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

Types

type Client

type Client struct {
	SSH  Interface
	Host string
}

func NewSSHClientWithCluster

func NewSSHClientWithCluster(cluster *v1.Cluster) (*Client, error)

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, srcFilePath, dstFilePath string) error
	// copy remote host files to localhost
	Fetch(host, srcFilePath, dstFilePath string) error
	// exec command on remote host, and asynchronous return logs
	CmdAsync(host string, cmd ...string) error
	// exec command on remote host, and return combined standard output and standard error
	Cmd(host, cmd string) ([]byte, error)
	// check remote file exist or not
	IsFileExist(host, remoteFilePath string) bool
	//Remote file existence returns true, nil
	RemoteDirExist(host, remoteDirpath string) (bool, error)
	// exec command on remote host, and return spilt standard output and standard error
	CmdToString(host, cmd, spilt string) (string, error)
	Ping(host string) error
}

func GetHostSSHClient added in v0.6.0

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

func NewSSHByCluster

func NewSSHByCluster(cluster *v1.Cluster) Interface

func NewSSHClient added in v0.6.0

func NewSSHClient(ssh *v1.SSH) Interface

type SSH

type SSH struct {
	User         string
	Password     string
	PkFile       string
	PkPassword   string
	Timeout      *time.Duration
	LocalAddress *[]net.Addr
}

func (*SSH) Cmd

func (s *SSH) Cmd(host, cmd string) ([]byte, error)

func (*SSH) CmdAsync

func (s *SSH) CmdAsync(host string, cmds ...string) error

func (*SSH) CmdToString

func (s *SSH) CmdToString(host, cmd, spilt string) (string, error)

CmdToString is in host exec cmd and replace to spilt str

func (*SSH) Connect

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

func (*SSH) Copy

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

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

func (*SSH) Fetch

func (s *SSH) Fetch(host, localFilePath, remoteFilePath string) error

CopyRemoteFileToLocal is scp remote file to local

func (*SSH) IsFileExist

func (s *SSH) IsFileExist(host, remoteFilePath string) bool

RemoteFileExist is

func (*SSH) Ping

func (s *SSH) Ping(host string) error

func (*SSH) RemoteDirExist

func (s *SSH) RemoteDirExist(host, remoteDirpath string) (bool, error)

if remote file not exist return false and nil

func (*SSH) RemoteMd5Sum

func (s *SSH) RemoteMd5Sum(host, remoteFilePath string) string

Jump to

Keyboard shortcuts

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