gossh

package
v0.0.0-...-b425644 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: Apache-2.0, Apache-2.0 Imports: 15 Imported by: 0

README

gossh

Golang ssh library

Example

package main

import (
  "github.com/dynport/gossh"
  "log"
)

// returns a function of type gossh.Writer func(...interface{})
// MakeLogger just adds a prefix (DEBUG, INFO, ERROR)
func MakeLogger(prefix string) gossh.Writer {
  return func(args ...interface{}) {
    log.Println((append([]interface{}{prefix}, args...))...)
  }
}

func main() {
  client := gossh.New("some.host", "user")
  // my default agent authentication is used. use
  // client.SetPassword("<secret>")
  // for password authentication
  client.DebugWriter = MakeLogger("DEBUG")
  client.InfoWriter = MakeLogger("INFO ")
  client.ErrorWriter = MakeLogger("ERROR")

  defer client.Close()
  rsp, e := client.Execute("uptime")
  if e != nil {
    client.ErrorWriter(e.Error())
  }
  client.InfoWriter(rsp.String())

  rsp, e = client.Execute("echo -n $(cat /proc/loadavg); cat /does/not/exists")
  if e != nil {
    client.ErrorWriter(e.Error())
    client.ErrorWriter("STDOUT: " + rsp.Stdout())
    client.ErrorWriter("STDERR: " + rsp.Stderr())
  }
}

Prints this result:

2013/08/25 00:31:40 DEBUG connecting some.host
2013/08/25 00:31:41 INFO  [EXEC  ] uptime
2013/08/25 00:31:41 DEBUG 22:31:41 up 375 days, 10:44,  0 users,  load average: 0.09, 0.13, 0.22
2013/08/25 00:31:41 INFO  => 0.944143
2013/08/25 00:31:41 INFO  map[stdout:72 bytes stderr:0 bytes runtime:0.944202 status:0]
2013/08/25 00:31:41 DEBUG already connected
2013/08/25 00:31:41 INFO  [EXEC  ] echo -n $(cat /proc/loadavg); cat /does/not/exists
2013/08/25 00:31:41 DEBUG 0.09 0.13 0.22 1/455 23396
2013/08/25 00:31:41 ERROR cat: /does/not/exists
2013/08/25 00:31:41 ERROR : No such file or directory
2013/08/25 00:31:41 INFO  => 0.067075
2013/08/25 00:31:41 ERROR Process exited with: 1. Reason was:  ()
2013/08/25 00:31:41 ERROR STDOUT: 0.09 0.13 0.22 1/455 23396
2013/08/25 00:31:41 ERROR STDERR: cat: /does/not/exists: No such file or directory

Tunnelling HTTP Connections

For services not bound to the public interface of a machine, tunnelling is a quite nice SSH feature. It allows to use a remote service like it is running at the local machine. This concept is used in the HTTP client returned by the NewHttpClient function. It is a common net/http.Client, but all requests are sent through the SSH connection given.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHttpClient

func NewHttpClient(sshClient *Client) (httpClient *http.Client, e error)

Returns an HTTP client that sends all requests through the SSH connection (aka tunnelling).

Types

type Client

type Client struct {
	User  string
	Host  string
	Port  int
	Agent net.Conn

	Conn        *ssh.Client
	DebugWriter Writer
	ErrorWriter Writer
	InfoWriter  Writer
	// contains filtered or unexported fields
}

func New

func New(host, user string) (c *Client)

func (*Client) Attach

func (client *Client) Attach() error

func (*Client) Close

func (c *Client) Close()

func (*Client) Connect

func (c *Client) Connect() (e error)

func (*Client) ConnectWhenNotConnected

func (c *Client) ConnectWhenNotConnected() (e error)

func (*Client) Connection

func (c *Client) Connection() (*ssh.Client, error)

func (*Client) Debug

func (c *Client) Debug(args ...interface{})

func (*Client) Error

func (c *Client) Error(args ...interface{})

func (*Client) Execute

func (c *Client) Execute(s string) (r *Result, e error)

func (*Client) Info

func (c *Client) Info(args ...interface{})

func (*Client) Password

func (c *Client) Password(user string) (password string, e error)

func (*Client) SetPassword

func (c *Client) SetPassword(password string)

func (*Client) Write

func (c *Client) Write(writer Writer, args []interface{})

func (*Client) WriteFile

func (c *Client) WriteFile(path, content, owner string, mode int) (res *Result, e error)

func (*Client) WriteFileCommand

func (c *Client) WriteFileCommand(path, content, owner string, mode int) string

type Config

type Config struct {
	Host     string
	User     string
	Port     int
	Password string
}

func (*Config) Connection

func (c *Config) Connection() (*ssh.Client, error)

type LogWriter

type LogWriter struct {
	LogTo  func(n ...interface{})
	Buffer bytes.Buffer
}

func (*LogWriter) String

func (w *LogWriter) String() (s string)

func (*LogWriter) Write

func (w *LogWriter) Write(b []byte) (i int, e error)

type Result

type Result struct {
	StdoutBuffer, StderrBuffer *LogWriter
	Runtime                    time.Duration
	Error                      error
	ExitStatus                 int
}

func (*Result) Stderr

func (r *Result) Stderr() string

func (*Result) Stdout

func (r *Result) Stdout() string

func (*Result) String

func (r *Result) String() (out string)

func (*Result) Success

func (self *Result) Success() bool

type Writer

type Writer func(...interface{})

Jump to

Keyboard shortcuts

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