ssh

package module
v0.0.0-...-19d5405 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2020 License: MIT Imports: 11 Imported by: 3

README

go-ssh-client

This is a little pacakge helps you run command on remote host via SSH

CI Go Report Card Go Doc asciicast

package main

import (
  "log"
  "os"

  ssh "github.com/metrue/go-ssh-client"
)

func main() {
  host := "127.0.0.1"
  script := `
x=1
while [ $x -le 5 ]; do
  echo 'hello'
  x=$(( $x + 1 ))
  sleep 1
done
`
  err := ssh.New(host).
    WithUser("root").
    WithKey("/your/path/to/id_ras").  // Default is ~/.ssh/id_rsa
    WithPort("2222").    // Default is 22
    RunCommand(script, ssh.CommandOptions{
      Stdout: os.Stdout,
      Stderr: os.Stderr,
      Stdin:  os.Stdin,
    })
  if err != nil {
    log.Fatal(err)
  }
}

Test

$ make start_ssh_server
$ make test
$ make clean #clean up running Docker container

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client ssh client

func New

func New(server string) Client

New create a client

func (Client) Connectable

func (c Client) Connectable(timeout time.Duration) (bool, error)

Connectable check if client can connect to ssh server within timeout

func (Client) RunCommand

func (c Client) RunCommand(command string, options CommandOptions) error

RunCommand run command onto remote server via SSH

func (Client) WithKey

func (c Client) WithKey(keyfile string) Client

WithKey with key

func (Client) WithPassword

func (c Client) WithPassword(password string) Client

WithPassword with key

func (Client) WithPort

func (c Client) WithPort(port string) Client

WithPort with port

func (Client) WithServer

func (c Client) WithServer(addr string) Client

WithServer with server

func (Client) WithUser

func (c Client) WithUser(user string) Client

WithUser with key

type Clienter

type Clienter interface {
	WithServer(add string) Client
	WithUser(user string) Client
	WithPassword(password string) Client
	WithKey(key string) Client
	WithPort(port string) Client
	Connectable(timeout time.Duration) (bool, error)
	RunCommand(command string, options CommandOptions) error
}

Clienter defines interface of SSH client

type CommandOptions

type CommandOptions struct {
	Stdin   io.Reader
	Stdout  io.Writer
	Stderr  io.Writer
	Timeout time.Duration
	Env     []string
}

CommandOptions options for command

Directories

Path Synopsis
Package mock_ssh is a generated GoMock package.
Package mock_ssh is a generated GoMock package.

Jump to

Keyboard shortcuts

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