transport

package
v0.7.9 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WindowsShellExt = ".bat"
	LinuxShellExt   = ".sh"
)
View Source
const (
	KillSignal       = "0x09"
	KeepAliveMessage = "keepalive@golang.org"

	GOOSLinux   = "linux"
	GOOSFreeBSD = "freebsd"
	GOOSWindows = "windows"
	GOOSDarwin  = "darwin"
	GOOSUnknown = "unknown"

	ArchAmd64   = "amd64"
	ArchI386    = "386"
	ArchArm     = "arm"
	ArchUnknown = "unknown"

	DefaultPtyCols = 200
	DefaultPtyRows = 40
)
View Source
const (
	CmiTimeLayout = "20060102150405.999999"
)

Variables

View Source
var (
	DefaultTimeoutSec   = utils.GetEnvInt("ENV_SSH_DIAL_TIMEOUT", defaultTimeout)
	DefaultRWTimeoutSec = utils.GetEnvInt("ENV_SSH_RW_TIMEOUT", defaultRWTimeout)

	SSHDialTimeout = time.Duration(DefaultTimeoutSec) * time.Second
	SSHRWTimeout   = time.Duration(DefaultRWTimeoutSec) * time.Second
)

Functions

func AuthWithAgent

func AuthWithAgent() (ssh.AuthMethod, error)

AuthWithAgent use already authed user

func AuthWithPrivateKeyBytes

func AuthWithPrivateKeyBytes(key []byte, password string) (ssh.AuthMethod, error)

AuthWithPrivateKeyBytes 直接通过秘钥的bytes

func Dial

func Dial(network, addr string, config *ssh.ClientConfig) (net.Conn, *ssh.Client, error)

func GetAllStats

func GetAllStats(client *Client, stats *Stats, wg *sync.WaitGroup) error

func RegisterSessionGauge

func RegisterSessionGauge(g Gauge)

RegisterSessionGauge call register a gauge listen session num

Types

type CPUInfo

type CPUInfo struct {
	Usage   float32 `json:"usage"`
	User    float32 `json:"user"`
	Nice    float32 `json:"nice"`
	System  float32 `json:"system"`
	Idle    float32 `json:"idle"`
	Iowait  float32 `json:"iowait"`
	Irq     float32 `json:"irq"`
	SoftIrq float32 `json:"soft_irq"`
	Steal   float32 `json:"steal"`
	Guest   float32 `json:"guest"`
}

type Client

type Client struct {
	Info *MachineInfo
	Conf *ClientConfig
	// contains filtered or unexported fields
}

func New

func New(config *ClientConfig) (client *Client, err error)

New 创建SSH client

func (*Client) Chmod

func (c *Client) Chmod(path string) error

func (*Client) Close

func (c *Client) Close() error

func (*Client) CollectTargetMachineInfo

func (c *Client) CollectTargetMachineInfo() error

func (*Client) GetFile

func (c *Client) GetFile(path string) (*sftp.File, error)

func (*Client) GetPwd

func (c *Client) GetPwd() (string, error)

func (*Client) GetRWFile

func (c *Client) GetRWFile(path string) (*sftp.File, error)

GetRWFile 获取可读写的sftp.File 可添加参数控制 write 方式(追加/清空后再追加等等)

func (*Client) GetSSHClient

func (c *Client) GetSSHClient() *ssh.Client

func (*Client) GetSftpClient

func (c *Client) GetSftpClient() *sftp.Client

func (*Client) GetTargetMachineOs

func (c *Client) GetTargetMachineOs() string

func (*Client) IsDir

func (c *Client) IsDir(path string) bool

func (*Client) MkdirAll

func (c *Client) MkdirAll(dirPath string) error

func (*Client) NewPty

func (c *Client) NewPty() (*Session, error)

func (*Client) NewSession

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

func (*Client) NewSessionWithPty

func (c *Client) NewSessionWithPty(cols, rows int) (*Session, error)

func (*Client) NewSftpClient

func (c *Client) NewSftpClient() error

func (*Client) Notify

func (c *Client) Notify(ch chan *Client)

func (*Client) PathExists

func (c *Client) PathExists(path string) bool

func (*Client) Ping

func (c *Client) Ping() error

func (*Client) ReadDir

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

func (*Client) RealPath

