Golang 从 *os.file 复制而不挂起等待 EOF

Golang copy from *os.file without hanging on waiting for EOF

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

我尝试过一次使用 1 个字节的 CopyN,这确实有效,但是如果我尝试等待特定的文本出现,然后我复制了一个已经推送到文件中的内容代码将挂起,我失去了会话。是否有仅 "read whats there" 然后停止的功能,或可以告诉复制暂时停止的不同标记(如 EOF)?

我也尝试读取 ptyI.pty 指向的文件的内容,但它总是 returns 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 视为批量复制或流的便利函数,而不是 request/response 模式的正确工具。

只需检查消息是否与每个字节上的消息匹配,即可将字节累积到消息中。直接使用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.Reader:playground