Shell Golang从*os.file复制,无需等待EOF

Shell Golang从*os.file复制,无需等待EOF,shell,go,ssh,pty,Shell,Go,Ssh,Pty,我正试图使用io.copy从go中的一个文件进行复制,它会在实际从其内部缓冲区复制字节之前等待EOF,对吗?在我的用例(PTY/SSH会话)中,EOF只在会话完成时出现,这意味着我一直在盲目飞行,直到会话决定结束 我曾尝试使用CopyN,每次使用1个字节,这是可行的,但如果我尝试等待出现特定的文本位,然后复制一个超过已推送到文件的文本位,代码将挂起,我将丢失会话。是否有一个函数可以“读取其中的内容”然后停止,或者有一个不同的标记(如EOF)可以告诉copy立即停止 我还尝试读取ptyI.pty指

我正试图使用
io.copy
从go中的一个文件进行复制,它会在实际从其内部缓冲区复制字节之前等待EOF,对吗?在我的用例(PTY/SSH会话)中,EOF只在会话完成时出现,这意味着我一直在盲目飞行,直到会话决定结束

我曾尝试使用CopyN,每次使用1个字节,这是可行的,但如果我尝试等待出现特定的文本位,然后复制一个超过已推送到文件的文本位,代码将挂起,我将丢失会话。是否有一个函数可以“读取其中的内容”然后停止,或者有一个不同的标记(如EOF)可以告诉copy立即停止

我还尝试读取ptyI.pty指向的文件的内容,但它总是返回0字节,因此我无法检查那里的更新

以下是从现在开始处理它的代码:

type PtyInterface struct {
    pty          *os.File
    buf          *bytes.Buffer
}

func (ptyI *PtyInterface) PrivCmd(cmdStr string) (string, error) {

    // Copy the command provided into the STDIN of the bash shell we opened with
    // the earlier PtyInterface
    _, _ = io.Copy(ptyI.pty, strings.NewReader(string("somecommand")))

    // Assuming everything has gone well, we wait for the prompt to appear

    // We do this by running through individual bytes until the prompt is
    // fully printed (otherwise we might try to send in the answer at the wrong time)
    for !strings.HasSuffix(ptyI.buf.String(), "Prompt question? ") {
        _, _ = io.CopyN(ptyI.buf, ptyI.pty, 1)
    }

    // Once we hit the prompt we throw the answer into STDIN along with a newline
    // and the bash shell should accept this and begin executing the command.
    _, _ = io.Copy(ptyI.pty, strings.NewReader(string("answer\n")))

    // If we dont throw an exit in there then the PTY will never receive an EOF marker and we'll
    // hang on the next copy
    _, _ = io.Copy(ptyI.pty, strings.NewReader(string("exit\n")))

    // Now this copy will wait for an EOF
    _, _ = io.Copy(ptyI.buf, ptyI.pty)

    //Debug info to be printed after
    fmt.Println("\nBytes written to buffer (newone): \n" + ptyI.buf.String())

    return ptyI.buf.String(), nil
}

io.Copy
视为批量复制或流的便利功能,而不是请求/响应模式的正确工具

只需通过检查每个字节是否与消息匹配,将字节累积到消息中。直接使用Read方法

func Expect(message string, r io.Reader) (resp string, err error) {
    b := []byte{0} // 1 byte buffer
    var n int

    for err == nil {
        n, err = r.Read(b)
        if n == 0 {
            continue
        }
        resp += string(b[0])
        if strings.HasSuffix(resp, message) {
            return resp, err
        }
    }

    return resp, err
}
在您的示例中,您可以这样使用:

resp, err := Expect("Prompt question? ", ptyI.pty)

下面是一个使用模拟连接io的演示。读卡器:

io.Copy
在开始复制之前不会等待EOF。它等待EOF知道它已完成复制。如果输出是行缓冲的,您可以使用扫描仪。您是否尝试过直接调用
Read
?@Adrian,但在函数知道其已完成对file@XanderMay; 这不是阅读形成联系的方式。如果没有要读取的内容,它会一直阻止,直到EOF。@XanderMay:不,你看不到是否有要读取的内容,你会一直阻止,直到read方法返回为止。Go中的并发非常简单,如果在从连接读取数据时有其他事情要做,请在goroutine中处理读取。