hug*_*own 60
如上所述,您可以使用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
Run Code Online (Sandbox Code Playgroud)
有几篇很好的文章,虽然你应该小心,因为它在过去的六个月里发生了变化:
现代Python黑客的工具:Virtualenv,Fabric和Pip
后来: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...
Run Code Online (Sandbox Code Playgroud)
这主要是化妆品,但是:ssh是paramiko的一个分支,两个库的维护者是相同的(Jeff Forcier,也是Fabric的作者),维护者计划以paramiko的名义重新统一paramiko和ssh.(通过pbanka进行此修正.)
Nei*_*eil 24
如果要避免任何额外的模块,可以使用子进程模块运行
ssh [host] [command]
Run Code Online (Sandbox Code Playgroud)
并捕获输出.
尝试类似的东西:
process = subprocess.Popen("ssh example.com ls", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output,stderr = process.communicate()
status = process.poll()
print output
Run Code Online (Sandbox Code Playgroud)
要处理用户名和密码,可以使用子进程与ssh进程交互,也可以在服务器上安装公钥以避免密码提示.
Seb*_*ack 17
我为libssh2编写了Python绑定.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)
Run Code Online (Sandbox Code Playgroud)
像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
Run Code Online (Sandbox Code Playgroud)
您还可以选择在程序运行时打印该程序的输出,如果您希望在退出之前查看长时间运行的命令的输出,这将非常有用:
result = shell.run(["echo", "-n", "hello"], stdout=sys.stdout)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
177951 次 |
| 最近记录: |