成功:
>>> scp_cmd = r"sudo scp -i /home/backup/.ssh/id_rsa /opt/backups/*conf backup@a-hostname.local:/opt/backups/"
>>> subprocess.call(scp_cmd, shell=True)
1eadmin1.conf 100% 83KB 83.5KB/s 00:00
1stflr_1.conf 100% 2904 2.8KB/s 00:00
>>> scp_cmd = """sudo scp -i /home/backup/.ssh/id_rsa /opt/backups/*conf backup@a-hostname.local:/opt/backups/"""
>>> os.system(scp_cmd)
1eadmin1.conf 100% 83KB 87.3KB/s 00:00
1stflr_1.conf 100% 2904 3.4KB/s 00:00
失败:
>>> scp_cmd = r"""sudo scp -i /home/backup/.ssh/id_rsa /opt/backups/*conf backup@a-hostname.local:/opt/backups/"""
>>> subprocess.call(scp_cmd, shell=True)
/opt/backups/*conf: No such file or directory
1
>>> subprocess.call(scp_cmd.split(' '))
/opt/backups/\*conf: No such file or directory
1
>>>
>>> subprocess.call(shlex.split(scp_cmd))
/opt/backups/*conf: No such file or directory
1
我很困惑为什么三引号在我使用时失败subprocess.call()
,但在我使用时通过os.system()
。为什么处理三引号字符串之间subprocess.call()
和处理时有区别?os.system()