gosshtool

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2020 License: MIT Imports: 16 Imported by: 0

README

gosshtool

Build Status

ssh tool library for Go,gosshtool provide some useful functions for ssh client in golang.implemented using golang.org/x/crypto/ssh.

supports

  • command execution on multiple servers.
  • ssh tunnel local port forwarding.
  • ssh authentication using private keys or password.
  • ssh session timeout support.
  • ssh file upload support.

Installation

go get -u github.com/scottkiss/gosshtool

Examples

command execution on single server
    import "github.com/scottkiss/gosshtool"
		sshconfig := &gosshtool.SSHClientConfig{
			User:     "user",
			Password: "pwd",
			Host:     "11.11.22.22",
		}
		sshclient := gosshtool.NewSSHClient(sshconfig)
		t.Log(sshclient.Host)
		stdout, stderr,session, err := sshclient.Cmd("pwd",nil,nil,0)
		if err != nil {
			t.Error(err)
		}
		t.Log(stdout)
		t.Log(stderr)
command execution on multiple servers
  import "github.com/scottkiss/gosshtool"

	config := &gosshtool.SSHClientConfig{
		User:     "sam",
		Password: "123456",
		Host:     "serverA", //ip:port
	}
	gosshtool.NewSSHClient(config)

	config2 := &gosshtool.SSHClientConfig{
		User:     "sirk",
		Privatekey: "sshprivatekey",
		Host:     "serverB",
	}
	gosshtool.NewSSHClient(config2)
	stdout, _,_, err := gosshtool.ExecuteCmd("pwd", "serverA")
	if err != nil {
		t.Error(err)
	}
	t.Log(stdout)

	stdout, _,_, err = gosshtool.ExecuteCmd("pwd", "serverB")
	if err != nil {
		t.Error(err)
	}
	t.Log(stdout)
ssh tunnel port forwarding

package main

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/scottkiss/gomagic/dbmagic"
	"github.com/scottkiss/gosshtool"
	//"io/ioutil"
	"log"
)

func dbop() {
	ds := new(dbmagic.DataSource)
	ds.Charset = "utf8"
	ds.Host = "127.0.0.1"
	ds.Port = 9999
	ds.DatabaseName = "test"
	ds.User = "root"
	ds.Password = "password"
	dbm, err := dbmagic.Open("mysql", ds)
	if err != nil {
		log.Fatal(err)
	}
	row := dbm.Db.QueryRow("select name from provinces where id=?", 1)
	var name string
	err = row.Scan(&name)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(name)
	dbm.Close()
}

func main() {
	server := new(gosshtool.LocalForwardServer)
	server.LocalBindAddress = ":9999"
	server.RemoteAddress = "remote.com:3306"
	server.SshServerAddress = "112.224.38.111"
	server.SshUserPassword = "passwd"
	//buf, _ := ioutil.ReadFile("/your/home/path/.ssh/id_rsa")
	//server.SshPrivateKey = string(buf)
	server.SshUserName = "sirk"
	server.Start(dbop)
	defer server.Stop()
}

More Examples

  • sshcmd simple ssh command line client.
  • gooverssh port forward server over ssh.

License

View the LICENSE file

Documentation

Index

Constants

View Source
const (
	DEFAULT_CHUNK_SIZE        = 65536
	MIN_CHUNKS                = 10
	THROUGHPUT_SLEEP_INTERVAL = 100
	MIN_THROUGHPUT            = DEFAULT_CHUNK_SIZE * MIN_CHUNKS * (1000 / THROUGHPUT_SLEEP_INTERVAL)
)
View Source
const (
	VINTR         = 1
	VQUIT         = 2
	VERASE        = 3
	VKILL         = 4
	VEOF          = 5
	VEOL          = 6
	VEOL2         = 7
	VSTART        = 8
	VSTOP         = 9
	VSUSP         = 10
	VDSUSP        = 11
	VREPRINT      = 12
	VWERASE       = 13
	VLNEXT        = 14
	VFLUSH        = 15
	VSWTCH        = 16
	VSTATUS       = 17
	VDISCARD      = 18
	IGNPAR        = 30
	PARMRK        = 31
	INPCK         = 32
	ISTRIP        = 33
	INLCR         = 34
	IGNCR         = 35
	ICRNL         = 36
	IUCLC         = 37
	IXON          = 38
	IXANY         = 39
	IXOFF         = 40
	IMAXBEL       = 41
	ISIG          = 50
	ICANON        = 51
	XCASE         = 52
	ECHO          = 53
	ECHOE         = 54
	ECHOK         = 55
	ECHONL        = 56
	NOFLSH        = 57
	TOSTOP        = 58
	IEXTEN        = 59
	ECHOCTL       = 60
	ECHOKE        = 61
	PENDIN        = 62
	OPOST         = 70
	OLCUC         = 71
	ONLCR         = 72
	OCRNL         = 73
	ONOCR         = 74
	ONLRET        = 75
	CS7           = 90
	CS8           = 91
	PARENB        = 92
	PARODD        = 93
	TTY_OP_ISPEED = 128
	TTY_OP_OSPEED = 129
)

