title: golang scp file using crypto/ssh
body: <p>I'm trying to download a remote file over ssh
  The following approach works fine on shell</p>
  
  <pre><code>ssh hostname "tar cz /opt/local/folder" &gt; folder.tar.gz
  </code></pre>
  
  <p>However the same approach on golang giving some difference in output artifact size. For example the same folders with pure shell produce artifact gz file 179B and same with go script 178B.
  I assume that something has been missed from io.Reader or session got closed earlier. Kindly ask you guys to help.</p>
  
  <p>Here is the example of my script:</p>
  
  <pre><code>func executeCmd(cmd, hostname string, config *ssh.ClientConfig, path string) error {
      conn, _ := ssh.Dial("tcp", hostname+":22", config)
      session, err := conn.NewSession()
      if err != nil {
          panic("Failed to create session: " + err.Error())
      }
  
      r, _ := session.StdoutPipe()
      scanner := bufio.NewScanner(r)
  
      go func() {
          defer session.Close()
  
          name := fmt.Sprintf("%s/backup_folder_%v.tar.gz", path, time.Now().Unix())
          file, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
          if err != nil {
              panic(err)
          }
          defer file.Close()
          for scanner.Scan() {
              fmt.Println(scanner.Bytes())
              if err := scanner.Err(); err != nil {
                  fmt.Println(err)
              }
  
              if _, err = file.Write(scanner.Bytes()); err != nil {
                  log.Fatal(err)
  
              }
          }
      }()
  
      if err := session.Run(cmd); err != nil {
          fmt.Println(err.Error())
          panic("Failed to run: " + err.Error())
      }
  
      return nil
  }
  </code></pre>
  
  <p>Thanks!</p>