I am trying to read from the stdoutpipe of an SSH connection. I am able to read it when there is data in the buffer, but when I try to read later after it's empty the read hangs.
Is there a way I can check if there is data in the buffer before reading it? or is something else going on here entirely?
I made a quick example to show what I'm seeing.
package main
import (
"fmt"
"strconv"
"golang.org/x/crypto/ssh"
)
func main() {
var user = "<user>"
var passwd = "<password>"
var ip = "<ip>"
sshConfig := &ssh.ClientConfig{
User: user,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{
ssh.Password(passwd),
},
}
var conn, _ = ssh.Dial("tcp", ip + ":22", sshConfig)
var sess, _ = conn.NewSession()
var stdin, _ = sess.StdinPipe()
var stdout, _ = sess.StdoutPipe()
sess.Shell()
stdin.Write([]byte("ls
"))
var numBytes = 1
var readCount = 0
for numBytes > 0 {
fmt.Println("Read count: " + strconv.Itoa(readCount))
var recvData = make([]byte, 1024)
numBytes, _ = stdout.Read(recvData)
fmt.Println(string(recvData[:numBytes]))
readCount++
}
}
You'll need to update the user, passwd, and ip vars, but after that you should see the program freeze after stdout is empty.