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

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

本文介绍了Golang写入输入并从终端进程获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于如何发送来自终端子进程(如ssh)的输入和接收输出的问题。在Python中的一个例子是这样的:





具体来说:

  func ExampleDial(){
//一个SSH客户端用ClientConn表示。目前仅支持
//支持密码验证方法。
//
//要通过远程服务器进行身份验证,您必须通过ClientConfig中的Auth字段至少传递一个
//执行AuthMethod。
config:=& ClientConfig {
用户:用户名,
认证:[] AuthMethod {
密码(yourpassword),
},
$ b client,err:= Dial(tcp,yourserver.com:22,config)
if err!= nil {
panic(无法拨号: + err.Error())
}

//每个ClientConn可以支持多个交互式会话,
//由Session表示。
session,err:= client.NewSession()
if err!= nil {
panic(Failed to create session:+ err.Error())
}
defer session.Close()

//创建Session后,您可以使用Run方法在
//远程端执行单个命令。
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())
}


I have a question regarding how to send input and receive output from a terminal subprocess such as ssh. An example in python would be something like this:

how to give subprocess a password and get stdout at the same time

I cannot find a simple example in Golang that is similar how the above work.

In Golang I would want to do something like this but it does not seem to work:

    cmd := exec.Command("ssh", "[email protected]")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    stdin, _ := cmd.StdinPipe()
    stdin.Write([]byte("password\n"))
    cmd.Run()

However; I'm not sure how to do this in go because every time i exec this ssh command I am only able to get the output. I am unable to input my password automatically from code.Does anyone have examples of writing to terminal processes such as ssh? If so, please share.

解决方案

Thanks to the comments above, I was able to get ssh access working with a password. I used golang's ssh api library. It was fairly simple as I followed the examples from:

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

Specifically:

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

这篇关于Golang写入输入并从终端进程获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:38