Golang写入输入并从终端进程获取输出

Golang写入输入并从终端进程获取输出,第1张

Golang写入输入并从终端进程获取输出

由于上面的评论,我可以使用密码使用ssh访问。我使用了golang的ssh api库。当我遵循以下示例时,这非常简单:

https://pre.google.com/p/go/source/browse/ssh/example_test.go?repo=crypto

特别:

func ExampleDial() {    // An SSH client is represented with a ClientConn. Currently only    // the "password" authentication method is supported.    //    // To authenticate with the remote server you must pass at least one    // implementation of AuthMethod via the Auth field in ClientConfig.    config := &ClientConfig{ User: "username", Auth: []AuthMethod{         Password("yourpassword"), },    }    client, err := Dial("tcp", "yourserver.com:22", config)    if err != nil { panic("Failed to dial: " + err.Error())    }    // Each ClientConn can support multiple interactive sessions,    // represented by a Session.    session, err := client.NewSession()    if err != nil { panic("Failed to create session: " + err.Error())    }    defer session.Close()    // once a Session is created, you can execute a single command on    // the remote side using the Run method.    var b bytes.Buffer    session.Stdout = &b    if err := session.Run("/usr/bin/whoami"); err != nil { panic("Failed to run: " + err.Error())    }    fmt.Println(b.String())}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5048770.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存