title: Is the output from ssh guaranteed to be retrieved?
body: <p>I use the <a href="https://pkg.go.dev/golang.org/x/crypto@v0.0.0-20210813211128-0a44fdfbc16e/ssh" rel="nofollow noreferrer">ssh package</a> to connect to a Linux server and retrieve the output of commands. The helper function I wrote for that is below:</p>
  <pre class="lang-golang prettyprint-override"><code>func sshRunCommand(host string, command string) (output string, err error) {
      keyData, err := ioutil.ReadFile(&quot;srv.private.openssh&quot;)
      if err != nil {
          return &quot;&quot;, err
      }
      key, err := ssh.ParsePrivateKey(keyData)
      if err != nil {
          return &quot;&quot;, err
      }
      // Authentication
      config := &amp;ssh.ClientConfig{
          User: &quot;root&quot;,
          HostKeyCallback: ssh.InsecureIgnoreHostKey(),
          Auth: []ssh.AuthMethod{
              ssh.PublicKeys(key),
          },
      }
      // Connect
      client, err := ssh.Dial(&quot;tcp&quot;, net.JoinHostPort(host, &quot;22&quot;), config)
      if err != nil {
          return &quot;&quot;, err
      }
      // Create a session. It is one session per command.
      session, err := client.NewSession()
      if err != nil {
          return &quot;&quot;, err
      }
      defer session.Close()
      var b bytes.Buffer
      session.Stdout = &amp;b //  this is the place I am concerned with
      // Finally, run the command
      err = session.Run(command)
      return b.String(), err
  }
  
  </code></pre>
  <p>This command usually works fine: it always connects but randomly does not return the output.</p>
  <p>Before going further in my investigations, <strong>I wanted to make sure that the output buffer is flushed before returning the output. Is this the case?</strong></p>