gossh

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

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 28 Imported by: 1

README

gossh

execute shell scripts among multiple ssh servers

Features

  1. Support global variables defined in the host definition, example @ARCH in hosts, at 2021-11-30
    # gossh -c xx.toml --group 4
    
    #printConfig = true
    #passphrase="xxxx"
    
    hosts = [
    "admin:{PBE}xxx@192.168.1.2 group=4 @ARCH=amd64", # ARM 测试编译机
    ]
    
    # 全部命令都默认成远程执行,相当于自动添加了%host标识。
    globalRemote = true
    cmdTimeout = "300s"
    # confirm = true
    # exec mode(0: cmd by cmd, 1 host by host).
    execMode = 0
    
    cmds = [
    "%local rm -fr git.commit && make git.commit",
    "%local find ./ -name \".DS_Store\" -exec rm -rf '{}' ';'",
    "%local rm -fr vendor && go mod download && go mod vendor",
    "%local cd .. && rm -fr xyz.tar.gz && tar --exclude .git --exclude .idea -czf xyz.tar.gz xyz",
    "%ul xyz.tar.gz xyzsrc/",
    "cd xyzsrc",
    "[[ -d xyz ]] && rm -fr xyz",
    "tar zxf xyz.tar.gz --warning=no-unknown-keyword --warning=no-timestamp --exclude .git && cd xyz",
    "make install",
    "%local cd xyz && pwd",
    "%local date '+%Y%m%d%H%M%S' => @Now",
    "%dl xyzsrc/xyz/build/xyz build/xyz_@ARCH_@Now",
    "%local pwd && ls -lhd build/xyz_@ARCH_@Now",
    "%local upx build/xyz_@ARCH_@Now && ls -lh build/xyz_@ARCH_@Now",
    "%local rm -fr vendor/",
    ]
    

Usage demo

repl mode
gossh -H "user:pass@aa.co" -H "user:pass@bb.co" --repl
log file /Users/bingoobjca/.gossh/logs/.-20211228140231.log created
>>> date

---> @aa.co:22 <---
Last login: Tue Dec 28 14:29:43 2021 from 60.247.93.190
[user@VM-24-15-centos ~]$ date
Tue Dec 28 14:31:52 CST 2021

---> bb.co:22 <---
Last login: Tue Dec 28 14:26:23 2021 from 192.168.225.11
[user@CS1 ~]# date
2021年 12月 28日 星期二 14:28:28 CST

>>> hostname

---> @aa.co:22 <---
[user@VM-24-15-centos ~]$ hostname
VM-24-15-centos

---> bb.co:22 <---
[user@CS1 ~]# hostname
CS1
>>> %local date

---> localhost <---
$ date
2021年12月28日 星期二 14时32分25秒 CST
>>> exit
log file /Users/bingoobjca/.gossh/logs/.-20211228140231.log recorded
$ 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

Config examples

demo configuration of proxy
hosts = [
    "12.26.85.0:22 user/pass id=0",
    "12.26.85.1:22 root/na id=1 proxy=0", # proxy by id=0
    "12.26.85.2:22 root/na id=2 proxy=0",
    "12.26.85.3:22 root/na id=3 proxy=0",
]

cmds = [
    # execute on hosts whose id between 1 and 3
    "%host-(1-3) hostname -I",
]
demo configuration of host group example
# group.toml
hosts = [
    # if no group specified, a group names default will be set.
    "12.26.85.0:22 user/pass group=proxy",
    "12.26.85.1:22 root/na proxy=0 group=g1/g3", # proxy by id=0
    "12.26.85.2:22 root/na proxy=0 group=g2/g3",
    "12.26.85.3:22 root/na proxy=0 group=g1/g2",
]


globalRemote = true

cmds = [
    "hostname -I",
]

cli commands:

  1. gossh -c group.toml --group=g1
  2. gossh -c group.toml --group=g2
  3. gossh -c group.toml --group=g3

demo configuration of tags

# tags.toml
hosts = [
    "12.26.85.0:22 user/pass",
    "12.26.85.1:22 user/pass",
    "12.26.85.2:22 user/pass",
    "12.26.85.3:22 user/pass",
]

globalRemote = true

# gossh -c tags.toml
cmds = [
    "echo bingoohuang",
]

