Golang写入输入并从终端进程获取输出 [英] Golang write input and get output from terminal process

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

问题描述

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



我在Golang中找不到一个简单的例子,类似于上面的工作。



在Golang中,我想要做这样的事情,但它不会似乎工作:

  cmd:= exec.Command(ssh,user @ xxxx)
cmd .Stdout = os.Stdout
cmd.Stderr = os.Stderr
stdin,_:= cmd.StdinPipe()
stdin.Write([] byte(password \\\
) )
cmd.Run()

然而;我不知道如何去做这件事,因为每次我执行这个SSH命令,我只能得到输出。我无法从代码自动输入我的密码。
有没有人写过例如ssh等终端进程的例子?如果是这样,请分享。

解决方案

感谢上述评论,我能够通过密码获得ssh访问权限。我使用了golang的ssh api库。这很简单,因为我遵循以下示例:



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



具体来说:

  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", "user@x.x.x.x")
    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写入输入并从终端进程获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