sshcommander

package module
v0.0.0-...-b8cef3b Latest Latest
Warning

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

Go to latest
Published: May 22, 2019 License: MIT Imports: 15 Imported by: 0

README

sshcommander (WIP)

Package that provides more options when connecting with ssh via golang

Simple usage example:

package main

import (
	"log"

	"github.com/supermock/sshcommander"
)

func main() {
	term, _ := sshcommander.NewSSHCommander(&sshcommander.Options{
		Host: &sshcommander.Host{
			IP:   "localhost",
			Port: 22,
		},
		Credentials: &sshcommander.Credentials{
			User:     "your-user",
			Password: "******",
		},
		Output: true,
	})

	if err := term.Connect(); err != nil {
		log.Fatal(err)
	}
	defer term.Disconnect()

	if out, err := term.RunCmd("cat /etc/issue"); err != nil {
		log.Fatalf("Failed on execute command. Err: %s", err)
	} else {
		log.Println(out)
	}

	if out, err := term.RunCmd("sudo ip addr"); err != nil {
		log.Fatalf("Failed on execute command. Err: %s", err)
	} else {
		log.Println(out)
	}
}

Documentation

Index

Constants

View Source
const (
	//ErrKeysNotFound | The keys not found on paths
	ErrKeysNotFound = "No keys found in past paths"
	//ErrMakeConnection | Failed create an connection
	ErrMakeConnection = "Failed to make an connection. %s"
	//ErrMakeSession | Failed create an session
	ErrMakeSession = "Failed to make an session. %s"
	//ErrPipeStdIn | Failed to pipe Standard In
	ErrPipeStdIn = "Failed to pipe Standard In. %s"
	//ErrRequestPseudoTerminal | Failed request pseudo terminal
	ErrRequestPseudoTerminal = "Request for pseudo terminal failed. %s"
	//ErrRequestShell | Request for shell failed
	ErrRequestShell = "Request for shell failed. %s"
)

Variables

This section is empty.

Functions

func LoadSSHHostKey

func LoadSSHHostKey(path string) *ssh.HostKeyCallback

LoadSSHHostKey | Load an Host Key and return HostKeyCallBack (Generally is in /etc/ssh/ssh_host_dsa_key)

Types

type Command

type Command struct {
	Cmd string
	Env []string
}

Command | The command for run

func NewSSHCommand

func NewSSHCommand(command string, environments []string) *Command

NewSSHCommand | Make an new instance of SSHCommand

type Credentials

type Credentials struct {
	User     string
	Password string
	Keys     []string
}

Credentials | Has an informations of credentials

type Host

type Host struct {
	IP   string
	Port int
}

Host | Has an informations of host

type Options

type Options struct {
	Host            *Host
	Credentials     *Credentials
	HostKeyCallBack *ssh.HostKeyCallback
	Output          bool
}

Options | Has an informations of options to SSHCommander

type RunError

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

RunError stores the information about an exited remote command as reported by Wait.

func (RunError) Error

func (re RunError) Error() string

func (RunError) ExitStatus

func (re RunError) ExitStatus() int

ExitStatus returns the exit status of the remote command.

func (RunError) Lang

func (re RunError) Lang() string

Lang returns the language tag. See RFC 3066

func (RunError) Msg

func (re RunError) Msg() string

Msg returns the exit message given by the remote command

func (RunError) Signal

func (re RunError) Signal() string

Signal returns the exit signal of the remote command if it was terminated violently.

type SSHCommander

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

SSHCommander | SSHCommander

func NewSSHCommander

func NewSSHCommander(options *Options) (*SSHCommander, error)

NewSSHCommander | Make an new instance of SSHCommander

func (*SSHCommander) Connect

func (sshCommander *SSHCommander) Connect() (err error)

Connect make connection from SSH

func (*SSHCommander) Disconnect

func (sshCommander *SSHCommander) Disconnect() (err error)

Disconnect | Close the SSH connection

func (*SSHCommander) DownloadFile

func (sshCommander *SSHCommander) DownloadFile(remotePath, localPath string) error

DownloadFile | Copy remote file to local machine

func (*SSHCommander) PowerOff

func (sshCommander *SSHCommander) PowerOff() error

PowerOff | PowerOff remote machine

func (*SSHCommander) Reboot

func (sshCommander *SSHCommander) Reboot(retries int) error

Reboot | Restart remote machine and return if connection is ok If retries is 0 retries is infinity.

func (*SSHCommander) Reconnect

func (sshCommander *SSHCommander) Reconnect() error

Reconnect | Close the SSH connection and reopen

func (*SSHCommander) Run

func (sshCommander *SSHCommander) Run(sshCommand *Command) (output string, err error)

Run | Run an command

func (*SSHCommander) RunCmd

func (sshCommander *SSHCommander) RunCmd(command string, args ...interface{}) (string, error)

RunCmd | Alis for Run

func (*SSHCommander) RunEnv

func (sshCommander *SSHCommander) RunEnv(command string, args ...interface{}) func([]string) (string, error)

RunEnv | Alis for Run

func (*SSHCommander) SetOptions

func (sshCommander *SSHCommander) SetOptions(options *Options) error

SetOptions | Change SSHCommander options and reset connection with remote host

func (*SSHCommander) UploadFile

func (sshCommander *SSHCommander) UploadFile(localPath, remotePath string) error

UploadFile | Copy local file to remote machine

func (*SSHCommander) UploadFileInMemory

func (sshCommander *SSHCommander) UploadFileInMemory(size int64, mode os.FileMode, fileName string, contents string, destinationPath string) error

UploadFileInMemory | Copy local memory file to remote machine

Jump to

Keyboard shortcuts

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