python - How to parse a variables to subprocess.run in python
问题描述
I am new to python. I have install.py. Below are the variables stored its values. When execute/run abc.py, it's unable to copy the file from remote to local server. When samething is hardcoded values in subprocess commands, able to transfer file, but passing variable in subprocess not working. Referred other articles, but no luck.
srcuser=abcd srcip=x.x.x.x srcpath=/home/sum/mnt/ sshprkey=/home/xyz/id_rsa
subprocess.run(['sudo', 'scp', '-P22', '-i sshprkey', 'srcuser@srcip:srcpath/"mongodb-org-3.6.repo"', '/etc/yum.repos.d/'])
Thanks
解决方案
试试这个,我们将 shell 关键字参数分配给 true。
subprocess.run(['sudo', 'scp', '-P22', '-i sshprkey', 'srcuser@srcip:srcpath/"mongodb-org-3.6.repo"', '/etc/yum.repos.d/'], shell=True)
使用 Popen(从 python 文档复制) 在 Unix 上,shell=True,shell 默认为 /bin/sh。如果 args 是字符串,则该字符串指定要通过 shell 执行的命令。这意味着字符串的格式必须与在 shell 提示符下键入时的格式完全相同。这包括,例如,引用或反斜杠转义文件名,其中包含空格。如果 args 是一个序列,则第一项指定命令字符串,任何附加项将被视为 shell 本身的附加参数。
您需要确保第一项指定命令字符串
Popen(['sudo scp', '-P22', '-i sshprkey', 'srcuser@srcip:srcpath/"mongodb-org-3.6.repo"', '/etc/yum.repos.d/'],shell=True)
推荐阅读
- python - Python3:模块'tabula'没有属性'read_pdf'
- node.js - 集群模式下的 PM2,重新启动后不要启动,直到我删除文件夹
- machine-learning - 日期列上的添加是否会导致过度拟合?
- import - 使用colab导入tensorrt时出现错误如何解决
- expo - expo 和 ui-kitten 中的 React-device-info 问题
- node.js - NodeJS(服务器):ReferenceError:当类型:模块时未定义要求
- php - SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列'items.box_box_barcode'
- ios - 如果在我的 webkit webview 中单击 Amazon 链接或 facebook 链接它将打开应用程序,或者如果找不到它将在浏览器中打开,我该怎么做
- python - 使用txt文件登录
- sql - 循环传递表 2 中的每条记录并根据表 1 更新新编号