我怎么能简单地SSH从本地的Python(3.0)脚本的远程服务器上,提供一个登录/密码,执行命令,并打印输出到Python的控制台?
我宁可不使用任何大的外部库或安装在远程服务器上的任何东西。
我怎么能简单地SSH从本地的Python(3.0)脚本的远程服务器上,提供一个登录/密码,执行命令,并打印输出到Python的控制台?
我宁可不使用任何大的外部库或安装在远程服务器上的任何东西。
我还没有尝试过,但是这pysftp模块也许会有帮助,这反过来使用的paramiko。 我相信一切都是客户端。
有趣的命令可能是.execute()
执行该远程机器上的任意命令。 (该模块还具有.get()
和.put
方法,其暗示更多其FTP字符)。
更新:
我已经重写了答案博客文章我原来挂到了后不可用。 一些引用旧版本这个答案的评论现在看起来很怪异。
您可以使用自己的paramiko它的代码,如上建议。 或者,你可以看看面料,做你问的事情Python应用程序:
织物是Python库和命令行工具旨在简化部署应用程序或经由SSH协议来执行系统管理任务。 它提供了用于运行任意shell命令(作为正常登录用户,或经由须藤),上载和下载的文件,等等工具。
我认为这符合您的需求。 它也不是一个大型图书馆,不需要安装服务器,虽然它确实有需要在客户端上安装上的paramiko和pycrypt依赖。
所使用的应用程序是在这里 。 现在可以找到这里 。
* The official, canonical repository is git.fabfile.org
* The official Github mirror is GitHub/bitprophet/fabric
有就可以了几篇好文章,但因为它在过去六个月已经改变了你要小心:
Django的部署与面料
VIRTUALENV,面料和PIP:现代的Python黑客工具
简单和易于部署与面料和VIRTUALENV
后来:面料不再需要的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的一个分支,这两个的维护者是相同的(杰夫Forcier,面料也是笔者),和维护者有计划地团聚的名义下的paramiko的paramiko和ssh 。 (通过这种校正pbanka 。)
如果你想避免任何额外的模块,你可以使用子模块运行
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流程交互,或者你可以在服务器上安装一个公共密钥以避免密码提示。
我已经写了Python绑定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)
你的“最简单”的定义是很重要的位置 - 简单的代码意味着使用一个模块(虽然“大外部库”是一个夸张)。
我相信最先进最新的(积极开发)模块的paramiko 。 它配备了演示脚本在下载,并有详细的在线API文档。 您也可以尝试PxSSH ,它包含在Pexpect的 。 有与第一链接的文档沿短样品。
再次相对于简单,注意良好的错误检测始终是不会让你的代码看起来更加复杂,但你应该能够重复使用大量的代码示例脚本,然后忘掉它。
像hughdbrown,我喜欢的面料。 请注意,虽然实现了自己的声明式脚本(用于将部署和对等),也可以导入为Python模块,并在你的程序中使用,而无需编写一个脚本面料。
织物具有新的维护者和处于被rewriten进程; 这意味着,在网络上,你会(目前)发现大多数教程不会与目前的版本。 此外,谷歌还表示,老布页面作为第一个结果。
对于最新的文档,你可以检查: http://docs.fabfile.org
我发现的paramiko是有点太低级,和Fabric不是特别适合于用作图书馆,所以我把我的自己的图书馆称为正使用的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)
对于那些谁来到这里google搜索蟒蛇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
看看spurplus ,周围的包装鞭策和的paramiko是我们开发的管理远程机器和进行文件操作。
Spurplus提供check_output()
函数外的现成:
import spurplus
with spurplus.connect_with_retries(
hostname='some-machine.example.com', username='devop') as shell:
out = shell.check_output(['/path/to/the/command', '--some_argument'])
print(out)