# gossh -c tags.toml --tag=hostname
hostname-cmds = [
    "hostname -I",
]

# gossh -c tags.toml --tag=date
date-cmds = [
    "date",
]

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. A statically-linked ssh server with a reverse connection feature for simple yet powerful remote access. Most useful during HackTheBox challenges, CTFs or similar. hackthebox是一个非常不错的在线实验平台,能帮助你提升渗透测试技能和黑盒测试技能,平台上有很多靶机,从易到难,各个级别的靶机都有。https://cloud.tencent.com/developer/article/1596548
  2. Rospo is a tool meant to create reliable ssh tunnels. It embeds an ssh server too if you want to reverse proxy a secured shell
  3. Stack Up is a simple deployment tool that performs given set of commands on multiple hosts in parallel. It reads Supfile, a YAML, which defines networks (groups of hosts), commands and targets.
  4. Bootstrap
  5. Bootstrap 4 Password Show Hide
  6. Tables
  7. Golang SSH Client: Multiple Commands, Crypto & Goexpect Examples
  8. bramvdbogaerde/go-scp
  9. golang 批量scp 远程传输文件
  10. PBEWithMD5AndDES in go
  11. like python-sh, for easy call shell with golang
  12. A scp client library written in Go. The remote server must have the scp command
  13. How the SCP protocol works
  14. Golang SFTP Client: Download File, Upload File Example
  15. package sftp
  16. sftp/example_test.go
  17. Golang Client Examples
  18. go语言使用sftp包上传文件和文件夹到远程服务器
  19. Implements support for double star (**) matches in golang's path.Match and filepath.Glob.
  20. easyssh-proxy provides a simple implementation of some SSH protocol features in Go
  21. List selection type alternative ssh/scp/sftp client. Pure Go.
  22. A library to handle ssh easily with Golang.It can do multiple proxy, x11 forwarding, etc. , go-sshlib doc
  23. An auditing / logging SSH relay for a jump box / bastion host.
  24. A curated list of SSH resources.
  25. melbahja/goph The native golang ssh client to execute your commands over ssh connection
  26. 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.

Functions

func DecryptPassphrase

func DecryptPassphrase(passphrase string)

func ExecCmds

func ExecCmds(gs *GoSSH, host *Host, stdout io.Writer, eo ExecOption, hostGroup string)

ExecCmds executes commands.

func ExecInHosts

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

ExecInHosts execute in specified hosts.

func ExecuteInitialCmd

func ExecuteInitialCmd(initialCmd string, w io.Writer)

ExecuteInitialCmd executes initial command.

func GetLastLine

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

GetLastLine gets the last line of s.

func IsCapitalized

func IsCapitalized(str string) bool

IsCapitalized test a string is a capitalized one.

func Repl

func Repl(gs *GoSSH, hosts []*Host, stdout io.Writer, hostGroup string)

Repl execute in specified hosts.

func StripAnsi

func StripAnsi(str string) string

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

func StripAnsiBytes

func StripAnsiBytes(b []byte) []byte

StripAnsi removes all ANSI Escape Sequences from the byteslice

Types

type AnsiStripper

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

func NewStripAnsiWriter

func NewStripAnsiWriter(w io.WriteCloser) *AnsiStripper

func (*AnsiStripper) Write

func (w *AnsiStripper) Write(data []byte) (int, error)

type CmdChanClosed

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

type CmdWrap struct {
	Cmd       string
	ResultVar string

	ExecOption
}

CmdWrap wraps a command with result variable name.

func (CmdWrap) String

func (c CmdWrap) String() string

type Config

type Config struct {
	ReplaceQuote string `help:"replace for quote" short:"q"`
	ReplaceBang  string `help:"replace for bang(!)" short:"b"`

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

	Group    string `help:"group name"`
	CmdsFile string `help:"cmds file"`

	HostsFile string `help:"hosts file" short:"f"`
	Pass      string `help:"pass."`

	User string `help:"user" short:"u"`

	Passphrase string   `help:"passphrase for decrypt {PBE}Password" short:"p"`
	Cmds       []string `help:"commands to be executedChan" short:"C"`

	Hosts []string `help:"hosts" short:"H"`

	ExecMode int `help:"exec mode(0: cmd by cmd, 1 host by host)" short:"e"`

	FirstConfirm bool

	Confirm bool `help:"conform to continue."`
	// 是否全局设置为远程shell命令
	GlobalRemote bool `help:"run as global remote ssh command(no need %host)" short:"g"`
	PrintConfig  bool `help:"print config before running" short:"P"`

	SplitSSH bool `help:"split ssh commands by comma or not" short:"S"`
}

