shell.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:centos-base-consul 作者: zeroc0d3lab 项目源码 文件源码
def run_cmd(pl, cmd, stdin=None, strip=True):
    '''Run command and return its stdout, stripped

    If running command fails returns None and logs failure to ``pl`` argument.

    :param PowerlineLogger pl:
        Logger used to log failures.
    :param list cmd:
        Command which will be run.
    :param str stdin:
        String passed to command. May be None.
    :param bool strip:
        True if the result should be stripped.
    '''
    try:
        p = Popen(cmd, shell=False, stdout=PIPE, stdin=PIPE)
    except OSError as e:
        pl.exception('Could not execute command ({0}): {1}', e, cmd)
        return None
    else:
        stdout, err = p.communicate(
            stdin if stdin is None else stdin.encode(get_preferred_output_encoding()))
        stdout = stdout.decode(get_preferred_input_encoding())
    return stdout.strip() if strip else stdout
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号