From 87e207932fcab1f25d843752b25057d3272519f5 Mon Sep 17 00:00:00 2001 From: Feng_Qi Date: Fri, 19 Jan 2018 17:54:08 +0800 Subject: [PATCH] version 0.2 --- .gitignore | 3 +- README.MD | 298 +++++++++++++++++++++++++++++++++----------- cmd2.txt.example | 6 +- funcs/ssh_test.go | 79 ++++++++++++ funcs/sshconnect.go | 200 +++++++++++++++++++++++++++++ cfg.go => g/cfg.go | 40 +++++- g/const.go | 9 ++ host.txt.example | 4 +- ip.txt.example | 3 +- main.go | 136 ++++++++++---------- ssh.json.example | 22 ++-- ssh_test.go | 53 -------- sshconnect.go | 97 -------------- 13 files changed, 636 insertions(+), 314 deletions(-) create mode 100644 funcs/ssh_test.go create mode 100644 funcs/sshconnect.go rename cfg.go => g/cfg.go (87%) create mode 100644 g/const.go delete mode 100644 ssh_test.go delete mode 100644 sshconnect.go diff --git a/.gitignore b/.gitignore index 2cc6534..98e9d73 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.txt *.json -*.exe \ No newline at end of file +*.exe +*.key \ No newline at end of file diff --git a/README.MD b/README.MD index 74a5c0c..cd7243b 100644 --- a/README.MD +++ b/README.MD @@ -2,6 +2,15 @@ 一个简单的并行 SSH 工具,可以批量的对主机通过 SSH 执行命令组合。 +支持: +- 并发执行 +- 单次执行多条命令 +- ip 地址段自动匹配主机(192.168.0.1-192.168.0.100) +- ssh 用户名/密码认证 +- ssh key 认证 +- json 格式输出 +- 输出到文本,文件名为 host.txt + #### 编译 ``` go get ./... @@ -17,33 +26,39 @@ https://github.com/shanghai-edu/multissh/releases/ #### 命令体系 ``` -./multissh -h - -cmd string - cmds // 需要执行的命令组合,多条命令以 ; 分割 +# ./multissh -h +Usage of ./multissh: + -c string + cfg File Path + -ciphers string + ciphers -cmdfile string - cmdfile path //需要执行的命令组合文件,文件内命令按行分割 + cmdfile path + -cmds string + cmds -hostfile string - hostfile path // 需要执行的主机列表文件,主机列表在文件内按行分割 + hostfile path -hosts string - host address list //需要执行的主机列表,多个主机以 ; 分割 + host address list -ipfile string - hostfile path //需要执行的主机(IP)列表文件,IP可以以地址段的方式逐行写在文本内 + hostfile path + -j print output in json format + -k string + ssh private key + -l In linux mode,multi command combine with && ,such as date&&cd /opt&&ls (default true) + -n int + max execute number (default 20) + -outTxt + write result into txt -p string - password // 主机的 SSH 密码 + password -port int - ssh port (default 22) //主机的 SSH 端口,默认 22 - -u string - username //主机的 SSH 用户名 - -j string - jsonFile //保存大量主机,包括主机地址,SSH用户名,SSH密码,SSH端口,所需执行的cmd指令文件地址 - -outTxt bool - outTxt (default false) //是否允许把结果保存到文件中,文件名为 ssh 连接的主机名(host 或 ip),true为允许 false为默认值 + ssh port (default 22) -t int - timeLimit (default 30) //单个 ssh 会话的最大时间,超过时间命令未执行完则超时 默认为30s - -n int - numLimit (default 20) //最大并发访问量 默认为20 + max timeout (default 30) + -u string + username -v show version - ``` **cmdfile 示例** ``` @@ -51,12 +66,13 @@ show clock ``` **hostfile 示例** ``` -192.168.15.101 +192.168.31.21 192.168.15.102 ``` **ipfile 示例** ``` -192.168.15.101-192.168.15.102 +192.168.15.101-192.168.15.103 +192.168.31.21-192.168.31.22 ``` **ssh.json 示例** @@ -64,17 +80,19 @@ show clock { "SshHosts": [ { - "Host": "192.168.15.101", - "Port": 22, - "Username": "admin", - "Password": "admin", - "CmdFile": "cmd1.txt.example" - }, + "Host": "192.168.31.51", + "Port": 22, + "Username": "admin", + "Password": "admin", + "cmds":"show clock;show clock" + }, { - "Host": "192.168.83.40", - "Port": 22, - "Username": "root", - "Password": "root", + "Host": "192.168.80.131", + "Port": 22, + "Username": "root", + "Password": "", + "key": "./server.key", + "linuxMode": true, "CmdFile": "cmd2.txt.example" } ] @@ -84,78 +102,206 @@ show clock ## 用法 #### cmd string & host string ``` -./multissh -cmd "show clock" -hosts "192.168.15.101;192.168.15.102" -u admin -p admin +# ./multissh -cmds "show clock" -hosts "192.168.31.21;192.168.15.102" -u admin -p password +2018/01/17 14:01:28 Multissh start +2018/01/17 14:01:31 Multissh finished. Process time 2.867808673s. Number of active ip is 2 +host: 192.168.31.21 +========= Result ========= -192.168.15.101 ssh start -sw-1#show clock -05:26:40.649 UTC Tue Jun 6 2017 -sw-1#exit +****************************************************************************** +* Copyright (c) 2004-2016 Hangzhou H3C Tech. Co., Ltd. All rights reserved. * +* Without the owner's prior written consent, * +* no decompiling or reverse-engineering shall be allowed. * +****************************************************************************** -192.168.15.101 ssh end +show clock +14:01:31 CN Wed 01/17/2018 +Time Zone : CN add 08:00:00 +exit -192.168.15.102 ssh start -sw-2#show clock -05:24:38.708 UTC Tue Jun 6 2017 -sw-2#exit +host: 192.168.15.102 +========= Result ========= + +sw-cisco#show clock +05:50:24.935 UTC Wed Jan 17 2018 +sw-cisco#exit -192.168.15.102 ssh end ``` #### cmdfile & hostfile ``` -./multissh -cmdfile cmd.txt -hostfile host.txt -u admin -p admin +# ./multissh -cmdfile cmd1.txt.example -hostfile host.txt.example -u admin -p password +2018/01/17 14:01:28 Multissh start +2018/01/17 14:01:31 Multissh finished. Process time 2.867808673s. Number of active ip is 2 +host: 192.168.31.21 +========= Result ========= + +****************************************************************************** +* Copyright (c) 2004-2016 Hangzhou H3C Tech. Co., Ltd. All rights reserved. * +* Without the owner's prior written consent, * +* no decompiling or reverse-engineering shall be allowed. * +****************************************************************************** -192.168.15.101 ssh start -sw-1#show clock -05:29:43.269 UTC Tue Jun 6 2017 -sw-1#exit +show clock +14:01:31 CN Wed 01/17/2018 +Time Zone : CN add 08:00:00 +exit -192.168.15.101 ssh end +host: 192.168.15.102 +========= Result ========= -192.168.15.102 ssh start -sw-2#show clock -05:27:41.332 UTC Tue Jun 6 2017 -sw-2#exit +sw-cisco#show clock +05:50:24.935 UTC Wed Jan 17 2018 +sw-cisco#exit -192.168.15.102 ssh end ``` #### ipfile ``` -./multissh -cmdfile cmd.txt -ipfile ip.txt -u admin -p admin +# ./multissh -cmdfile cmd1.txt.example -ipfile ip.txt.example -u admin -p password +2018/01/17 14:25:26 Multissh start +2018/01/17 14:25:29 Multissh finished. Process time 2.847347642s. Number of active ip is 5 +host: 192.168.15.101 +========= Result ========= + +sw-cisco-1#show clock +06:17:49.422 UTC Wed Jan 17 2018 +sw-cisco-1#exit + +host: 192.168.15.102 +========= Result ========= +sw-cisco-2#show clock +06:14:22.445 UTC Wed Jan 17 2018 +sw-cisco-2#exit + +host: 192.168.15.103 +========= Result ========= +sw-cisco-3#show clock +06:19:14.487 UTC Wed Jan 17 2018 +sw-cisco-3#exit + +host: 192.168.31.21 +========= Result ========= + +****************************************************************************** +* Copyright (c) 2004-2016 Hangzhou H3C Tech. Co., Ltd. All rights reserved. * +* Without the owner's prior written consent, * +* no decompiling or reverse-engineering shall be allowed. * +****************************************************************************** + +show clock +14:25:29 CN Wed 01/17/2018 +Time Zone : CN add 08:00:00 +exit + +host: 192.168.31.22 +========= Result ========= + +sw-cisco-4#show clock +14:25:27.639 beijing Wed Jan 17 2018 +sw-cisco-4#exit +``` +#### ssh key-based Auth and linuxMode +``` +# ./multissh -hosts "192.168.80.131" -cmds "date;cd /opt;ls" -u root -k "server.key" +2018/01/17 14:33:55 Multissh start +2018/01/17 14:33:56 Multissh finished. Process time 960.367764ms. Number of active ip is 1 +host: 192.168.80.131 +========= Result ========= +Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-98-generic x86_64) -192.168.15.101 ssh start -sw-1#show clock -05:29:43.269 UTC Tue Jun 6 2017 -sw-1#exit + * Documentation: https://help.ubuntu.com + * Management: https://landscape.canonical.com + * Support: https://ubuntu.com/advantage -192.168.15.101 ssh end + System information as of Wed Jan 17 14:33:55 CST 2018 -192.168.15.102 ssh start -sw-2#show clock -05:27:41.332 UTC Tue Jun 6 2017 -sw-2#exit + System load: 0.0 Processes: 335 + Usage of /: 10.0% of 90.18GB Users logged in: 0 + Memory usage: 2% IP address for eth0: 192.168.80.131 + Swap usage: 0% IP address for docker0: 172.17.0.1 + + Graph this data and manage this system at: + https://landscape.canonical.com/ + +0 个可升级软件包。 +0 个安全更新。 + +New release '17.10' available. +Run 'do-release-upgrade' to upgrade to it. + +You have new mail. +Last login: Wed Jan 17 14:29:39 2018 from 202.120.80.201 +root@ubuntu-docker-node3:~# 201817:33:56 CST +root@ubuntu-docker-node3:~# root@ubuntu-docker-node3:/opt# cisco +composer.json +composer.phar +example-oauth2-server +getting-started-with-mmdb +gitlab +gitlab-ce_8.0.4-ce.1_amd64.deb +oauth2-demo-php +oauth2-server-php +python_test +rsyslog-maxminddb +root@ubuntu-docker-node3:/opt# 注销 + +# ./multissh -hosts "192.168.80.131" -cmds "date;cd /opt;ls" -u root -k "server.key" -l +2018/01/17 14:34:02 Multissh start +2018/01/17 14:34:02 Multissh finished. Process time 842.465643ms. Number of active ip is 1 +host: 192.168.80.131 +========= Result ========= +201817:34:02 CST +cisco +composer.json +composer.phar +example-oauth2-server +getting-started-with-mmdb +gitlab +gitlab-ce_8.0.4-ce.1_amd64.deb +oauth2-demo-php +oauth2-server-php +python_test +rsyslog-maxminddb -192.168.15.102 ssh end ``` #### ssh.json ``` -./multissh -j ssh.json -t 30 -n 20 -outTxt -192.168.15.101 ssh start -sw-1#show clock -05:29:43.269 UTC Tue Jun 6 2017 -sw-1#exit +./multissh -c ssh.json.example +2018/01/17 14:29:38 Multissh start +2018/01/17 14:29:41 Multissh finished. Process time 2.922928532s. Number of active ip is 2 +host: 192.168.31.51 +========= Result ========= + +****************************************************************************** +* Copyright (c) 2004-2016 Hangzhou H3C Tech. Co., Ltd. All rights reserved. * +* Without the owner's prior written consent, * +* no decompiling or reverse-engineering shall be allowed. * +****************************************************************************** -192.168.15.101 ssh end +show clock +14:29:41 CN Wed 01/17/2018 +Time Zone : CN add 08:00:00 +show clock +14:29:41 CN Wed 01/17/2018 +Time Zone : CN add 08:00:00 +exit -192.168.83.40 ssh start -2017年 06月 09日 星期五 09:33:11 CST -2017年 06月 09日 星期五 09:33:14 CST -192.168.83.40 ssh end +host: 192.168.80.131 +========= Result ========= +cisco +composer.json +composer.phar +example-oauth2-server +getting-started-with-mmdb +gitlab +gitlab-ce_8.0.4-ce.1_amd64.deb +oauth2-demo-php +oauth2-server-php +python_test +rsyslog-maxminddb ``` -#### TODO -增加使用证书认证的支持 #### LICENSE Apache License 2.0 \ No newline at end of file diff --git a/cmd2.txt.example b/cmd2.txt.example index 341974a..71c5190 100644 --- a/cmd2.txt.example +++ b/cmd2.txt.example @@ -1,3 +1,3 @@ -date -sleep 3 -date \ No newline at end of file +cd /opt +sleep 2 +ls \ No newline at end of file diff --git a/funcs/ssh_test.go b/funcs/ssh_test.go new file mode 100644 index 0000000..a868590 --- /dev/null +++ b/funcs/ssh_test.go @@ -0,0 +1,79 @@ +package funcs + +import ( + "bytes" + // "os" + //"strings" + "testing" +) + +const ( + username = "root" + password = "" + ip = "192.168.80.131" + port = 22 + cmd = "cd /opt;pwd;exit" + key = "../server.key" +) + +/* +func Test_SSH(t *testing.T) { + var cipherList []string + session, err := connect(username, password, ip, key, port, cipherList) + if err != nil { + t.Error(err) + return + } + defer session.Close() + + cmdlist := strings.Split(cmd, ";") + stdinBuf, err := session.StdinPipe() + if err != nil { + t.Error(err) + return + } + + var outbt, errbt bytes.Buffer + session.Stdout = &outbt + + session.Stderr = &errbt + err = session.Shell() + if err != nil { + t.Error(err) + return + } + for _, c := range cmdlist { + c = c + "\n" + stdinBuf.Write([]byte(c)) + + } + session.Wait() + t.Log((outbt.String() + errbt.String())) + return +} +*/ + +func Test_SSH_run(t *testing.T) { + var cipherList []string + session, err := connect(username, password, ip, key, port, cipherList) + if err != nil { + t.Error(err) + return + } + defer session.Close() + + //cmdlist := strings.Split(cmd, ";") + //newcmd := strings.Join(cmdlist, "&&") + var outbt, errbt bytes.Buffer + session.Stdout = &outbt + + session.Stderr = &errbt + err = session.Run(cmd) + if err != nil { + t.Error(err) + return + } + t.Log((outbt.String() + errbt.String())) + + return +} diff --git a/funcs/sshconnect.go b/funcs/sshconnect.go new file mode 100644 index 0000000..afb985f --- /dev/null +++ b/funcs/sshconnect.go @@ -0,0 +1,200 @@ +package funcs + +import ( + "bytes" + "fmt" + "io/ioutil" + + "net" + "strconv" + "strings" + "time" + + "github.com/shanghai-edu/multissh/g" + "golang.org/x/crypto/ssh" +) + +func connect(user, password, host, key string, port int, cipherList []string) (*ssh.Session, error) { + var ( + auth []ssh.AuthMethod + addr string + clientConfig *ssh.ClientConfig + client *ssh.Client + config ssh.Config + session *ssh.Session + err error + ) + // get auth method + auth = make([]ssh.AuthMethod, 0) + if key == "" { + auth = append(auth, ssh.Password(password)) + } else { + pemBytes, err := ioutil.ReadFile(key) + if err != nil { + return nil, err + } + + var signer ssh.Signer + if password == "" { + signer, err = ssh.ParsePrivateKey(pemBytes) + } else { + signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(password)) + } + if err != nil { + return nil, err + } + auth = append(auth, ssh.PublicKeys(signer)) + } + + if len(cipherList) == 0 { + config = ssh.Config{ + Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"}, + } + } else { + config = ssh.Config{ + Ciphers: cipherList, + } + } + + clientConfig = &ssh.ClientConfig{ + User: user, + Auth: auth, + Timeout: 30 * time.Second, + Config: config, + HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { + return nil + }, + } + + // connet to ssh + addr = fmt.Sprintf("%s:%d", host, port) + + if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil { + return nil, err + } + + // create session + if session, err = client.NewSession(); err != nil { + return nil, err + } + + modes := ssh.TerminalModes{ + ssh.ECHO: 0, // disable echoing + ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud + ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud + } + + if err := session.RequestPty("xterm", 80, 40, modes); err != nil { + return nil, err + } + + return session, nil +} + +func Dossh(username, password, host, key string, cmdlist []string, port, timeout int, cipherList []string, linuxMode bool, ch chan g.SSHResult) { + chSSH := make(chan g.SSHResult) + if linuxMode { + go dossh_run(username, password, host, key, cmdlist, port, cipherList, chSSH) + } else { + go dossh_session(username, password, host, key, cmdlist, port, cipherList, chSSH) + } + var res g.SSHResult + + select { + case <-time.After(time.Duration(timeout) * time.Second): + res.Host = host + res.Success = false + res.Result = ("SSH run timeout:" + strconv.Itoa(timeout) + " second.") + ch <- res + case res = <-chSSH: + ch <- res + } + return +} + +func dossh_session(username, password, host, key string, cmdlist []string, port int, cipherList []string, ch chan g.SSHResult) { + session, err := connect(username, password, host, key, port, cipherList) + var sshResult g.SSHResult + sshResult.Host = host + + if err != nil { + sshResult.Success = false + sshResult.Result = fmt.Sprintf("<%s>", err.Error()) + ch <- sshResult + return + } + defer session.Close() + + cmdlist = append(cmdlist, "exit") + + stdinBuf, _ := session.StdinPipe() + + var outbt, errbt bytes.Buffer + session.Stdout = &outbt + + session.Stderr = &errbt + err = session.Shell() + if err != nil { + sshResult.Success = false + sshResult.Result = fmt.Sprintf("<%s>", err.Error()) + ch <- sshResult + return + } + for _, c := range cmdlist { + c = c + "\n" + stdinBuf.Write([]byte(c)) + } + session.Wait() + if errbt.String() != "" { + sshResult.Success = false + sshResult.Result = errbt.String() + ch <- sshResult + } else { + sshResult.Success = true + sshResult.Result = outbt.String() + ch <- sshResult + } + + return +} + +func dossh_run(username, password, host, key string, cmdlist []string, port int, cipherList []string, ch chan g.SSHResult) { + session, err := connect(username, password, host, key, port, cipherList) + var sshResult g.SSHResult + sshResult.Host = host + + if err != nil { + sshResult.Success = false + sshResult.Result = fmt.Sprintf("<%s>", err.Error()) + ch <- sshResult + return + } + defer session.Close() + + cmdlist = append(cmdlist, "exit") + newcmd := strings.Join(cmdlist, "&&") + + var outbt, errbt bytes.Buffer + session.Stdout = &outbt + + session.Stderr = &errbt + err = session.Run(newcmd) + if err != nil { + sshResult.Success = false + sshResult.Result = fmt.Sprintf("<%s>", err.Error()) + ch <- sshResult + return + } + + if errbt.String() != "" { + sshResult.Success = false + sshResult.Result = errbt.String() + ch <- sshResult + } else { + sshResult.Success = true + sshResult.Result = outbt.String() + ch <- sshResult + } + + return +} diff --git a/cfg.go b/g/cfg.go similarity index 87% rename from cfg.go rename to g/cfg.go index 7aa4d88..2a9ed96 100644 --- a/cfg.go +++ b/g/cfg.go @@ -1,4 +1,4 @@ -package main +package g import ( "bufio" @@ -12,6 +12,38 @@ import ( "strings" ) +type SSHHost struct { + Host string + Port int + Username string + Password string + CmdFile string + Cmds string + CmdList []string + Key string + LinuxMode bool + Result SSHResult +} + +type HostJson struct { + SshHosts []SSHHost +} + +type SSHResult struct { + Host string + Success bool + Result string +} + +func SplitString(str string) (strList []string) { + if strings.Contains(str, ",") { + strList = strings.Split(str, ",") + } else { + strList = strings.Split(str, ";") + } + return +} + func GetfileAll(filePath string) ([]byte, error) { result, err := ioutil.ReadFile(filePath) if err != nil { @@ -52,8 +84,8 @@ func GetJsonFile(filePath string) ([]SSHHost, error) { result = m.SshHosts return result, nil } -func WriteIntoTxt(sshHost SSHHost) error { - outputFile, outputError := os.OpenFile(sshHost.Host+".txt", os.O_WRONLY|os.O_CREATE, 0666) +func WriteIntoTxt(sshResult SSHResult) error { + outputFile, outputError := os.OpenFile(sshResult.Host+".txt", os.O_WRONLY|os.O_CREATE, 0666) if outputError != nil { return outputError } @@ -62,7 +94,7 @@ func WriteIntoTxt(sshHost SSHHost) error { outputWriter := bufio.NewWriter(outputFile) //var outputString string - outputString := sshHost.Result + outputString := sshResult.Result outputWriter.WriteString(outputString) outputWriter.Flush() return nil diff --git a/g/const.go b/g/const.go new file mode 100644 index 0000000..ecbb867 --- /dev/null +++ b/g/const.go @@ -0,0 +1,9 @@ +package g + +// changelog: +// 0.1 fisrt version +// 0.1.2 fix ssh error on h3c switch +// 0.2 +const ( + VERSION = "0.2" +) diff --git a/host.txt.example b/host.txt.example index 024af64..097f8b4 100644 --- a/host.txt.example +++ b/host.txt.example @@ -1,2 +1,2 @@ -192.168.15.101 -192.168.15.110 \ No newline at end of file +192.168.31.21 +192.168.15.102 \ No newline at end of file diff --git a/ip.txt.example b/ip.txt.example index a99ff44..746458e 100644 --- a/ip.txt.example +++ b/ip.txt.example @@ -1 +1,2 @@ -192.168.15.101-192.168.15.110 \ No newline at end of file +192.168.15.101-192.168.15.110 +192.168.31.21-192.168.31.22 \ No newline at end of file diff --git a/main.go b/main.go index 8390fb6..4a16c95 100644 --- a/main.go +++ b/main.go @@ -1,65 +1,52 @@ package main import ( + "encoding/json" "flag" "fmt" "log" "os" - "strconv" - "strings" + "time" - // "github.com/bitly/go-simplejson" -) -const ( - VERSION = "0.1.2" + "github.com/shanghai-edu/multissh/funcs" + "github.com/shanghai-edu/multissh/g" ) -type SSHHost struct { - Host string - Port int - Username string - Password string - CmdFile string - Cmd []string - Result string -} -type HostJson struct { - SshHosts []SSHHost -} - func main() { version := flag.Bool("v", false, "show version") hosts := flag.String("hosts", "", "host address list") - cmd := flag.String("cmd", "", "cmds") + cmds := flag.String("cmds", "", "cmds") username := flag.String("u", "", "username") password := flag.String("p", "", "password") + key := flag.String("k", "", "ssh private key") port := flag.Int("port", 22, "ssh port") + ciphers := flag.String("ciphers", "", "ciphers") cmdFile := flag.String("cmdfile", "", "cmdfile path") hostFile := flag.String("hostfile", "", "hostfile path") ipFile := flag.String("ipfile", "", "hostfile path") - //gu - jsonFile := flag.String("j", "", "Json File Path") + cfgFile := flag.String("c", "", "cfg File Path") + jsonMode := flag.Bool("j", false, "print output in json format") outTxt := flag.Bool("outTxt", false, "write result into txt") + linuxMode := flag.Bool("l", false, "In linux mode,multi command combine with && ,such as date&&cd /opt&&ls") timeLimit := flag.Int("t", 30, "max timeout") numLimit := flag.Int("n", 20, "max execute number") flag.Parse() - var cmdList []string - var hostList []string + + var cmdList, hostList, cipherList []string var err error - sshHosts := []SSHHost{} - var host_Struct SSHHost - timeout := time.Duration(*timeLimit) * time.Second + sshHosts := []g.SSHHost{} + var host_Struct g.SSHHost if *version { - fmt.Println(VERSION) + fmt.Println(g.VERSION) os.Exit(0) } if *ipFile != "" { - hostList, err = GetIpList(*ipFile) + hostList, err = g.GetIpList(*ipFile) if err != nil { log.Println("load hostlist error: ", err) return @@ -67,89 +54,104 @@ func main() { } if *hostFile != "" { - hostList, err = Getfile(*hostFile) + hostList, err = g.Getfile(*hostFile) if err != nil { log.Println("load hostfile error: ", err) return } } if *hosts != "" { - hostList = strings.Split(*hosts, ";") + hostList = g.SplitString(*hosts) } if *cmdFile != "" { - cmdList, err = Getfile(*cmdFile) + cmdList, err = g.Getfile(*cmdFile) if err != nil { log.Println("load cmdfile error: ", err) return } } - if *cmd != "" { - cmdList = strings.Split(*cmd, ";") + if *cmds != "" { + cmdList = g.SplitString(*cmds) + + } + if *ciphers != "" { + cipherList = g.SplitString(*ciphers) } - if *jsonFile == "" { + if *cfgFile == "" { for _, host := range hostList { host_Struct.Host = host host_Struct.Username = *username host_Struct.Password = *password host_Struct.Port = *port - host_Struct.Cmd = cmdList + host_Struct.CmdList = cmdList + host_Struct.Key = *key + host_Struct.LinuxMode = *linuxMode sshHosts = append(sshHosts, host_Struct) } - } - //gu - if *jsonFile != "" { - sshHosts, err = GetJsonFile(*jsonFile) + } else { + sshHosts, err = g.GetJsonFile(*cfgFile) if err != nil { - log.Println("load jsonFile error: ", err) + log.Println("load cfgFile error: ", err) return } for i := 0; i < len(sshHosts); i++ { - cmdList, err = Getfile(sshHosts[i].CmdFile) - if err != nil { - log.Println("load cmdFile error: ", err) - return + if sshHosts[i].Cmds != "" { + sshHosts[i].CmdList = g.SplitString(sshHosts[i].Cmds) + } else { + cmdList, err = g.Getfile(sshHosts[i].CmdFile) + if err != nil { + log.Println("load cmdFile error: ", err) + return + } + sshHosts[i].CmdList = cmdList } - sshHosts[i].Cmd = cmdList } } chLimit := make(chan bool, *numLimit) //控制并发访问量 - chs := make([]chan string, len(sshHosts)) - limitFunc := func(chLimit chan bool, ch chan string, host SSHHost) { - dossh(host.Username, host.Password, host.Host, host.Cmd, host.Port, ch) + chs := make([]chan g.SSHResult, len(sshHosts)) + startTime := time.Now() + log.Println("Multissh start") + limitFunc := func(chLimit chan bool, ch chan g.SSHResult, host g.SSHHost) { + funcs.Dossh(host.Username, host.Password, host.Host, host.Key, host.CmdList, host.Port, *timeLimit, cipherList, host.LinuxMode, ch) <-chLimit } for i, host := range sshHosts { - chs[i] = make(chan string, 1) + chs[i] = make(chan g.SSHResult, 1) chLimit <- true go limitFunc(chLimit, chs[i], host) } - for i, ch := range chs { - fmt.Println(sshHosts[i].Host, " ssh start") - select { - case res := <-ch: - if res != "" { - fmt.Println(res) - sshHosts[i].Result += res - } - case <-time.After(timeout): - log.Println("SSH run timeout") - sshHosts[i].Result += ("SSH run timeout:" + strconv.Itoa(int(*timeLimit)) + "second.") + sshResults := []g.SSHResult{} + for _, ch := range chs { + res := <-ch + if res.Result != "" { + sshResults = append(sshResults, res) } - - fmt.Println(sshHosts[i].Host, " ssh end") } - + endTime := time.Now() + log.Printf("Multissh finished. Process time %s. Number of active ip is %d", endTime.Sub(startTime), len(sshHosts)) //gu if *outTxt { - for i := 0; i < len(sshHosts); i++ { - err = WriteIntoTxt(sshHosts[i]) + for _, sshResult := range sshResults { + err = g.WriteIntoTxt(sshResult) if err != nil { log.Println("write into txt error: ", err) return } } } - + if *jsonMode { + jsonResult, err := json.Marshal(sshResults) + if err != nil { + log.Println("json Marshal error: ", err) + } + fmt.Println(string(jsonResult)) + } else { + for _, sshResult := range sshResults { + fmt.Println("host: ", sshResult.Host) + fmt.Println("========= Result =========") + fmt.Println(sshResult.Result) + } + } } diff --git a/ssh.json.example b/ssh.json.example index f18a812..bede1a5 100644 --- a/ssh.json.example +++ b/ssh.json.example @@ -1,17 +1,19 @@ { "SshHosts": [ { - "Host": "192.168.15.101", - "Port": 22, - "Username": "admin", - "Password": "admin", - "CmdFile": "cmd1.txt.example" - }, + "Host": "192.168.31.51", + "Port": 22, + "Username": "admin", + "Password": "admin", + "cmds":"show clock;show clock" + }, { - "Host": "192.168.83.40", - "Port": 22, - "Username": "root", - "Password": "root", + "Host": "192.168.80.131", + "Port": 22, + "Username": "root", + "Password": "", + "key": "./server.key", + "linuxMode": true, "CmdFile": "cmd2.txt.example" } ] diff --git a/ssh_test.go b/ssh_test.go deleted file mode 100644 index 93c615c..0000000 --- a/ssh_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "os" - "strings" - - "testing" -) - -const ( - username = "admin" - password = "admin" - ip = "192.168.31.21" - port = 22 - cmd = "show clock;exit" -) - -func Test_SSH(t *testing.T) { - session, err := connect(username, password, ip, port) - if err != nil { - t.Error(err) - return - } - defer session.Close() - - cmdlist := strings.Split(cmd, ";") - - stdinBuf, err := session.StdinPipe() - if err != nil { - t.Error(err) - return - } - // var bt bytes.Buffer - // session.Stdout = &bt - t.Log(session.Stdout) - t.Log(session.Stderr) - session.Stdout = os.Stdout - session.Stderr = os.Stderr - session.Stdin = os.Stdin - err = session.Shell() - if err != nil { - t.Error(err) - return - } - for _, c := range cmdlist { - c = c + "\n" - stdinBuf.Write([]byte(c)) - } - session.Wait() - t.Error(err) - // t.Log(bt.String()) - return -} diff --git a/sshconnect.go b/sshconnect.go deleted file mode 100644 index 1962557..0000000 --- a/sshconnect.go +++ /dev/null @@ -1,97 +0,0 @@ -package main - -import ( - "bytes" - "fmt" - "net" - //"os" - - "time" - - "golang.org/x/crypto/ssh" -) - -func connect(user, password, host string, port int) (*ssh.Session, error) { - var ( - auth []ssh.AuthMethod - addr string - clientConfig *ssh.ClientConfig - client *ssh.Client - config ssh.Config - session *ssh.Session - err error - ) - // get auth method - auth = make([]ssh.AuthMethod, 0) - auth = append(auth, ssh.Password(password)) - - config = ssh.Config{ - Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "blowfish-cbc", "cast128-cbc", "aes192-cbc", "aes256-cbc", "arcfour"}, - } - - clientConfig = &ssh.ClientConfig{ - User: user, - Auth: auth, - Timeout: 30 * time.Second, - Config: config, - HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { - return nil - }, - } - - // connet to ssh - addr = fmt.Sprintf("%s:%d", host, port) - - if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil { - return nil, err - } - - // create session - if session, err = client.NewSession(); err != nil { - return nil, err - } - modes := ssh.TerminalModes{ - ssh.ECHO: 0, // disable echoing - ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud - ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud - } - - if err := session.RequestPty("xterm", 80, 40, modes); err != nil { - return nil, err - } - - return session, nil -} - -func dossh(username, password, ip string, cmdlist []string, port int, ch chan string) { - session, err := connect(username, password, ip, port) - - if err != nil { - ch <- fmt.Sprintf("<%s>", err.Error()) - //<-chLimit - return - - } - defer session.Close() - - cmdlist = append(cmdlist, "exit") - // cmd := "ls;date;exit" - stdinBuf, _ := session.StdinPipe() - //fmt.Fprintf(os.Stdout, "%s", stdinBuf) - var outbt, errbt bytes.Buffer - session.Stdout = &outbt - - session.Stderr = &errbt - err = session.Shell() - for _, c := range cmdlist { - c = c + "\n" - stdinBuf.Write([]byte(c)) - - } - session.Wait() - - ch <- (outbt.String() + errbt.String()) - //<-chLimit - return - -}