ssh

package
v0.0.0-...-ed45c2d Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2019 License: MIT Imports: 5 Imported by: 0

README

SSH Client

SSH client execute command on remote host.

Use password to execute

t := &ssh.Task{
    Username: "ubuntu",
    AuthMethods: []ssh.AuthMethod{
        {
            Type:    ssh.AuthByPassword,
            Content: "pass",
        },
    },
    Host: "ip:22",
    Script: `
ls /
`,
}
result, err := t.Execute()
if err != nil {
    panic(err)
}

fmt.Println(result)

Use ssh command line

Sometimes you're not able to ssh the remote host by password or public key but only able to ssh by ssh client in terminal, so there's another way to create a process calling the shell command ssh.

t := &ssh.Task{
    Username: "ubuntu",
    UseSSHCommand: true,
    Host: "10.0.0.11",
    Script: `
ls /
`,
}
result, err := t.Execute()
if err != nil {
    panic(err)
}

fmt.Println(result)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthMethod

type AuthMethod struct {
	Type    AuthType `json:"type"`
	Content string   `json:"content"`
}

AuthMethod is the authentication method.

type AuthType

type AuthType int
const (
	// AuthByPassword indicates using password to authenticate.
	AuthByPassword AuthType = iota + 1
	// AuthByPublicKey indicates using public key to authenticate.
	AuthByPublicKey
)

type Task

type Task struct {
	// Username is the username used to login the remote server.
	Username string `json:"username"`
	// AuthMethods includes the authentication methods used to login server.
	AuthMethods []AuthMethod `json:"auth_methods"`
	// Host indicates the remote server host.
	Host string `json:"host"`
	// Port indicates the remote server ssh port, default is 22.
	Port int `json:"port"`
	// Script is the shell command.
	Script string `json:"script"`
	// Timeout indicates the max command running time.
	Timeout time.Duration `json:"timeout"`
	// UseSSHCommand uses shell command to execute commands on remote machines.
	UseSSHCommand bool `json:"use_ssh_command"`
	// SSHCommandPath indicates the file location of the SSH command.
	SSHCommandPath string `json:"ssh_command_path"`
}

Task wraps the remote SSH task.

func (*Task) Execute

func (t *Task) Execute() (string, error)

Execute the SSH task on remote host.

Jump to

Keyboard shortcuts

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