POSIX terminal mode flags as listed in RFC 4254 Section 8.

Variables

This section is empty.

Functions

func CopyIOAndUpdateSessionDeadline

func CopyIOAndUpdateSessionDeadline(dst io.Writer, src io.Reader, session *SshSession) (written int64, err error)

func UploadFile

func UploadFile(hostname, sourceFile, targetFile string) (stdout, stderr string, err error)

Types

type ForwardConfig

type ForwardConfig struct {
	LocalBindAddress string
	RemoteAddress    string
	SshServerAddress string
	SshUserName      string
	SshUserPassword  string
	SshPrivateKey    string
}

type LocalForwardServer

type LocalForwardServer struct {
	ForwardConfig
	// contains filtered or unexported fields
}

func (*LocalForwardServer) Start

func (this *LocalForwardServer) Start(call func())

func (*LocalForwardServer) Stop

func (this *LocalForwardServer) Stop()

type PtyInfo

type PtyInfo struct {
	Term  string
	H     int
	W     int
	Modes ssh.TerminalModes
}

type ReadWriteCloser

type ReadWriteCloser interface {
	io.Reader
	io.WriteCloser
}

type SSHClient

type SSHClient struct {
	SSHClientConfig
	// contains filtered or unexported fields
}

func NewSSHClient

func NewSSHClient(config *SSHClientConfig) (client *SSHClient)

func (*SSHClient) Cmd

func (c *SSHClient) Cmd(cmd string, sn *SshSession, deadline *time.Time, idleTimeout int) (output, errput string, currentSession *SshSession, err error)

func (*SSHClient) Connect

func (c *SSHClient) Connect() (conn *ssh.Client, err error)

func (*SSHClient) Pipe

func (c *SSHClient) Pipe(rw ReadWriteCloser, pty *PtyInfo, deadline *time.Time, idleTimeout int) (currentSession *SshSession, err error)

func (*SSHClient) TransferData

func (c *SSHClient) TransferData(target string, data []byte) (stdout, stderr string, err error)

type SSHClientConfig

type SSHClientConfig struct {
	Host              string
	User              string
	Password          string
	Privatekey        string
	DialTimeoutSecond int
	MaxDataThroughput uint64
}

type SshSession

type SshSession struct {
	Stdout *bytes.Buffer
	Stderr *bytes.Buffer
	// contains filtered or unexported fields
}

func ExecuteCmd

func ExecuteCmd(cmd, hostname string) (output, errput string, currentSession *SshSession, err error)

func NewSession

func NewSession(conn *ssh.Client, deadline *time.Time, idleTimeout int) (ss *SshSession, err error)

func NewSessionWithChannel

func NewSessionWithChannel(conn *ssh.Client, ch ssh.Channel, deadline *time.Time, idleTimeout int) (ss *SshSession, err error)

func (*SshSession) Close

func (sc *SshSession) Close() error

func (*SshSession) RequestPty

func (sc *SshSession) RequestPty(term string, h int, w int, termmodes ssh.TerminalModes) (err error)

func (*SshSession) Run

func (sc *SshSession) Run(cmd string) (err error)

func (*SshSession) SetDeadline

func (sc *SshSession) SetDeadline(deadline *time.Time)

func (*SshSession) Shell

func (sc *SshSession) Shell() error

func (*SshSession) StderrPipe

func (sc *SshSession) StderrPipe() (io.Reader, error)

func (*SshSession) StdinPipe

func (sc *SshSession) StdinPipe() (io.WriteCloser, error)

func (*SshSession) StdoutPipe

func (sc *SshSession) StdoutPipe() (io.Reader, error)

func (*SshSession) Wait

func (sc *SshSession) Wait() error

type Tunnel

type Tunnel struct {
	Client *ssh.Client
}

type UUID

type UUID [16]byte

func FromStr

func FromStr(s string) (id UUID, err error)

func MustFromStr

func MustFromStr(s string) UUID

func Rand

func Rand() UUID

Rand generates a new version 4 UUID.

func (UUID) Hex

func (this UUID) Hex() string

Hex returns a hex string representation of the UUID in xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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