Fix macOS pssh logging caused by line buffering

Fix macOS pssh logging caused by line buffering

macos homebrew python

If you want to log the output of an SSH connection under macOS with pssh from Homebrew, the following error occurs:

line buffering (buffering=1) isn't supported in binary mode

In the pssh command this happens when you define the -o option for output.

pssh -t 0 -p 10 -l root -h host.list -o logs 'command'

In order to log the output of the SSH session, we have to make a small adjustment to the Python psshlib.

Here is how to fix pssh session logging on macOS Ventura

Let’s open this file with an editor of your choice:

The path may change for you, especially if you have a different version of pssh installed. Just adjust the path to your version number.

/opt/homebrew/Cellar/pssh/2.3.1_6/libexec/lib/python3.10/site-packages/psshlib/manager.py

Inside the file we search for buffering. Then we change the code as follows.

buffering=1 needs to be changed to buffering=0

At the end of the if else statement, we add the following two lines:

dest.write(data)
dest.flush()

This code block should now look similar to the following code:

    def run(self):
        while True:
            filename, data = self.queue.get()
            if filename == self.ABORT:
                return

            if data == self.OPEN:
                self.files[filename] = open(filename, 'wb', buffering=0)
                psshutil.set_cloexec(self.files[filename])
            else:
                dest = self.files[filename]
                if data == self.EOF:
                    dest.close()
                else:
                    dest.write(data)
                    dest.flush()

Now we save the file and our changes. From now on pssh can log the output to a file without getting a buffering error message.

Please keep in mind that you may need to make this change again in case you apply a homebrew update on your Mac.

Previous Post Next Post