duanbi3786 2013-06-05 06:10
浏览 239

如何在Golang程序中以编程方式向os.stdin输入字符

I want to automatically provide input to a command in the bash terminal.

Is it possible to programmatically enter a "yes" after than ssh prompt? Here is a script representing the idea.

package main
import (
    "bytes"
    "io"
    "os"
    "fmt"
    "os/exec"
 )
func main() {
    c1 := exec.Command("ssh","root@172.30.0.77")
    r, w := io.Pipe()
    c1.Stdout = w
    c1.Stdin = r
    c1.Start()
    var b bytes.Buffer
    b.Write([]byte("yes"))
    fmt.Fprintf(&b, "
")
    b.WriteTo(os.Stdin)
    c1.Wait()
    w.Close()

 }

# go run login.go 
yes
The authenticity of host '172.30.0.77 (172.30.0.77)' can't be established.
RSA key fingerprint is 13:46:96:ff:ab:12:76:0e:24:6e:3e:7a:ee:c0:81:af.
Are you sure you want to continue connecting (yes/no)? 
  • 写回答

1条回答 默认 最新

  • douhuan3448 2013-06-05 06:49
    关注

    This is wrong on so many levels, but let me pick just the worst one.

    Many programs, usually for very good reasons, try hard to ensure that they're interacting with a human, not a redirected input, here doc etc.

    Attempts to subvert those very good reasons is a horrible idea and IMHO a big no-no.

    If you want to interact programmatically with the ssh protocol, use a library (a package in case of Go), not a client binary.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示