cmd

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecCommand

func ExecCommand(command string, args []string) ([]byte, error)

ExecCommand 执行系统命名

// 执行命令
output, err := ExecCommand("ls", []string{"-la"})
if err != nil {
	fmt.Printf("execute command error: %s", err)
	return
}

// 输出执行结果
fmt.Println(string(output))

func ExecRealtimeCommand

func ExecRealtimeCommand(command string, args []string, outputHandler CommandOutputHandler) error

ExecRealtimeCommand 实时输出命了执行结果

    // 定义一个实时输出函数
	handler := func(output string) {
		fmt.Print(output)
	}
	command := "top"
	args := []string{"-b", "-n", "10"}
	// 执行 top 命令
	if err := cmd.ExecRealtimeCommand(command, args, handler); err != nil {
		fmt.Println(err)
	}

Types

type CommandOutputHandler

type CommandOutputHandler func(output string)

CommandOutputHandler 定义一个函数类型,用于处理命令的实时输出

type RemoteCommander

type RemoteCommander interface {
	RunCommand(cmd string) (string, error)
	SendFile(localFilePath string, remoteDirPath string) error
	GetFile(remoteFilePath string, localDirPath string) error
}

type SSHCommander

type SSHCommander struct {
	Host     string
	User     string
	Password string
}

func (*SSHCommander) GetFile

func (s *SSHCommander) GetFile(remoteFilePath string, localDirPath string) error

GetFile 从远程服务器获取文件到本地

sshCmd := &cmd.SSHCommander{
	Host:     "xxx.xxx.xxx.xxx",
	User:     "user",
	Password: "123456",
}
// 从远程服务器获取文件到本地
remoteFilePath := "/path/to/remote/file"
localDirPath := "/path/to/local/dir"
err = sshCmd.GetFile(remoteFilePath, localDirPath)
if err != nil {
	panic(err)
}
fmt.Printf("Downloaded file %s from remote path %s to local directory %s\n", filepath.Base(remoteFilePath), remoteFilePath, localDirPath)

func (*SSHCommander) RunCommand

func (s *SSHCommander) RunCommand(cmd string) (string, error)

RunCommand 运行远程命令

sshCmd := &cmd.SSHCommander{
	Host:     "xxx.xxx.xxx.xxx",
	User:     "user",
	Password: "123456",
}
// 运行远程命令
out, err := sshCmd.RunCommand("ls -al")
if err != nil {
	panic(err)
}
fmt.Println(out)

func (*SSHCommander) SendFile

func (s *SSHCommander) SendFile(localFilePath string, remoteDirPath string) error

SendFile 发送本地文件到远程服务器

sshCmd := &cmd.SSHCommander{
	Host:     "xxx.xxx.xxx.xxx",
	User:     "user",
	Password: "123456",
}
// 发送本地文件到远程服务器
localFilePath := "logfile.log"
remoteDirPath := "/home/"
err = sshCmd.SendFile(localFilePath, remoteDirPath)
if err != nil {
	panic(err)
}
fmt.Println("File sent successfully!")

type SSHTunnel

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

func NewSSHTunnel

func NewSSHTunnel(remoteHost string, remotePort int, sshConfig *ssh.ClientConfig, localAddr string) (*SSHTunnel, error)

NewSSHTunnel

sshConfig := &ssh.ClientConfig{
	User: "username",
	Auth: []ssh.AuthMethod{
		ssh.Password("password"),
	},
}

tunnel, err := sshutil.NewSSHTunnel("example.com", 80, sshConfig, "127.0.0.1:8080")

if err != nil {
	log.Fatalf("failed to create SSH tunnel: %v", err)
}

defer tunnel.Close()

// 使用隧道转发的本地端口 // ...

func (*SSHTunnel) Close

func (tunnel *SSHTunnel) Close() error

Jump to

Keyboard shortcuts

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