gossh

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2020 License: Apache-2.0 Imports: 31 Imported by: 1

README

gossh

Travis CI Software License GoDoc Coverage Status goreport

execute shell scripts among multiple ssh servers

Usage demo

$ gossh --quoteReplace=%q --bangReplace=%b --hosts="192.168.1.1:8022 app/app" --cmds="%host MYSQL_PWD='%babcdefg' mysql -h127.0.0.1 -uroot -e %qshow variables like 'server%'%q"

--- 192.168.1.1:8022 ---
$ MYSQL_PWD='!abcdefg' mysql -h127.0.0.1 -uroot -e "show variables like 'server%'"
+----------------+--------------------------------------+
| Variable_name  | Value                                |
+----------------+--------------------------------------+
| server_id      | 1                                    |
| server_id_bits | 32                                   |
| server_uuid    | 43e9cbe5-b38a-11e9-8570-04d4c439354e |
+----------------+--------------------------------------+
gossh -h="192.168.1.(9 18):8022 app/app id=(9 18)" --cmds="%host-9 MYSQL_PWD='\!abcdefg' mysql -u root -h 127.0.0.1 -vvv -e 'show slave status\G'"
gossh -h="192.168.1.9:8022 app/app id=9, 192.168.1.18:8022 app/app id=18" --cmds="%host-9 %ul ~/go/bin/linux_amd64/mci ./mci,%host-9 ./mci/mci -v"
gossh -h="192.168.1.9:8022 app/app id=9, 192.168.1.18:8022 app/app id=18" --cmds="%host-9 %dl ./mci/mci ."

proxy supported:

gossh --hosts="192.168.1.3:6022 huangjinbing/123 id=1, 192.168.9.1:22 user proxy=1" --cmds="%host-2 %dl 1.log 10.log"
gossh --hosts="192.168.1.3:6022 huangjinbing/123 id=1, 192.168.9.1:22 user proxy=1" --cmds="%host-2 cat 1.log"
gossh --hostsFile ~/hosts.txt --cmdsFile ~/cmds.txt --user root --pass "{PBE}H3y5VaKfj-vxSJ5JUHL0R-CBtZTkR2UR"
# hosts.txt
13.26.15.12:(1061-1063)
13.26.15.13:222
13.26.15.14
# cmds.txt
%host pwd
%host hostname -I

Substitute ResultVars

  1. define result variables like ... => @varName
  2. use the variables like echo @varName

Notice:

  1. @varName which is not capitalized will be limited to the host related.
  2. @VarName which is capitalized will be global to all hosts.
#printConfig = false
#passphrase="xxxx"

hosts = [
"12.26.85.62:1082 root/111",
"12.26.85.62:1083 root/222",
"12.26.85.62:1084 root/333",
]

# 全部命令都默认成远程执行,相当于自动添加了%host标识。
globalRemote = true
cmdTimeout = "15s"

cmds = [
    "date '+%Y%m%d' => @Today",
    "sh /tmp/hostdailycheck.sh",
    "%dl /tmp/log/HostDailyCheck-*-@Today.txt ./dailychecks@today/",
]
$  uuidgen
6F948925-429E-4D2C-B551-C9C6D12E5062
$  gossh --pbe hello,word -p C9C6D12E5062
+---+-------+-----------------------------+
| # | PLAIN | ENCRYPTED                   |
+---+-------+-----------------------------+
| 1 | hello | {PBE}eiRMlsZPLikVYpZMcHicyg |
| 2 | word  | {PBE}lAHH0UfuqZ0YtV_5VE77uw |
+---+-------+-----------------------------+
$  gossh --pbe hello,word -p C9C6D12E5062
+---+-------+-----------------------------+
| # | PLAIN | ENCRYPTED                   |
+---+-------+-----------------------------+
| 1 | hello | {PBE}6RGab13x5WfzFP0NpA_suA |
| 2 | word  | {PBE}qmPJAysHSmnfQEK-a6JM0A |
+---+-------+-----------------------------+

$  gossh --ebp 6RGab13x5WfzFP0NpA_suA,qmPJAysHSmnfQEK-a6JM0A -p C9C6D12E5062
+---+------------------------+-------+
| # | ENCRYPTED              | PLAIN |
+---+------------------------+-------+
| 1 | 6RGab13x5WfzFP0NpA_suA | hello |
| 2 | qmPJAysHSmnfQEK-a6JM0A | word  |
+---+------------------------+-------+
$  gossh --ebp {PBE}eiRMlsZPLikVYpZMcHicyg,{PBE}lAHH0UfuqZ0YtV_5VE77uw -p C9C6D12E5062
+---+-----------------------------+-------+
| # | ENCRYPTED                   | PLAIN |
+---+-----------------------------+-------+
| 1 | {PBE}eiRMlsZPLikVYpZMcHicyg | hello |
| 2 | {PBE}lAHH0UfuqZ0YtV_5VE77uw | word  |
+---+-----------------------------+-------+

