JavaEar 专注于收集分享传播有价值的技术资料

Writing and reading stdout unbuffered to a file over SSH

I'm using Node to execute a Python script. The Python script SSH's into a server, and then runs a Pig job. I want to be able to get the standard out from the Pig job, and display it in the browser.

I'm using the PExpect library to make the SSH calls, but this will not print the output of the pig call until it has totally completed (at least the way I have it written). Any tips on how to restructure it?

child.sendline(command)
child.expect(COMMAND_PROMPT)
print(child.before)

I know I shouldn't be expecting the command prompt (cause that will only show up when the process ends), but I'm not sure what I should be expecting.

1个回答

    最佳答案
  1. Repeating my comment as an answer, since it solved the issue:

    If you set child.logfile_read to a writable file-like object (e.g. sys.stdout), Pexpect will the forward the output there as it reads it.

    child.logfile_read = sys.stdout
    child.sendline(command)
    child.expect(COMMAND_PROMPT)