# Buggy code
def _send(self, data):
    if self.child_fd is None:
        return None

    if not select.select([], [self.child_fd], [], 0)[1]:
        return 0

    try:
        if self.stdin_logger:
            self.stdin_logger.log(self.stdin_logger_level, data)
        written = os.write(self.child_fd, data)
    except OSError as why:
        if why.errno == errno.EPIPE:  # broken pipe
            os.close(self.child_fd)
            self.child_fd = None
            return
        raise
    return written


# Traceback (TypeError)
(virtualenv) [root@li451-148 virtualenv]# salt-ssh -c ./etc/salt '*' -i test.ping
Permission denied for host ssh, do you want to deploy the salt-ssh key? (password required):
[Y/n] y
Password for root@ssh:
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
TypeError: a bytes-like object is required, not 'str'
Traceback (most recent call last):
File "/usr/local/bin/salt-ssh", line 6, in <module>
exec(compile(open(__file__).read(), __file__, 'exec'))
File "/root/salt/scripts/salt-ssh", line 10, in <module>
salt_ssh()
File "/root/salt/salt/scripts.py", line 416, in salt_ssh
client.run()
File "/root/salt/salt/cli/ssh.py", line 21, in run
ssh.run()
File "/root/salt/salt/client/ssh/__init__.py", line 636, in run
ret = self.key_deploy(host, ret)
File "/root/salt/salt/client/ssh/__init__.py", line 358, in key_deploy
return self._key_deploy_run(host, target, True)
File "/root/salt/salt/client/ssh/__init__.py", line 381, in _key_deploy_run
stdout, stderr, retcode = single.shell.copy_id()
File "/root/salt/salt/client/ssh/shell.py", line 219, in copy_id
stdout, stderr, retcode = self._run_cmd(self._copy_id_str_new())
File "/root/salt/salt/client/ssh/shell.py", line 387, in _run_cmd
term.sendline(self.passwd)
File "/root/salt/salt/utils/vt.py", line 294, in sendline
return self.send('{0}{1}'.format(data, linesep))
File "/root/salt/salt/utils/vt.py", line 288, in send
return self._send(data)
File "/root/salt/salt/utils/vt.py", line 569, in _send
written = os.write(self.child_fd, data)
TypeError: a bytes-like object is required, not 'str'
Traceback (most recent call last):
File "/usr/local/bin/salt-ssh", line 6, in <module>
exec(compile(open(__file__).read(), __file__, 'exec'))
File "/root/salt/scripts/salt-ssh", line 10, in <module>
salt_ssh()
File "/root/salt/salt/scripts.py", line 416, in salt_ssh
client.run()
File "/root/salt/salt/cli/ssh.py", line 21, in run
ssh.run()
File "/root/salt/salt/client/ssh/__init__.py", line 636, in run
ret = self.key_deploy(host, ret)
File "/root/salt/salt/client/ssh/__init__.py", line 358, in key_deploy
return self._key_deploy_run(host, target, True)
File "/root/salt/salt/client/ssh/__init__.py", line 381, in _key_deploy_run
stdout, stderr, retcode = single.shell.copy_id()
File "/root/salt/salt/client/ssh/shell.py", line 219, in copy_id
stdout, stderr, retcode = self._run_cmd(self._copy_id_str_new())
File "/root/salt/salt/client/ssh/shell.py", line 387, in _run_cmd
term.sendline(self.passwd)
File "/root/salt/salt/utils/vt.py", line 294, in sendline
return self.send('{0}{1}'.format(data, linesep))
File "/root/salt/salt/utils/vt.py", line 288, in send
return self._send(data)
File "/root/salt/salt/utils/vt.py", line 569, in _send
written = os.write(self.child_fd, data)
TypeError: a bytes-like object is required, not 'str'

# Fixed code
def _send(self, data):
    if self.child_fd is None:
        return None

    if not select.select([], [self.child_fd], [], 0)[1]:
        return 0

    try:
        if self.stdin_logger:
            self.stdin_logger.log(self.stdin_logger_level, data)
        if six.PY3:
            written = os.write(self.child_fd, data.encode(__salt_system_encoding__))
        else:
            written = os.write(self.child_fd, data)
    except OSError as why:
        if why.errno == errno.EPIPE:  # broken pipe
            os.close(self.child_fd)
            self.child_fd = None
            return
        raise
    return written