linux

package
v0.3.19 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: Apache-2.0 Imports: 22 Imported by: 8

Documentation

Index

Constants

View Source
const (
	DefaultEstimateLineSize = 1024
	MinStartPosition        = 0
)
View Source
const (
	DefaultSplitStr           = constant.CRLFString
	DefaultSuccessReturnValue = constant.ZeroInt
	DefaultFailedReturnValue  = constant.OneInt
	DefaultSSHTimeout         = 10 * time.Second
	DefaultSSHPortNum         = 22
	DefaultSSHUserName        = "root"
	DefaultSSHUserPass        = "root"
	DefaultByteBufferSize     = 1024 * 1024 // 1MB

)

Variables

This section is empty.

Functions

func CompareAddr added in v0.3.17

func CompareAddr(addr1, addr2 string) (int, error)

CompareAddr compares two addresses, if addr1 is equal to addr2, it returns 0 if addr1 is less than addr2, it returns -1 if addr1 is greater than addr2, it returns 1

func CompareIP added in v0.3.17

func CompareIP(ip1, ip2 string) (int, error)

CompareIP compares two ip addresses, if ip1 is equal to ip2, it returns 0 if ip1 is less than ip2, it returns -1 if ip1 is greater than ip2, it returns 1

func ExecuteCommand added in v0.1.4

func ExecuteCommand(command string) (output string, err error)

ExecuteCommand is an alias of ExecuteCommandAndWait

func ExecuteCommandAndWait added in v0.1.20

func ExecuteCommandAndWait(command string) (output string, err error)

ExecuteCommandAndWait executes shell command and wait for it to complete

func ExecuteCommandNoWait added in v0.1.20

func ExecuteCommandNoWait(command string) (output string, err error)

ExecuteCommandNoWait executes shell command and does not wait for it to complete

func FindMountPoint added in v0.3.10

func FindMountPoint(path string) (string, error)

FindMountPoint returns the mount point of the given path, note that this function only works on unix-like system

func GetDefaultIP

func GetDefaultIP() (string, error)

GetDefaultIP gets the default non-local ip, if there are more than one ips, it will return the first one

func GetFileNameDest added in v0.1.4

func GetFileNameDest(fileNameSource, dirDest string) string

GetFileNameDest returns the destination file name

func GetMinAddr added in v0.3.17

func GetMinAddr(addrs []string) (string, error)

GetMinAddr gets the minimum address from the addr list

func GetPathDirMapLocal added in v0.1.5

func GetPathDirMapLocal(dirName, rootPath string) (map[string]string, error)

GetPathDirMapLocal reads all subdirectories and files of given directory and calculate the relative path of rootPath, then map the absolute path of subdirectory names and file names as keys, relative paths as values to fileDirMap

func GetPidFromPidFile added in v0.1.17

func GetPidFromPidFile(pidFile string) (int, error)

GetPidFromPidFile reads pid file and returns pid

func HandleSignals added in v0.3.17

func HandleSignals(pidFile string, stopFuncs ...func() error)

HandleSignals handles operating system signals

func IsDir added in v0.1.4

func IsDir(path string) (isDir bool, err error)

IsDir returns if given path is a directory or not

func IsEmptyDir added in v0.1.4

func IsEmptyDir(dirName string) (isEmpty bool, err error)

IsEmptyDir returns if given directory is empty or not

func IsRunningWithPid added in v0.1.15

func IsRunningWithPid(pid int) (bool, error)

IsRunningWithPid returns if given pid is running

func IsRunningWithPidFile added in v0.1.15

func IsRunningWithPidFile(pidFile string) (bool, error)

IsRunningWithPidFile returns if process of which pid was saved in given pid file is running

func IsValidIP added in v0.3.17

func IsValidIP(ip string) bool

IsValidIP checks if the ip is valid

func KillServer added in v0.1.18

func KillServer(pid int, opts ...string) (err error)

KillServer kills server with given pid, it acts like shell command "kill -9", it will also remove pid file if pid file path is specified as opts

func MatchMountPoint added in v0.3.11

func MatchMountPoint(path string, mountPoints []string) (string, error)

MatchMountPoint matches mount point of given path in the mount point slice, if nothing matched, it returns "/" as default mount point

func PathExists added in v0.1.5

func PathExists(path string) (bool, error)

PathExists returns if given path exists

func Readdir added in v0.1.4

func Readdir(dirName string) (fileInfoList []os.FileInfo, err error)

