def better_exec_command(ssh, command, msg):
chan = ssh.get_transport().open_session()<EOL>chan.exec_command(command)<EOL>exit_status = chan.recv_exit_status()<EOL>if exit_status != <NUM_LIT:0>:<EOL><INDENT>msg_str = chan.recv_stderr(<NUM_LIT>)<EOL>err_msgs = []<EOL>while len(msg_str) > <NUM_LIT:0>:<EOL><INDENT>err_msgs.append(msg_str)<EOL>msg_str = chan.recv_stderr(<NUM_LIT>)<EOL><DEDENT>err_msg = '<STR_LIT>'.join(err_msgs)<EOL>logger.error(err_msg)<EOL>raise ParamikoError(msg, err_msg)<EOL><DEDENT>return chan<EOL>
Uses paramiko to execute a command but handles failure by raising a ParamikoError if the command fails.
    Note that unlike paramiko.SSHClient.exec_command this is not asynchronous because we wait until the exit status is known

    :Parameter ssh: a paramiko SSH Client
    :Parameter command: the command to execute
    :Parameter msg: message to print on failure

    :Returns (paramiko.Channel)
     the underlying channel so that the caller can extract stdout or send to stdin

    :Raises  SSHException: if paramiko would raise an SSHException
    :Raises  ParamikoError: if the command produces output to stderr