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/ ,如需转载,请注明出处,否则将追究法律责任。