func (c *Client) RealPath(path string) (string, error)

func (*Client) Remove

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

func (*Client) RemoveDir

func (c *Client) RemoveDir(remoteDir string) error

func (*Client) SendRequest

func (c *Client) SendRequest(name string, wantReply bool, payload []byte) (bool, []byte, error)

func (*Client) Stat

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

func (*Client) UploadFile

func (c *Client) UploadFile(local, fPath string, fName string) error

func (*Client) UploadFileRaw

func (c *Client) UploadFileRaw(context string, fPath string) error

func (*Client) UploadHttpFile

func (c *Client) UploadHttpFile(fileH *multipart.FileHeader, remote string) error

type ClientConfig

type ClientConfig struct {
	ID         int    `json:"-"`
	Host       string `json:"host"`
	User       string `json:"user"`
	Password   string `json:"password"`
	Passphrase string `json:"passphrase"`
	KeyBytes   []byte `json:"key_bytes"`
	Port       int    `json:"port"`
}

func (*ClientConfig) Serialize

func (h *ClientConfig) Serialize() int64

type FSInfo

type FSInfo struct {
	MountPoint string `json:"mount_point"`
	Used       uint64 `json:"used"`
	Free       uint64 `json:"free"`
}

type Gauge

type Gauge interface {
	Set(float64)
	Inc()
	Dec()
}

type MachineInfo

type MachineInfo struct {
	Goos string
	Arch string
	Cmd  string
}

type NetIntfInfo

type NetIntfInfo struct {
	IPv4 string `json:"ipv4"`
	IPv6 string `json:"ipv6"`
	Rx   uint64 `json:"rx"`
	Tx   uint64 `json:"tx"`
}

type Session

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

func (*Session) Close

func (s *Session) Close() error

func (*Session) Kill

func (s *Session) Kill() error

func (*Session) Output

func (s *Session) Output(cmd string) ([]byte, error)

Output run done and return output

func (*Session) OutputContext

func (s *Session) OutputContext(ctx context.Context, cmd string) ([]byte, error)

OutputContext run done and return output with context

func (*Session) Run

func (s *Session) Run(cmd string) error

func (*Session) RunScript

func (s *Session) RunScript(shell string, sudo bool, fPath string) ([]byte, error)

func (*Session) SetStderr

func (s *Session) SetStderr(stderr io.Writer)

func (*Session) SetStdout

func (s *Session) SetStdout(stdout io.Writer)

func (*Session) Shell

func (s *Session) Shell() error

func (*Session) Start

func (s *Session) Start(cmd string) error

func (*Session) Sudo

func (s *Session) Sudo(cmd, passwd string) ([]byte, error)

func (*Session) SudoContext

func (s *Session) SudoContext(ctx context.Context, cmd, passwd string) ([]byte, error)

func (*Session) Wait

func (s *Session) Wait() error

func (*Session) WindowChange

func (s *Session) WindowChange(h, w int) error

func (*Session) Write

func (s *Session) Write(b []byte) (int, error)

type Stats

type Stats struct {
	Uptime       time.Duration          `json:"uptime"`
	StartUpTime  string                 `json:"start_up_time"`
	Hostname     string                 `json:"hostname"`
	Load1        string                 `json:"load_1"`
	Load5        string                 `json:"load_5"`
	Load10       string                 `json:"load_10"`
	RunningProcs string                 `json:"running_procs"`
	TotalProcs   string                 `json:"total_procs"`
	MemTotal     uint64                 `json:"mem_total"`
	MemFree      uint64                 `json:"mem_free"`
	MemUsage     float32                `json:"mem_usage"`
	MemBuffers   uint64                 `json:"mem_buffers"`
	MemCached    uint64                 `json:"mem_cached"`
	SwapTotal    uint64                 `json:"swap_total"`
	SwapFree     uint64                 `json:"swap_free"`
	SwapUsage    float32                `json:"swap_usage"`
	FSInfos      []FSInfo               `json:"fs_infos"`
	NetIntf      map[string]NetIntfInfo `json:"-"`
	CPU          CPUInfo                `json:"cpu"`
	PreCPU       *cpuRaw                `json:"-"`
}

func NewStatus

func NewStatus() *Stats

Jump to

Keyboard shortcuts

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