diff --git a/README.MD b/README.MD index 12f8bfc..bc46240 100644 --- a/README.MD +++ b/README.MD @@ -28,6 +28,15 @@ go build ssh port (default 22) //主机的 SSH 端口,默认 22 -u string username //主机的 SSH 用户名 + -j string + jsonFile //保存大量主机,包括主机地址,SSH用户名,SSH密码,SSH端口,所需执行的cmd指令文件地址 + -outTxt bool + outTxt (default false) //是否允许把结果保存到文件中,true为允许 false为默认值 + -t duration + timeLimit (default 30) //最大并发访问时间 默认为30s + -n int + numLimit (default 20) //最大并发访问量 默认为20 + ``` **cmdfile 示例** ``` @@ -102,3 +111,20 @@ sw-2#exit 10.10.15.102 ssh end ``` +#### ipfile +``` +./multissh -j jsonSample.json -t 30 -n 20 -outTxt true +10.10.15.101 ssh start +sw-1#show clock +05:29:43.269 UTC Tue Jun 6 2017 +sw-1#exit + +10.10.15.101 ssh end + +10.10.15.102 ssh start +sw-2#show clock +05:27:41.332 UTC Tue Jun 6 2017 +sw-2#exit + +10.10.15.102 ssh end +``` \ No newline at end of file diff --git a/main.go b/main.go index ed05b1d..73bfe28 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "strconv" "strings" "time" + // "github.com/bitly/go-simplejson" ) @@ -28,13 +29,13 @@ func main() { cmd := flag.String("cmd", "", "cmds") username := flag.String("u", "", "username") password := flag.String("p", "", "password") - port := flag.String("port", "", "ssh port") + port := flag.Int("port", 22, "ssh port") cmdFile := flag.String("cmdfile", "", "cmdfile path") hostFile := flag.String("hostfile", "", "hostfile path") ipFile := flag.String("ipfile", "", "hostfile path") cfg := flag.String("cfg", "", "cfg path") //gu - jsonFile := flag.String("j", "ssh.json", "Json File Path") + jsonFile := flag.String("j", "", "Json File Path") outTxt := flag.Bool("outTxt", false, "write result into txt") timeLimit := flag.Duration("t", 30, "max timeout") numLimit := flag.Int("n", 20, "max execute number") @@ -43,10 +44,6 @@ func main() { var cmdList []string var hostList []string var err error - //gu - var usernameList []string - var passwordList []string - var portList []string sshHosts := []SSHHost{} var host_Struct SSHHost @@ -70,19 +67,6 @@ func main() { hostList = strings.Split(*hosts, ";") } - //gu - if *username != "" { - usernameList = strings.Split(*username, ";") - } - - if *password != "" { - passwordList = strings.Split(*password, ";") - } - - if *port != "" { - portList = strings.Split(*port, ";") - } - //// if *cmdFile != "" { cmdList, err = Getfile(*cmdFile) if err != nil { @@ -94,11 +78,11 @@ func main() { cmdList = strings.Split(*cmd, ";") } if *cfg == "" { - for pos, host := range hostList { + for _, host := range hostList { host_Struct.Host = host - host_Struct.Username = usernameList[pos] - host_Struct.Password = passwordList[pos] - host_Struct.Port, _ = strconv.Atoi(portList[pos]) + host_Struct.Username = *username + host_Struct.Password = *password + host_Struct.Port = *port host_Struct.Cmd = cmdList sshHosts = append(sshHosts, host_Struct) } @@ -140,7 +124,7 @@ func main() { } */ //fmt.Println(sshhosts) - chLimit := make(chan bool, numLimit) + 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) @@ -163,11 +147,12 @@ func main() { log.Println("SSH run timeout") sshHosts[i].Result += ("SSH run timeout:" + strconv.Itoa(int(*timeLimit)) + "second.") } + fmt.Println(sshHosts[i].Host, " ssh end") } //gu - if *outTxt { + if !*outTxt { for i := 0; i < len(sshHosts); i++ { err = WriteIntoTxt(sshHosts[i]) if err != nil { diff --git a/sshconnect.go b/sshconnect.go index c44ffcb..c11ad2b 100644 --- a/sshconnect.go +++ b/sshconnect.go @@ -5,6 +5,7 @@ import ( "fmt" "net" //"os" + "time" "golang.org/x/crypto/ssh" @@ -78,6 +79,7 @@ func dossh(username, password, ip string, cmdlist []string, port int, ch chan st } session.Wait() + ch <- (outbt.String() + errbt.String()) //<-chLimit return