使用包:golang.org/x/crypto/ssh
以下封装一个发送命令的Cli结构体
package utils import ( "fmt" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/terminal" "io" "net" "os" "time" ) type Cli struct { IP string //IP地址 Username string //用户名 Password string //密码 Port int //端口号 client *ssh.Client //ssh客户端 LastResult string //最近一次Run的结果 } //创建命令行对象 //@param ip IP地址 //@param username 用户名 //@param password 密码 //@param port 端口号,默认22 func New(ip string, username string, password string, port ...int) *Cli { cli := new(Cli) cli.IP = ip cli.Username = username cli.Password = password if len(port) <= 0 { cli.Port = 22 } else { cli.Port = port[0] } return cli } //执行shell //&#