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 } 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() // 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 }