Config represents the structure of input toml file structure.

func (Config) GetSeparator

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

type DlCmd struct {
	UlDl
}

DlCmd download cmd structure.

func (*DlCmd) Exec

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

Exec execute in specified host.

type EchoState

type EchoState int

EchoState 回显状态.

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

type ExecOption

type ExecOption struct {
	Repl bool
}

ExecOption defines the options of execute.

type Extra

type Extra struct {
	Dur time.Duration
}

func (*Extra) DoExtra

func (e *Extra) DoExtra()

type GoSSH

type GoSSH struct {
	Config *Config
	Hosts  Hosts
	Cmds   []HostsCmd
}

GoSSH defines the structure of the whole cfg context.

func (*GoSSH) Close

func (g *GoSSH) Close() error

Close closes gossh.

type Host

type Host struct {
	Properties map[string][]string

	Proxy *Host

	Password string // empty when using public key
	Addr     string
	User     string
	ID       string
	// contains filtered or unexported fields
}

Host represents the structure of remote host information for ssh.

func NewExecModeCmdByCmd

func NewExecModeCmdByCmd() *Host

NewExecModeCmdByCmd creates an exec mode command.

func (*Host) Close

func (h *Host) Close() error

Close closes the resource associated to the host.

func (*Host) GetGosshConnect

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

GetGosshConnect get gossh Connect.

func (*Host) GetSftpClient

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

GetSftpClient get sftClient by host.

func (*Host) IsConnected

func (h *Host) IsConnected() bool

IsConnected tells if host is connected by ssh or sftp.

func (*Host) IsExecModeCmdByCmd

func (h *Host) IsExecModeCmdByCmd() bool

IsExecModeCmdByCmd tests if this is mode of cmd one by one or not.

func (Host) PrintSCP

func (h Host) PrintSCP()

PrintSCP prints sshpass scp commands.

func (*Host) PrintSSH

func (h *Host) PrintSSH()

PrintSSH prints sshpass ssh commands.

func (*Host) Prop

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

Prop finds property by name.

func (*Host) SSH

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

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

func (*Host) SetResultVar

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

SetResultVar sets the value of result variable.

func (*Host) SubstituteResultVars

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

SubstituteResultVars substitutes the variables in the command line string.

type Hosts

type Hosts []*Host

Hosts stands for slice of Host.

func (Hosts) Close

func (hosts Hosts) Close() error

Close closes all the host related resources.

func (Hosts) FixHost

func (hosts Hosts) FixHost()

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

func (Hosts) FixProxy

func (hosts Hosts) FixProxy()

FixProxy fix proxy.

func (Hosts) PrintSCP

func (hosts Hosts) PrintSCP()

PrintSCP prints sshpass scp commands for all hosts.

func (Hosts) PrintSSH

func (hosts Hosts) PrintSSH()

PrintSSH prints sshpass ssh commands for all hosts.

type HostsCmd

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

HostsCmd means the executable interface.

type LocalCmd

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

LocalCmd means local commands.

func (*LocalCmd) Exec

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

Exec execute in specified host.

func (LocalCmd) RawCmd

func (l LocalCmd) RawCmd() string

RawCmd returns the original raw command.

func (LocalCmd) TargetHosts

func (LocalCmd) TargetHosts(string) 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

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

Exec execute in specified host.

func (*SSHCmd) TargetHosts

func (s *SSHCmd) TargetHosts(hostGroup string) Hosts

TargetHosts returns target hosts for the command.

type UlCmd

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

UlCmd upload cmd structure.

func (*UlCmd) Exec

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

Exec execute in specified host.

type UlDl

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

UlDl scp...

func (*UlDl) TargetHosts

func (u *UlDl) TargetHosts(hostGroup string) Hosts

TargetHosts returns target hosts for the command.

Directories

Path Synopsis
pkg
brg

Jump to

Keyboard shortcuts

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