title: Interact with a shell script running remotely via SSH
body: <p>I am working an application written in golang,for which one of the capability will be to SSH into a device and execute a shell script there. With my current implementation,a normal script is getting executed.The problem lies in the execution of a script that requires user input: for e.g the script asks the user, can we proceed with the installation: and the user has to type in Y or N. This is the part where it is failing. Here is my implementation:</p>
  <pre><code>func main() {
      
      cmdOutput,err := ExecuteScriptSSH()
      if err != nil{
          fmt.Printf(&quot;Error is %s&quot;,err)
      }
      fmt.Printf(&quot;Command Output is %s&quot;, cmdOutput)
  }
  
  func ExecuteScriptSSH() (string, error) {
      script := &quot;#!/bin/sh\n\n ls -l \n\n date&quot;
      params := make(map[string]interface{})
      params[&quot;Username&quot;] = &quot;uname&quot;
      params[&quot;Password&quot;] = &quot;pwd&quot;
      params[&quot;IPAddress&quot;] = &quot;ip&quot;
      params[&quot;Port&quot;] = &quot;22&quot;
      connection := NewConnection(params)
  
      client, err := connection.ConnectNonSecure()
      if err != nil {
          fmt.Printf(&quot;unable to Connect:%v &quot;, err)
      }
      defer client.Close()
      ss, err := client.NewSession()
      if err != nil {
          fmt.Printf(&quot;unable to create SSH session:%v &quot;, err)
      }
      //Converting the Script to string to a shell script file by writing to it.
      d1 := []byte(script)
      err = os.WriteFile(&quot;/tmp/script.sh&quot;, d1, 0777)
      if err != nil {
          fmt.Printf(&quot;Error constructing Script file %v&quot;, err)
      }
      // opening the script file
      scriptFile, err2 := os.OpenFile(&quot;/tmp/script.sh&quot;, os.O_RDWR|os.O_CREATE, 0777)
      if err2 != nil {
          fmt.Printf(&quot;Error opening Script file is %s&quot;, err)
      }
      cmdOutput := &amp;bytes.Buffer{}
      ss.Stdout = cmdOutput
      ss.Stdin = scriptFile
      interpreter := &quot;sh&quot;
      err = ss.Run(interpreter)
      if err != nil {
          fmt.Printf(&quot;Error in Executing Script file %v&quot;, err.Error())
      }
      return cmdOutput.String(), err
  
  }
  
  type Params struct {
      Username string
      Password string
      IPAddress  string
      Port     string
  }
  
  func NewConnection(data map[string]interface{}) *Params {
      var params Params
      fmt.Printf(&quot;Data is %v&quot;,data)
      err := mapstructure.Decode(data,&amp;params)
      if err != nil {
          panic(err)
      }
      fmt.Printf(&quot;Mapped Connection details %v&quot;,params)
      return &amp;params
  }
  
  func (p *Params) ConnectNonSecure() (*ssh.Client, error){
  
      config := &amp;ssh.ClientConfig{
          User: p.Username,
          Auth: []ssh.AuthMethod{
              ssh.Password(p.Password),
          },
          // Non-Production-only
          HostKeyCallback: ssh.InsecureIgnoreHostKey(),
      }
      fmt.Printf(&quot;Connection details %v&quot;,p)
      // Connect to the remote server and perform a handshake
      client, err := ssh.Dial(&quot;tcp&quot;, p.IPAddress+&quot;:&quot;+p.Port, config)
      if err != nil {
          log.Fatalf(&quot;unable to connect to: %v %s&quot;, err,p.IPAddress)
      }
  
      return client, err
  
  }
  </code></pre>
  <p>for simplicity sake I have added a very simple script,but actually the script is huge and requires user confirmation.Any idea on how to solve this issue?</p>