3

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, "\n")
    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)? 
Ytsejammer
  • 1,384
  • 2
  • 13
  • 25
user164485
  • 71
  • 4

1 Answers1

-3

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.

zzzz
  • 87,403
  • 16
  • 175
  • 139
  • Thanks, use library is a good way , I need change my view. In my work environment, there are many different script, I 'm just want integrate them. – user164485 Jun 05 '13 at 07:03
  • 2
    Or add whatever-the-option-is-called to skip this check (for new connections only ideally). – Ask Bjørn Hansen Jun 05 '13 at 07:04
  • 4
    I don't think "you're doing it wrong" is a good answer. Piping is a programmatic way of providing stdin, but that's clearly not "wrong". – ysimonson Jan 28 '15 at 17:12