resources

  1. Bootstrap
  2. Bootstrap 4 Password Show Hide
  3. Tables
  4. Golang SSH Client: Multiple Commands, Crypto & Goexpect Examples
  5. bramvdbogaerde/go-scp
  6. golang 批量scp 远程传输文件
  7. PBEWithMD5AndDES in go
  8. like python-sh, for easy call shell with golang
  9. A scp client library written in Go. The remote server must have the scp command
  10. How the SCP protocol works
  11. Golang SFTP Client: Download File, Upload File Example
  12. package sftp
  13. sftp/example_test.go
  14. Golang Client Examples
  15. go语言使用sftp包上传文件和文件夹到远程服务器
  16. Implements support for double star (**) matches in golang's path.Match and filepath.Glob.
  17. easyssh-proxy provides a simple implementation of some SSH protocol features in Go
  18. List selection type alternative ssh/scp/sftp client. Pure Go.
  19. A library to handle ssh easily with Golang.It can do multiple proxy, x11 forwarding, etc., go-sshlib doc
  20. An auditing / logging SSH relay for a jump box / bastion host.
  21. A curated list of SSH resources.
  22. melbahja/goph The native golang ssh client to execute your commands over ssh connection
  23. yahoo/vssh Go library to handle tens of thousands SSH connections and execute the command(s) with higher-level API for building network device / server automation.

Documentation

Index

Constants

View Source
const (
	// ExecModeCmdByCmd means execute a command in all relative hosts and then continue to next command
	// eg. cmd1: host1,host2, cmd2:host1, host2
	ExecModeCmdByCmd int = iota
	// ExecModeHostByHost means execute a host relative commands and the continue to next host.
	// eg .host1: cmd1,cmd2, host2:cmd1, cmd2
	ExecModeHostByHost
)

Variables

View Source
var LocalHost = &Host{ID: "localhost", Addr: "localhost", resultVars: make(map[string]string)}

LocalHost means the local host. nolint:gochecknoglobals

Functions

func ExecInHosts added in v1.0.0

func ExecInHosts(gs *GoSSH, target *Host, hostsCmd HostsCmd, stdout io.Writer) error

ExecInHosts execute in specified hosts.

func GetLastLine added in v1.0.0

func GetLastLine(s string) (preLines, curLine string)

GetLastLine gets the last line of s.

func IsCapitalized added in v1.0.1

func IsCapitalized(str string) bool

IsCapitalized test a string is a capitalized one.

func StripAnsi added in v1.0.1

func StripAnsi(str string) string

StripAnsi strips the cursor, clears, and save positions escape code.

Types

type CmdChanClosed added in v1.0.0

type CmdChanClosed struct{}

CmdChanClosed represents the cmd channel closed event.

type CmdExcResult

type CmdExcResult struct {
}

CmdExcResult means the detail exec result of cmd.

type CmdWrap added in v1.0.1

type CmdWrap struct {
	Cmd       string
	ResultVar string
}

CmdWrap wraps a command with result variable name.

type Config

type Config struct {
	ReplaceQuote string `pflag:"replace for quote(\"). shorthand=q"`
	ReplaceBang  string `pflag:"replace for bang(!). shorthand=b"`

	Separator  string `pflag:"separator for hosts, cmds, default comma. shorthand=s"`
	NetTimeout string `pflag:"timeout(eg. 15s, 3m), empty for no timeout."`
	CmdTimeout string `pflag:"timeout(eg. 15s, 3m), default 15m."`

	SplitSSH    bool `pflag:"split ssh commands by comma or not. shorthand=S"`
	PrintConfig bool `pflag:"print config before running. shorthand=P"`
	// 是否全局设置为远程shell命令
	GlobalRemote bool `pflag:"run as global remote ssh command(no need %host). shorthand=g"`
	Confirm      bool `pflag:"conform to continue."`
	FirstConfirm bool

	Passphrase string   `pflag:"passphrase for decrypt {PBE}Password. shorthand=p"`
	Hosts      []string `pflag:"hosts. shorthand=H"`
	Cmds       []string `pflag:"commands to be executedChan. shorthand=C"`

	User      string `pflag:"user. shorthand=u"`
	Pass      string `pflag:"pass."`
	HostsFile string `pflag:"hosts file. shorthand=f"`
	CmdsFile  string `pflag:"cmds file."`

	ExecMode int `pflag:"exec mode(0: cmd by cmd, 1 host by host). shorthand=e"`
}

Config represents the structure of input toml file structure.

func (Config) GetSeparator added in v1.0.0

func (c Config) GetSeparator() string

GetSeparator get the separator.

func (*Config) Parse

func (c *Config) Parse() GoSSH

Parse parses the flags or cnf files to GoSSH.

type DlCmd added in v0.2.0

type DlCmd struct {
	UlDl
}

DlCmd download cmd structure.

func (*DlCmd) Exec added in v1.0.0

func (s *DlCmd) Exec(gs *GoSSH, h *Host, stdout io.Writer) error

Exec execute in specified host.

type EchoState added in v1.0.1

type EchoState int

EchoState 回显状态.

const (
	// EchoStateInit 初始化,未知
	EchoStateInit EchoState = iota
	// EchoStateSent 已发送
	EchoStateSent
	// EchoStateFound 服务器回显
	EchoStateFound
	// EchoStateNotFound 服务器没有回显
	EchoStateNotFound
)

