python - 如何使用python远程执行进程

标签 python ssh

我想使用 Python 连接到远程服务器并在远程服务器上执行一个进程。我希望能够获得进程的返回码和标准错误(如果有的话)。以前有没有人做过这样的事情。我已经使用 ssh 完成了,但我想从 Python 脚本中完成。

干杯。

最佳答案

使用 ssh module called paramiko它是为此目的而不是使用 subprocess 而创建的。下面是一个例子:

from paramiko import SSHClient
client = SSHClient()
client.load_system_host_keys()
client.connect("hostname", username="user")
stdin, stdout, stderr = client.exec_command('program')
print "stderr: ", stderr.readlines()
print "pwd: ", stdout.readlines()

更新:该示例曾经使用 ssh 模块,但现在已弃用,paramiko是在 python 中提供 ssh 功能的最新模块。

关于python - 如何使用python远程执行进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/946946/

相关文章:

python - 命名序列

python - 限制使用元素总数的背包

python - django logout() 不显示模板

python - SQLAlchemy 中的 PostgreSQL 多维数组

Bash 脚本,替换文件中的文本?

php - 使用 PHP 关闭远程 linux 服务器

ssh - Ansible: "sudo: a password is required\r\n"

python - 如何将我的 multiprocessing.mangers.BaseManager 子类使用的序列化程序更改为 cPickle?

linux - 增加ssh超时

git - 是否可以在不打开浏览器的情况下从 CLI 在 GitHub 上创建远程仓库?