operator

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2020 License: MIT Imports: 16 Imported by: 0

README

operator

A simple library for executing commands and scripts on a remote host via SSH.

go get github.com/jsiebens/operator

Example

This example uploads a given script to a remote host and executes it:

package main

import (
	"flag"
	"fmt"
	"github.com/jsiebens/operator"
	"math/rand"
	"os"
	"time"
)

var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))

func main() {

	user := flag.String("user", "root", "Username for SSH login (default \"root\")")
	host := flag.String("host", "", "Target host")
	port := flag.Int("port", 22, "The port on which to connect for ssh (default 22)")
	pwd := flag.String("password", "", "The password to use for remote login (optional)")
	privateKey := flag.String("private-key", "", "The ssh key to use for remote login (optional)")
	script := flag.String("script", "", "The script to executed on the remote host")

	flag.Parse()

	if *host == "" || *script == "" {
		flag.PrintDefaults()
		os.Exit(1)
	}

	callback := func(op operator.CommandOperator) error {
		remoteFile := fmt.Sprintf("/tmp/script.%d", seededRand.Int())
		err := op.UploadFile(*script, remoteFile, "0755")
		if err != nil {
			return err
		}
		op.Execute(remoteFile)
		return nil
	}

	var err error
	if *privateKey != "" {
		err = operator.ExecuteRemoteWithPrivateKey(*host, *port, *user, *privateKey, callback)
	} else if *pwd != "" {
		err = operator.ExecuteRemoteWithPassword(*host, *port, *user, *pwd, callback)
	} else {
		err = operator.ExecuteRemote(*host, *port, *user, callback)
	}

	if err != nil {
		fmt.Printf("error: %s \n\n", err)
	}
}

Contributing

Commits must be signed off with git commit -s

License: MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecuteLocal

func ExecuteLocal(callback Callback) error

func ExecuteRemote

func ExecuteRemote(host string, port int, user string, callback Callback) error

func ExecuteRemoteWithPassword

func ExecuteRemoteWithPassword(host string, port int, user string, password string, callback Callback) error

func ExecuteRemoteWithPrivateKey

func ExecuteRemoteWithPrivateKey(host string, port int, user string, privateKey string, callback Callback) error

Types

type Callback

type Callback func(CommandOperator) error

type CommandOperator

type CommandOperator interface {
	Execute(command string) (CommandRes, error)
	Upload(src io.Reader, remotePath string, mode string) error
	UploadFile(path string, remotePath string, mode string) error
}

type CommandRes

type CommandRes struct {
	StdOut []byte
	StdErr []byte
}

type LocalOperator

type LocalOperator struct {
}

func NewLocalOperator

func NewLocalOperator() *LocalOperator

func (LocalOperator) Execute

func (e LocalOperator) Execute(command string) (CommandRes, error)

func (LocalOperator) Upload

func (e LocalOperator) Upload(source io.Reader, remotePath string, mode string) error

func (LocalOperator) UploadFile

func (e LocalOperator) UploadFile(path string, remotePath string, mode string) error

type SSHOperator

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

func NewSSHOperator

func NewSSHOperator(address string, config *ssh.ClientConfig) (*SSHOperator, error)

func (SSHOperator) Close

func (s SSHOperator) Close() error

func (SSHOperator) Execute

func (s SSHOperator) Execute(command string) (CommandRes, error)

func (SSHOperator) Upload

func (s SSHOperator) Upload(source io.Reader, remotePath string, mode string) error

func (SSHOperator) UploadFile

func (s SSHOperator) UploadFile(path string, remotePath string, mode string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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