Python执行复杂的shell命令

neo*_*org 1 python

嗨我必须执行一个shell命令:diff <(ssh -n root@10.22.254.34 cat /vms/cloudburst.qcow2.*)<(ssh -n root@10.22.254.101 cat /vms/cloudburst.qcow2)我试过

cmd="diff <(ssh -n root@10.22.254.34 cat /vms/cloudburst.qcow2.*) <(ssh -n root@10.22.254.101 cat /vms/cloudburst.qcow2)"
args = shlex.split(cmd)
output,error = subprocess.Popen(args,stdout = subprocess.PIPE, stderr= subprocess.PIPE).communicate()
Run Code Online (Sandbox Code Playgroud)

但是我得到一个错误diff:额外的操作数cat

我对python很新.任何帮助,将不胜感激

Ned*_*der 7

您正在使用<(...)(进程替换)语法,该语法由shell解释.提供shell=True给Popen让它使用shell:

cmd = "diff <(ssh -n root@10.22.254.34 cat /vms/cloudburst.qcow2.*) <(ssh -n root@10.22.254.101 cat /vms/cloudburst.qcow2)"
output,error = subprocess.Popen(cmd, shell=True, executable="/bin/bash", stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
Run Code Online (Sandbox Code Playgroud)

由于您不需要Bourne shell(/ bin/sh),因此请使用executable参数来确定要使用的shell.