type GoSSH

type GoSSH struct {
	Vars  *Config
	Hosts Hosts

	Cmds []HostsCmd
}

GoSSH defines the structure of the whole cfg context.

func (*GoSSH) Close added in v0.1.1

func (g *GoSSH) Close() error

Close closes gossh.

type Host

type Host struct {
	ID         string
	Addr       string
	User       string
	Password   string // empty when using public key
	Properties map[string]string

	Proxy *Host
	// contains filtered or unexported fields
}

Host represents the structure of remote host information for ssh.

func (*Host) Close added in v1.0.0

func (h *Host) Close() error

Close closes the resource associated to the host.

func (*Host) GetGosshConnect added in v1.0.0

func (h *Host) GetGosshConnect() (*gossh.Connect, error)

GetGosshConnect get gossh Connect.

func (*Host) GetSftpClient added in v1.0.0

func (h *Host) GetSftpClient() (*sftp.Client, error)

GetSftpClient get sftClient by host.

func (*Host) IsConnected added in v1.0.1

func (h *Host) IsConnected() bool

IsConnected tells if host is connected by ssh or sftp.

func (Host) PrintSCP added in v1.0.0

func (h Host) PrintSCP()

PrintSCP prints sshpass scp commands.

func (*Host) PrintSSH added in v1.0.0

func (h *Host) PrintSSH()

PrintSSH prints sshpass ssh commands.

func (*Host) Prop added in v1.0.0

func (h *Host) Prop(name string) string

Prop finds property by name.

func (*Host) SSH added in v1.0.0

func (h *Host) SSH(cmds []string, resultVar string, stdout io.Writer) error

SSH executes ssh commands on remote host h. http://networkbit.ch/golang-ssh-client/

func (*Host) SetResultVar added in v1.0.1

func (h *Host) SetResultVar(varName, varValue string)

SetResultVar sets the value of result variable.

func (*Host) SubstituteResultVars added in v1.0.1

func (h *Host) SubstituteResultVars(cmd string) string

SubstituteResultVars substitutes the variables in the command line string.

type Hosts added in v1.0.0

type Hosts []*Host

Hosts stands for slice of Host.

func (Hosts) Close added in v1.0.0

func (hosts Hosts) Close() error

Close closes all the host related resources.

func (Hosts) FixHostID added in v1.0.0

func (hosts Hosts) FixHostID()

FixHostID fix the host ID by sequence if it is blank.

func (Hosts) FixProxy added in v1.0.0

func (hosts Hosts) FixProxy()

FixProxy fix proxy.

func (Hosts) PrintSCP added in v1.0.0

func (hosts Hosts) PrintSCP()

PrintSCP prints sshpass scp commands for all hosts.

func (Hosts) PrintSSH added in v1.0.0

func (hosts Hosts) PrintSSH()

PrintSSH prints sshpass ssh commands for all hosts.

type HostsCmd added in v1.0.0

type HostsCmd interface {
	// Parse parses the command.
	Parse()
	// Exec execute in specified host.
	Exec(gs *GoSSH, host *Host, stdout io.Writer) error
	// TargetHosts returns target hosts for the command
	TargetHosts() Hosts
}

HostsCmd means the executable interface.

type LocalCmd

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

LocalCmd means local commands.

func (*LocalCmd) Exec added in v1.0.0

func (l *LocalCmd) Exec(_ *GoSSH, h *Host, stdout io.Writer) error

Exec execute in specified host. nolint:nestif

func (*LocalCmd) Parse

func (l *LocalCmd) Parse()

Parse parses the local cmd.

func (LocalCmd) RawCmd added in v1.0.0

func (l LocalCmd) RawCmd() string

RawCmd returns the original raw command.

func (LocalCmd) TargetHosts added in v1.0.0

func (LocalCmd) TargetHosts() Hosts

TargetHosts returns target hosts for the command.

type SSHCmd

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

SSHCmd means SSH command.

func (*SSHCmd) Exec added in v1.0.0

func (s *SSHCmd) Exec(gs *GoSSH, h *Host, stdout io.Writer) error

Exec execute in specified host.

func (*SSHCmd) Parse

func (*SSHCmd) Parse()

Parse parses command.

func (*SSHCmd) TargetHosts added in v1.0.0

func (s *SSHCmd) TargetHosts() Hosts

TargetHosts returns target hosts for the command.

type UlCmd added in v0.2.0

type UlCmd struct {
	UlDl
	// contains filtered or unexported fields
}

UlCmd upload cmd structure.

func (*UlCmd) Exec added in v1.0.0

func (s *UlCmd) Exec(gs *GoSSH, h *Host, stdout io.Writer) error

Exec execute in specified host.

type UlDl added in v0.2.0

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

UlDl scp...

func (*UlDl) Parse added in v0.2.0

func (*UlDl) Parse()

Parse parses UlCmd.

func (*UlDl) TargetHosts added in v1.0.0

func (u *UlDl) TargetHosts() Hosts

TargetHosts returns target hosts for the command.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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