完成读取json文件、结果导出为txt文件、超时处理(时限参数可输入) 控制并发访问

This commit is contained in:
alen 2017-06-08 10:06:13 +08:00
parent 9e410eaa21
commit 47366d7696
3 changed files with 141 additions and 45 deletions

View file

@ -3,9 +3,9 @@ package main
import (
"bytes"
"fmt"
"time"
"net"
//"os"
"time"
"golang.org/x/crypto/ssh"
)
@ -55,29 +55,31 @@ func connect(user, password, host string, port int) (*ssh.Session, error) {
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()
// 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
}