ITPub博客

首页 > 自动化运维 > DevOps > Python执行操作系统命令并取得返回值和退出码,支持有互信的远程执行

Python执行操作系统命令并取得返回值和退出码,支持有互信的远程执行

原创 DevOps 作者:wpgy 时间:2020-03-03 23:10:31 0 删除 编辑

def exec_command(shell_cmd, hostname=None):
    if hostname:
        p = Popen('/usr/bin/ssh -Tq '+hostname, shell=True, stdout=PIPE, stdin=PIPE)
        p.stdin.write(str.encode(shell_cmd))
        p.stdin.flush()
    else:
        p = Popen(shell_cmd, shell=True, stdout=PIPE, stdin=PIPE)
    
    stdout, stderr = p.communicate()
    return stdout, stderr, p.poll()

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31560527/viewspace-2678384/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
擅长: Oracle、MySQL, Python、PHP、Web 开发; 研究领域:数据库技术、自动化运维

注册时间:2018-11-01

  • 博文量
    29
  • 访问量
    39461