# Buggy code
def shim_cmd(self, cmd_str, extension="py"):
    """
    Run a shim command.

    If tty is enabled, we must scp the shim to the target system and
    execute it there
    """
    if not self.tty and not self.winrm:
        return self.shell.exec_cmd(cmd_str)

    # Write the shim to a temporary file in the default temp directory
    with tempfile.NamedTemporaryFile(
        mode="w+b", prefix="shim_", delete=False
    ) as shim_tmp_file:
        shim_tmp_file.write(cmd_str)

    # Copy shim to target system, under $HOME/.<randomized name>
    target_shim_file = ".{0}.{1}".format(binascii.hexlify(os.urandom(6)), extension)
    if self.winrm:
        target_shim_file = saltwinshell.get_target_shim_file(self)
    self.shell.send(shim_tmp_file.name, target_shim_file, makedirs=True)

    # Remove our shim file
    try:
        os.remove(shim_tmp_file.name)
    except IOError:
        pass

    # Execute shim
    if extension == "ps1":
        ret = self.shell.exec_cmd('"powershell {0}"'.format(target_shim_file))
    else:
        if not self.winrm:
            ret = self.shell.exec_cmd("/bin/sh '$HOME/{0}'".format(target_shim_file))
        else:
            ret = saltwinshell.call_python(self)

    # Remove shim from target system
    if not self.winrm:
        self.shell.exec_cmd("rm '$HOME/{0}'".format(target_shim_file))
    else:
        self.shell.exec_cmd("del {0}".format(target_shim_file))

    return ret


# Traceback (TypeError)
salt-ssh -i '*' test.ping

[ERROR   ] An un-handled exception from the multiprocessing process 'MultiprocessingProcess-1' was caught:
Process MultiprocessingProcess-1:
Traceback (most recent call last):
File "/usr/lib64/python3.4/multiprocessing/process.py", line 254, in _bootstrap
self.run()
File "/home/ezh/.virtualenvs/salt/lib/python3.4/site-packages/salt/utils/process.py", line 647, in _run
return self._original_run()
File "/usr/lib64/python3.4/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "/home/ezh/.virtualenvs/salt/lib/python3.4/site-packages/salt/client/ssh/__init__.py", line 426, in handle_routine
stdout, stderr, retcode = single.run()
File "/home/ezh/.virtualenvs/salt/lib/python3.4/site-packages/salt/client/ssh/__init__.py", line 851, in run
stdout, stderr, retcode = self.cmd_block()
File "/home/ezh/.virtualenvs/salt/lib/python3.4/site-packages/salt/client/ssh/__init__.py", line 1128, in cmd_block
stdout, stderr, retcode = self.shim_cmd(cmd_str)
File "/home/ezh/.virtualenvs/salt/lib/python3.4/site-packages/salt/client/ssh/__init__.py", line 1083, in shim_cmd
shim_tmp_file.write(cmd_str)
File "/home/ezh/.virtualenvs/salt/lib64/python3.4/tempfile.py", line 417, in func_wrapper
return func(*args, **kwargs)
TypeError: 'str' does not support the buffer interface
[ERROR   ] Target 'ezh' did not return any data, probably due to an error.

# Fixed code
def shim_cmd(self, cmd_str, extension="py"):
    """
    Run a shim command.

    If tty is enabled, we must scp the shim to the target system and
    execute it there
    """
    if not self.tty and not self.winrm:
        return self.shell.exec_cmd(cmd_str)

    # Write the shim to a temporary file in the default temp directory
    with tempfile.NamedTemporaryFile(
        mode="w+b", prefix="shim_", delete=False
    ) as shim_tmp_file:
        shim_tmp_file.write(salt.utils.to_bytes(cmd_str))

    # Copy shim to target system, under $HOME/.<randomized name>
    target_shim_file = ".{0}.{1}".format(binascii.hexlify(os.urandom(6)), extension)
    if self.winrm:
        target_shim_file = saltwinshell.get_target_shim_file(self)
    self.shell.send(shim_tmp_file.name, target_shim_file, makedirs=True)

    # Remove our shim file
    try:
        os.remove(shim_tmp_file.name)
    except IOError:
        pass

    # Execute shim
    if extension == "ps1":
        ret = self.shell.exec_cmd('"powershell {0}"'.format(target_shim_file))
    else:
        if not self.winrm:
            ret = self.shell.exec_cmd("/bin/sh '$HOME/{0}'".format(target_shim_file))
        else:
            ret = saltwinshell.call_python(self)

    # Remove shim from target system
    if not self.winrm:
        self.shell.exec_cmd("rm '$HOME/{0}'".format(target_shim_file))
    else:
        self.shell.exec_cmd("del {0}".format(target_shim_file))

    return ret