Readdir returns subdirectories and files of given directory on the remote host, it returns a slice of os.FileInfo

func RemovePidFile added in v0.1.19

func RemovePidFile(pidFile string) error

func SavePid added in v0.1.4

func SavePid(pid int, pidFile string, fileMode os.FileMode) error

SavePid saves pid to pid file with given file mode

func SendSignal added in v0.2.19

func SendSignal(pid int, sig syscall.Signal, opts ...string) (err error)

SendSignal sends signal to given pid, it will also remove pid file if pid file path is specified as opts

func ShutdownServer added in v0.2.19

func ShutdownServer(pid int, opts ...string) (err error)

ShutdownServer kills server with given pid, it acts like shell command "kill -15", it will also remove pid file if pid file path is specified as opts

func SortAddrs added in v0.3.17

func SortAddrs(addrs []string) error

SortAddrs sorts the addr list with quick sort algorithm

func SyscallMode added in v0.1.4

func SyscallMode(fileMode os.FileMode) (fileModeSys uint32)

SyscallMode returns file mode which could be used at syscall

func TailN added in v0.1.4

func TailN(fileName string, n int) (lines []string, err error)

TailN try to get the latest n line of the file.

Types

type SSHConfig added in v0.3.17

type SSHConfig struct {
	HostIp   string
	PortNum  int
	UserName string
	UserPass string
	// contains filtered or unexported fields
}

func NewSSHConfig added in v0.3.17

func NewSSHConfig(hostIP string, portNum int, userName string, userPass string, useSudo bool) *SSHConfig

NewSSHConfig returns a new *SSHConfig

func NewSSHConfigWithDefault added in v0.3.17

func NewSSHConfigWithDefault(hostIP string) *SSHConfig

NewSSHConfigWithDefault returns a new *SSHConfig with default values

func (*SSHConfig) SetUseSudo added in v0.3.17

func (c *SSHConfig) SetUseSudo(useSudo bool)

SetUseSudo sets useSudo

type SSHConn added in v0.3.17

type SSHConn struct {
	Config     *SSHConfig
	SSHClient  *ssh.Client
	SFTPClient *sftp.Client
}

func NewSSHConn added in v0.3.17

func NewSSHConn(hostIP string, portNum int, userName, userPass string, useSudo bool) (*SSHConn, error)

NewSSHConn returns a new *SSHConn

func (*SSHConn) Cat added in v0.3.17

func (conn *SSHConn) Cat(path string) (string, error)

Cat returns the content of the given file on the remote host, it will act like shell command "cat $path"

func (*SSHConn) Chmod added in v0.3.17

func (conn *SSHConn) Chmod(path string, mode string) error

Chmod changes the mode of the given path on the remote host, it will act like shell command "chmod -R $mode $path"

func (*SSHConn) Chown added in v0.3.17

func (conn *SSHConn) Chown(path, user, group string) error

Chown changes the owner and group of the given path on the remote host, it will act like shell command "chown -R $user:$group $path"

func (*SSHConn) Close added in v0.3.17

func (conn *SSHConn) Close() error

Close closes connections with the remote host

func (*SSHConn) Copy added in v0.3.17

func (conn *SSHConn) Copy(src, dest string) error

Copy copies a file or directory on the remote host, it will act like shell command "copy -r $src $dest"

func (*SSHConn) CopyDirFromRemote added in v0.3.17

func (conn *SSHConn) CopyDirFromRemote(dirNameSource, dirNameDest string, tmpDir ...string) error

CopyDirFromRemote copies a directory with all subdirectories and files from remote to local

func (*SSHConn) CopyDirToRemote added in v0.3.17

func (conn *SSHConn) CopyDirToRemote(dirNameSource, dirNameDest string, tmpDir ...string) error

CopyDirToRemote copies a directory with all subdirectories and files from local to remote

func (*SSHConn) CopyFile added in v0.3.17

func (conn *SSHConn) CopyFile(fileSource io.Reader, fileDest io.Writer, bufferSize int) error

CopyFile copy file content from source to destination, it doesn't care about which one is local or remote

func (*SSHConn) CopyFromRemote added in v0.3.17

func (conn *SSHConn) CopyFromRemote(pathSource, pathDest string, tmpDir ...string) (err error)

CopyFromRemote copies no matter a directory or a file from remote to local

func (*SSHConn) CopySingleFileFromRemote added in v0.3.17

