python - python 2.7发送的远程ssh命令
问题描述
试图弄清楚如何发送 ssh 命令。这通过 cli 工作:
ssh -i /path/myKey.pem centos@myServer.com lsb_release -a
但是当我像这样设置命令时失败:
cmd = ['ssh', '-i', '/path/myKey.pem', 'centos@myServer.com', 'lsb_release', '-a']):
p = Popen( cmd, shell=True, stdout=PIPE, stderr=PIPE )
( output, errStr ) = p.communicate()
我得到这个错误号 255:
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
#Snip other usage
有人有什么想法吗?
解决方案
与列表一起使用shell=True
,您正在运行相当于
sh -c ssh -i /path/myKey.pem centos@myServer.com lsb_release -a
这不是你想要的;它运行ssh
,但它-i
用作 的值$0
,而不是 的第一个参数ssh
。这/path/myKey.pem
看起来像您要连接的主机的地址。
只是下降shell=True
。
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
lsb_release -a
虽然在这里不是问题,但 Klaus D. 建议作为单个参数传递通常是一个好主意。ssh
必须将它们连接成一个字符串才能传递到sh -c
远程端;不妨自己动手,以确保正确完成。
cmd = ['ssh', '-i', '/path/myKey.pem', 'centos@myServer.com', 'lsb_release -a']
推荐阅读
- proxy - 自动代理 pac 和有什么区别
- c# - 由 HtmlAgilityPack 解析的 HTML 文档返回 null
- forms - Symfony 中的自动页面返回链接
- math - 将vector(x,y,z)坐标转换为0到32767之间的整数
- objective-c - 如何检测到 watchOS 应用程序已终止?
- mysql - /writeReview/1 __str__ 处的 Django 模型 TypeError 返回非字符串(int 类型)
- angular - 初始加载后如何显示方向数据?
- android - Android可以在不杀死托管它的活动的情况下杀死Fragment吗?
- html - 一些按钮在 iOS 上显示为圆圈
- python - 如何在训练 tensorflow.keras 期间替换损失函数