sshclient

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

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

Go to latest
Published: Jul 31, 2019 License: MIT Imports: 8 Imported by: 0

README

#+TITLE: simple sshclient with golang
#+OPTIONS: toc:2

This package implemented a ssh client. It can run remote command, execute
remote script, request terminal and request non-interactive shell simply.

* create a ssh client
+ Dial with passwd
#+BEGIN_SRC golang
client, err := DialWithPasswd("host:port", "username", "passwd")
if err != nil {
  handleErr(err)
}
defer client.Close()
#+END_SRC

+ Dial with private key
#+BEGIN_SRC go
client, err := DialWithKey("host:port", "username", "prikeyFile")
if err != nil {
  handleErr(err)
}
defer client.Close()
#+END_SRC

+ Dial with private key and a passphrase to decrypt the key
#+BEGIN_SRC go
client, err := DialWithKeyWithPassphrase("host:port", "username", "prikeyFile", "my-passphrase"))
if err != nil {
  handleErr(err)
}
defer client.Close()
#+END_SRC

+ Dia
#+BEGIN_SRC go
config := &ssh.ClientConfig{
	User: user,
	Auth: []ssh.AuthMethod{
		ssh.Password("yourpasswd"),
	},
}
client, err := Dial("network", "host:port", config)
if err != nil {
  handleErr(err)
}
defer client.Close()
#+END_SRC

* execute commmand
+ Don't care about output, calling Run
#+BEGIN_SRC go
// run one command
if err := client.Cmd("cmd").Run(); err {
  handleErr(err)
}

// run muti command one time
// if there is a command run err, and the next commands will not run
if err := client.Cmd("cmd1").Cmd("cmd2").Cmd("cmd3").Run(); err != nil {
  handleErr(err)
}
#+END_SRC

+ Get output, calling Output
#+BEGIN_SRC go
out, err := client.Cmd("cmd").Output()
if err != nil {
  handleErr(err)
}
fmt.Println(string(out))
#+END_SRC

+ Return stderr message, when execution error, calling SmartOutput
#+BEGIN_SRC go
out, err := client.Cmd("cmd").SmartOutput()
if err != nil {
  // the 'out' is stderr output
  handleErr(err, out)
}
// the 'out' is stdout output
fmt.Println(string(out))
#+END_SRC

+ Write stdout and stderr to your buffer, calling SetStdio
#+BEGIN_SRC go
var (
  stdout bytes.Buffer
  stderr bytes.Buffer
)

if err := client.Cmd("cmd").SetStdio(&stdout, &stderr).Run(); err {
  handleErr(err)
}

// get it
fmt.Println(string(stdout))
fmt.Println(string(stderr))
#+END_SRC

* execute script
+ Run script
#+BEGIN_SRC go
script = `
  statment1
  statment2
`

// It's as same as Cmd
client.Script(script).Run()
client.Script(script).Output()
client.Script(script).SmartOutput()
#+END_SRC

+ Run a shell script file
#+BEGIN_SRC go
client.ScriptFile("/path/to/the/script").Run()
client.ScriptFile("/path/to/the/script").Output()
client.ScriptFile("/path/to/the/script").SmartOutput()
#+END_SRC

* get shell
+ Get a non-interactive shell
#+BEGIN_SRC go
if err := client.Shell().Start(); err != nil {
  handleErr(err)
}
#+END_SRC

+ Get a interactive shell
#+BEGIN_SRC go
// default terminal
if err := client.Terminal(nil).Start(); err != nil {
  handleErr(err)
}

// with a terminal config
config := &sshclient.TerminalConfig {
  Term: "xterm",
  Height: 40,
  Weight: 80,
  Modes: ssh.TerminalModes {
	  ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
	  ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
  }
}
if err := client.Terminal(config).Start(); err != nil {
  handleErr(err)
}
#+END_SRC

+ And sometimes, you could set your stdio buffer
#+BEGIN_SRC go
var (
  stdin  bytes.Buffer
  stdout bytes.Buffer
  stderr bytes.Buffer
)

// Now, it's like client.Script("script").Run()
stdin.NewBufferString("script")
if err := client.Shell().SetStdio(&stdin, &stdout, &stderr).Start(); err != nil {
  handleErr(err)
}

fmt.Println(stdout.String())
fmt.Println(stderr.String())
#+END_SRC

Documentation

Overview

sshclient implements an ssh client

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
}

func Dial

func Dial(network, addr string, config *ssh.ClientConfig) (*Client, error)

Dial starts a client connection to the given SSH server. This is wrap the ssh.Dial

func DialWithKey

func DialWithKey(addr, user, keyfile string) (*Client, error)

DialWithKey starts a client connection to the given SSH server with key authmethod.

func DialWithKeyInline

func DialWithKeyInline(addr string, user string, key []byte) (*Client, error)

DialWithKeyInline starts a client connection to the given SSH server with key authmethod already loaded into a byte array

func DialWithKeyWithPassphrase

func DialWithKeyWithPassphrase(addr, user, keyfile string, passphrase string) (*Client, error)

DialWithKeyWithPassphrase same as DialWithKey but with a passphrase to decrypt the private key

func DialWithPasswd

func DialWithPasswd(addr, user, passwd string) (*Client, error)

DialWithPasswd starts a client connection to the given SSH server with passwd authmethod.

func (*Client) Close

func (c *Client) Close() error

func (*Client) Cmd

func (c *Client) Cmd(cmd string) *remoteScript

Cmd create a command on client

func (*Client) Script

func (c *Client) Script(script string) *remoteScript

Script

func (*Client) ScriptFile

func (c *Client) ScriptFile(fname string) *remoteScript

ScriptFile

func (*Client) Shell

func (c *Client) Shell() *remoteShell

Shell create a noninteractive shell on client.

func (*Client) Terminal

func (c *Client) Terminal(config *TerminalConfig) *remoteShell

Terminal create a interactive shell on client.

type TerminalConfig

type TerminalConfig struct {
	Term   string
	Hight  int
	Weight int
	Modes  ssh.TerminalModes
}

Jump to

Keyboard shortcuts

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