go shell执行优化版+ 输出结果为table格式
package mainimport (\"fmt\"\"github.com/modood/table\"\"golang.org/x/crypto/ssh\"\"io/ioutil\"\"net\"\"time\")func connect(user, password, host, key string, port int, cipherList []string) (*ssh.Session, error) {var (auth []ssh.AuthMethodaddr stringclientConfig *ssh.ClientConfigclient *ssh.Clientconfig ssh.Configsession *ssh.Sessionerr error)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.Signerif 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) e38f8rror {return nil},}// connet to sshaddr = fmt.Sprintf(\"%s:%d\", host, port)if client, err = ssh.Dial(\"tcp\", addr, clientConfig); err != nil {return nil, err}// create sessionif session, err = client.NewSession(); err != nil {return nil, err}return session, nil}func main() {var ciphers []stringsession, err := connect(\"root\", \"123456\", \"192.168.100.50\", \"\", 22, ciphers)if err != nil {fmt.Println(err)return}defer session.Close()buf, err := session.CombinedOutput(\"hostname\")if err != nil {fmt.Println(err)return}re := string(buf)type House struct {Host stringShell stringReturn string}s := []House{{\"192.168.100.50\", \"hostname\", re},}t := table.Table(s)fmt.Println(t)return}