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

  

def exec_command (shell_cmd、主机名=None):   如果主机名
:   
p=Popen (“/usr/bin/ssh tq”+主机名、shell=True, stdout=管,stdin=管)   
p.stdin.write (str.encode (shell_cmd))   
p.stdin.flush ()   
:   
p=Popen (shell_cmd shell=True, stdout=管,stdin=管)   
  
stdout, stderr=p.communicate ()   
返回stdout, stderr, p.poll ()

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