【问题标题】:running rsync with quoted arguments using python's subprocess使用 python 的子进程运行带引号的参数的 rsync
【发布时间】:2017-06-08 07:55:01
【问题描述】:

我有一些使用subprocess 模块执行rsync 的自动化备份脚本。 在指定 ssh 身份文件的新要求之前一切都很好。

根据rsync 手册,ssh 身份应作为带引号的字符串传递:

-e "ssh -i /home/username/.ssh/identity"

但这将无法在subprocess 中运行:

subprocess.Popen(['rsync', '-avz', '-e "ssh -i /home/username/.ssh/identity"', '~/Downloads', 'root@remote_server:/lvm/archive/backup'])
<subprocess.Popen object at 0x7fd111939710>
rsync: Failed to exec ssh -i /home/username/.ssh/identity: No such file or directory (2)
rsync error: error in IPC code (code 14) at pipe.c(85) [sender=3.1.0]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in IPC code (code 14) at io.c(226) [sender=3.1.0]

有什么想法吗?

谢谢

【问题讨论】:

    标签: python subprocess rsync


    【解决方案1】:

    subprocess.Popen(['rsync', '-avz', '-e', 'ssh -i /home/username/.ssh/identity', '~/Downloads', 'root@remove_server:/lvm/archive/backup'])

    【讨论】:

    • 谢谢 :) 这么简单......但是为什么实际上不需要将引号传递给subprocess 的原因?
    • 这就是整个命令行rsync -avz -e "ssh -i /home/username/.ssh/identity" ~/Downloads root@remove_server:/lvm/archive/backup被shell解析并传递给exec()系统调用的方式。我只是手动解析的。 :-)
    • shlex.split('rsync -avz -e "ssh -i /home/username/.ssh/identity" ~/Downloads root@remove_server:/lvm/archive/backup') => ['rsync', '-avz', '-e', 'ssh -i /home/username/.ssh/identity', '~/Downloads', ' root@remove_server:/lvm/archive/backup']
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2016-04-06
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多