...
这版正常点
package main import ( "bytes" "fmt" "golang.org/x/crypto/ssh" "io" "log" "net" "time" ) type Connection struct { *ssh.Client password string } func Connect(addr, user, password string) (*Connection, error) { sshConfig := &ssh.ClientConfig{ User: user, Auth: []ssh.AuthMethod{ ssh.Password(password), }, HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }), } conn, err := ssh.Dial("tcp", addr, sshConfig) if err != nil { return nil, err } return &Connection{conn, password}, nil } func (conn *Connection) SendCommands(cmds string) ([]byte, error) { session, err := conn.NewSession() if err != nil { log.Fatal(err) } defer session.Close() modes := ssh.TerminalModes{ ssh.ECHO: 0, // disable echoing ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud } err = session.RequestPty("xterm", 80, 40, modes) if err != nil { return []byte{}, err } stdoutB := new(bytes.Buffer) session.Stdout = stdoutB in, _ := session.StdinPipe() go func(in io.Writer, output *bytes.Buffer) { log.Println("go run func run1...") in.Write([]byte("cat > 1.txt <<EOF ")) time.Sleep(time.Second) in.Write([]byte("uosuos ")) time.Sleep(time.Second) in.Write([]byte("uosuos ")) time.Sleep(time.Second) in.Write([]byte("EOF ")) time.Sleep(time.Second) log.Println(output.ReadByte()) }(in, stdoutB) //go func(in io.Writer, output *bytes.Buffer) { // time.Sleep(3*time.Second) // log.Println("go run func run2...") // in.Write([]byte("pwd ")) // time.Sleep(1*time.Second) //}(in, stdoutB) //err = session.Run(cmds) session.Shell() time.Sleep(4* time.Second) if err != nil { return []byte{}, err } return stdoutB.Bytes(), nil } func main() { // ssh refers to the custom package above conn, err := Connect("1.1.13.3:22", "huzh", "uosuos") if err != nil { log.Fatal(err) } output, err := conn.SendCommands("sudo ls /root") if err != nil { log.Fatal(err) } fmt.Println("output===>",string(output),"xxx") }
from https://stackoverflow.com/questions/44471749/golang-enter-ssh-sudo-password-on-prompt-or-exit
golang ssh sudo
package main import ( "bytes" "fmt" "io" "log" "net" "strings" "golang.org/x/crypto/ssh" ) type Connection struct { *ssh.Client password string } func Connect(addr, user, password string) (*Connection, error) { sshConfig := &ssh.ClientConfig{ User: user, Auth: []ssh.AuthMethod{ ssh.Password(password), }, HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }), } conn, err := ssh.Dial("tcp", addr, sshConfig) if err != nil { return nil, err } return &Connection{conn, password}, nil } func (conn *Connection) SendCommands(cmds string) ([]byte, error) { session, err := conn.NewSession() if err != nil { log.Fatal(err) } defer session.Close() modes := ssh.TerminalModes{ ssh.ECHO: 0, // disable echoing ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud } err = session.RequestPty("xterm", 80, 40, modes) if err != nil { return []byte{}, err } stdoutB := new(bytes.Buffer) session.Stdout = stdoutB in, _ := session.StdinPipe() go func(in io.Writer, output *bytes.Buffer) { for { if strings.Contains(string(output.Bytes()), "[sudo] password for ") { _, err = in.Write([]byte(conn.password + " ")) if err != nil { break } fmt.Println("put the password --- end .") break } } }(in, stdoutB) err = session.Run(cmds) if err != nil { return []byte{}, err } return stdoutB.Bytes(), nil } func main() { // ssh refers to the custom package above conn, err := Connect("1.1.13.20:22", "uos", "uosuos") if err != nil { log.Fatal(err) } output, err := conn.SendCommands("sleep 3; ls /tmp") if err != nil { log.Fatal(err) } fmt.Println(len(string(output))) }