func (conn *SSHConn) CopySingleFileFromRemote(fileNameSource string, fileNameDest string, tmpDir ...string) error

CopySingleFileFromRemote copies one single file from remote to local. if tmpDir is not empty and is different with the parent directory of fileNameSource, it will copy the file to tmpDir on the remote host first, then transfer it to the local host, after that, it will remove the temporary file on the remote host automatically. it is your responsibility to assure that tmpDir exists and is a directory, the connection should have enough privilege to the tmpDir and the tmpDir has enough space to hold the file temporarily, note that only the first tmpDir will be used.

func (*SSHConn) CopySingleFileToRemote added in v0.3.17

func (conn *SSHConn) CopySingleFileToRemote(fileNameSource string, fileNameDest string, tmpDir ...string) error

CopySingleFileToRemote copies one single file from local to remote if tmpDir is not empty and is different with the parent directory of fileNameDest, it will transfer the file to tmpDir on the remote host first, then copy it to fileNameDest, after that, it will remove the temporary file on the remote host automatically. it is your responsibility to assure that tmpDir exists and is a directory, the connection should have enough privilege to the tmpDir and the tmpDir has enough space to hold the file temporarily, note that only the first tmpDir will be used.

func (*SSHConn) CopyToRemote added in v0.3.17

func (conn *SSHConn) CopyToRemote(pathSource, pathDest string, tmpDir ...string) (err error)

CopyToRemote copies no matter a directory or a file from local to remote

func (*SSHConn) ExecuteCommand added in v0.3.17

func (conn *SSHConn) ExecuteCommand(cmd string) (string, error)

ExecuteCommand executes shell command on the remote host

func (*SSHConn) ExecuteCommandWithoutOutput added in v0.3.17

func (conn *SSHConn) ExecuteCommandWithoutOutput(cmd string) error

ExecuteCommandWithoutOutput executes a command without output

func (*SSHConn) GetHostName added in v0.3.17

func (conn *SSHConn) GetHostName() (string, error)

GetHostName returns hostname of remote host

func (*SSHConn) GetPathDirMapRemote added in v0.3.17

func (conn *SSHConn) GetPathDirMapRemote(dirName, rootPath string) (map[string]string, error)

GetPathDirMapRemote reads all subdirectories and files of given directory on the remote host and calculate the relative path of rootPath, then map the absolute path of subdirectory names and file names as keys, relative paths as values to fileDirMap

func (*SSHConn) IsDir added in v0.3.17

func (conn *SSHConn) IsDir(path string) (bool, error)

IsDir returns if given path on the remote host is a directory or not

func (*SSHConn) IsEmptyDir added in v0.3.17

func (conn *SSHConn) IsEmptyDir(dirName string) (bool, error)

IsEmptyDir returns if given directory is empty or not on the remote host

func (*SSHConn) ListPath added in v0.3.17

func (conn *SSHConn) ListPath(path string) ([]string, error)

ListPath returns subdirectories and files of given path on the remote host, it returns a slice of sub paths

func (*SSHConn) MkdirAll added in v0.3.17

func (conn *SSHConn) MkdirAll(path string) error

MkdirAll creates a directory named path, along with any necessary parents, on the remote host, it will act like shell command "mkdir -p $path"

func (*SSHConn) Move added in v0.3.17

func (conn *SSHConn) Move(src, dest string) error

Move moves a file or directory on the remote host, it will act like shell command "mv $src $dest"

func (*SSHConn) PathExists added in v0.3.17

func (conn *SSHConn) PathExists(path string) (bool, error)

PathExists returns if given path exists

func (*SSHConn) ReadDir added in v0.3.17

func (conn *SSHConn) ReadDir(dirName string) ([]os.FileInfo, error)

ReadDir returns subdirectories and files of given directory on the remote host, it returns a slice of os.FileInfo

func (*SSHConn) RemoveAll added in v0.3.17

func (conn *SSHConn) RemoveAll(path string) error

RemoveAll removes given path on the remote host, it will act like shell command "rm -rf $path",

func (*SSHConn) SetUseSudo added in v0.3.17

func (conn *SSHConn) SetUseSudo(useSudo bool)

SetUseSudo sets if use sudo or not

func (*SSHConn) Touch added in v0.3.17

func (conn *SSHConn) Touch(path string) error

Touch touches the given path on the remote host, it will act like shell command "touch $path"

Jump to

Keyboard shortcuts

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