【发布时间】:2010-11-17 01:07:28
【问题描述】:
如何从本地 Python (3.0) 脚本简单地通过 SSH 连接到远程服务器、提供登录名/密码、执行命令并将输出打印到 Python 控制台?
我宁愿不使用任何大型外部库或在远程服务器上安装任何东西。
【问题讨论】:
如何从本地 Python (3.0) 脚本简单地通过 SSH 连接到远程服务器、提供登录名/密码、执行命令并将输出打印到 Python 控制台?
我宁愿不使用任何大型外部库或在远程服务器上安装任何东西。
【问题讨论】:
您可以按照上面的建议使用 Paramiko 自己编写代码。或者,您可以查看 Fabric,这是一个 Python 应用程序,可以完成您所询问的所有事情:
Fabric 是一个 Python 库,并且 命令行工具旨在 简化部署应用程序或 执行系统管理任务 通过 SSH 协议。它提供 用于运行任意 shell 的工具 命令(作为正常登录 用户,或通过 sudo),上传和 下载文件等。
我认为这符合您的需求。它也不是一个大型库,不需要安装服务器,尽管它确实依赖于 paramiko 和 pycrypt,需要在客户端安装。
* The official, canonical repository is git.fabfile.org
* The official Github mirror is GitHub/bitprophet/fabric
有几篇关于它的好文章,但你应该小心,因为它在过去六个月内发生了变化:
Tools of the Modern Python Hacker: Virtualenv, Fabric and Pip
Simple & Easy Deployment with Fabric and Virtualenv
后来:Fabric 不再需要安装 paramiko:
$ pip install fabric
Downloading/unpacking fabric
Downloading Fabric-1.4.2.tar.gz (182Kb): 182Kb downloaded
Running setup.py egg_info for package fabric
warning: no previously-included files matching '*' found under directory 'docs/_build'
warning: no files found matching 'fabfile.py'
Downloading/unpacking ssh>=1.7.14 (from fabric)
Downloading ssh-1.7.14.tar.gz (794Kb): 794Kb downloaded
Running setup.py egg_info for package ssh
Downloading/unpacking pycrypto>=2.1,!=2.4 (from ssh>=1.7.14->fabric)
Downloading pycrypto-2.6.tar.gz (443Kb): 443Kb downloaded
Running setup.py egg_info for package pycrypto
Installing collected packages: fabric, ssh, pycrypto
Running setup.py install for fabric
warning: no previously-included files matching '*' found under directory 'docs/_build'
warning: no files found matching 'fabfile.py'
Installing fab script to /home/hbrown/.virtualenvs/fabric-test/bin
Running setup.py install for ssh
Running setup.py install for pycrypto
...
Successfully installed fabric ssh pycrypto
Cleaning up...
这主要是装饰性的,但是:ssh 是 paramiko 的一个分支,两个库的维护者是相同的(Jeff Forcier,也是 Fabric 的作者)和 the maintainer has plans to reunite paramiko and ssh under the name paramiko。 (此更正来自pbanka。)
【讨论】:
我还没有尝试过,但是这个pysftp 模块可能会有所帮助,它又使用了 paramiko。我相信一切都是客户端。
有趣的命令可能是.execute(),它在远程机器上执行任意命令。 (该模块还具有 .get() 和 .put 方法,更多地暗示了它的 FTP 字符)。
更新:
在我最初链接到的博客文章不再可用后,我重新编写了答案。一些引用此答案旧版本的 cmets 现在看起来很奇怪。
【讨论】:
如果你想避免任何额外的模块,你可以使用子进程模块来运行
ssh [host] [command]
并捕获输出。
尝试类似:
process = subprocess.Popen("ssh example.com ls", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output,stderr = process.communicate()
status = process.poll()
print output
处理用户名和密码,可以使用子进程与ssh进程交互,也可以在服务器上安装公钥,避免密码提示。
【讨论】:
ssh 子进程提供密码可能很困难。见Why not just use a pipe (popen())?。您可能需要pty、pexpect 模块来解决它。
我写了Python bindings for libssh2。 Libssh2 是一个实现 SSH2 协议的客户端库。
import socket
import libssh2
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('exmaple.com', 22))
session = libssh2.Session()
session.startup(sock)
session.userauth_password('john', '******')
channel = session.channel()
channel.execute('ls -l')
print channel.read(1024)
【讨论】:
喜欢hughdbrown,我喜欢Fabric。请注意,虽然它实现了自己的声明性脚本(用于进行部署等),但它也可以作为 Python 模块导入并在您的程序中使用,而无需编写 Fabric 脚本。
Fabric 有一个新的维护者,正在重写;这意味着您(当前)在网络上找到的大多数教程都不适用于当前版本。此外,Google 仍将旧的 Fabric 页面显示为第一个结果。
有关最新文档,您可以查看:http://docs.fabfile.org
【讨论】:
我发现 paramiko 有点太低级了,而且 Fabric 并不是特别适合用作库,所以我将自己的库放在一起,名为 spur,它使用 paramiko 实现了一个更好的接口:
import spur
shell = spur.SshShell(hostname="localhost", username="bob", password="password1")
result = shell.run(["echo", "-n", "hello"])
print result.output # prints hello
您还可以选择在程序运行时打印程序的输出,如果您想在程序退出之前查看长时间运行的命令的输出,这很有用:
result = shell.run(["echo", "-n", "hello"], stdout=sys.stdout)
【讨论】:
为了那些到达这里谷歌搜索 python ssh 示例的人的利益。 原来的问题和答案现在几乎是旧的解码了。 似乎 paramiko 获得了一些功能(好吧。我承认 - 这里纯粹是猜测 - 我是 Python 新手),您可以直接使用 paramiko 创建 ssh 客户端。
import base64
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('192.168.1.1', username='user', password='password')
stdin, stdout, stderr = client.exec_command('cat /proc/meminfo')
for line in stdout:
print('... ' + line.strip('\n'))
client.close()
此代码改编自 https://github.com/paramiko/paramiko 的演示 它对我有用。
【讨论】:
这对我有用
import subprocess
import sys
HOST="IP"
COMMAND="ifconfig"
def passwordless_ssh(HOST):
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print >>sys.stderr, "ERROR: %s" % error
return "error"
else:
return result
【讨论】:
please refer to paramiko.org, its very useful while doing ssh using python.
import paramiko
import time
ssh = paramiko.SSHClient() #SSHClient() is the paramiko object</n>
#Below lines adds the server key automatically to know_hosts file.use anyone one of the below
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
#Here we are actually connecting to the server.
ssh.connect('10.106.104.24', port=22, username='admin', password='')
time.sleep(5)
#I have mentioned time because some servers or endpoint prints there own information after
#loggin in e.g. the version, model and uptime information, so its better to give some time
#before executing the command.
#Here we execute the command, stdin for input, stdout for output, stderr for error
stdin, stdout, stderr = ssh.exec_command('xstatus Time')
#Here we are reading the lines from output.
output = stdout.readlines()
print(output)
#Below all are the Exception handled by paramiko while ssh. Refer to paramiko.org for more information about exception.
except (BadHostKeyException, AuthenticationException,
SSHException, socket.error) as e:
print(e)
【讨论】: