paramiko介绍(全是洋文,看不懂啊,赶紧有道翻译吧,等有朝一日,我去报个华尔街):
"Paramiko" is a combination of the esperanto words for "paranoid" and "friend". It's a module for Python 2.6+ that implements the SSH2 protocol for secure (encrypted and authenticated) connections to remote machines. Unlike SSL (aka TLS), SSH2 protocol does not require hierarchical certificates signed by a powerful central authority. You may know SSH2 as the protocol that replaced Telnet and rsh for secure access to remote shells, but the protocol also includes the ability to open arbitrary channels to remote services across the encrypted tunnel (this is how SFTP works, for example).
It is written entirely in Python (no C or platform-dependent code) and is released under the GNU Lesser General Public License (LGPL). The package and its API is fairly well documented in the "doc/" folder that should have come with this archive.
罗总帮忙给翻译的(瞬间感觉我自己弱爆了):
"Paramiko" 是针对paranoid 和friend 的一种通用组合。对于python2.6以上的模块,这些模块能够应用SSH2安全协议。不像SSL(aka TLS)这种协议,SSH2协议并不要求有权威机构发布的等级证书。你可能知道SSH2作为一种协议已经代替了用于远程登录和执行一个远端命令这种安全登录模式来远程操作shells,但是协议也可以通过加密通道来打开任意的通道来实现远程服务(这就是SFTP怎样来实现工作的,例如)
它完全由python编写,并没有使用C或者依赖平台的代码方式),并且由LGPL授权发布新的版本。这个包和它的API很方便使用doc方式写入存档。
github地址:
https://github.com/paramiko/paramiko
paramiko安装:
安装环境:ubuntu-14.04.2
1、首先确定一下gcc是否安装,如果没有安装的话需要安装一下
# 安装命令
sudo apt-get install gcc
2、需要安装PyCrypto
下载地址:https://www.dlitz.net/software/pycrypto/
# 我下载的是pycrypto-2.6..tar.gz tar -zxf pycrypto-2.6..tar.gz
cd pycrypto-2.6./
python setup.py build
sudo python setup.py install 检查是否安装成功:
进入python解释环境下导入Crypto模块,没有报错说明安装成功:
>>> import Crypto
注意:在python3编译pycrypto的时候需要先安装一下python3-dev 否则会报以下错误
此错误解决方法参考:http://gangmax.me/blog/2015/06/02/resolve-python-pycrypto-installation-error/
ubuntu安装方法:sudo apt-get install python3-dev
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash._MD2' extension
x86_64-linux-gnu-gcc -pthread -fwrapv -Wall -Wstrict-prototypes -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/usr/include/python3.4m -c src/MD2.c -o build/temp.linux-x86_64-3.4/src/MD2.o
src/MD2.c:31:20: fatal error: Python.h: 没有那个文件或目录
#include "Python.h"
^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
3、安装paramiko
sudo pip install paramiko
如果没有安装pip的可以参考:http://www.cnblogs.com/CongZhang/p/5111195.html 检查paramiko是否安装成功:
进入python解释器环境下导入paramiko模块,没有报错说明安装成功:
>>> import paramiko
4、牛刀小试
#!/usr/bin/env python
# -*- coding: utf- -*- # 本程序在python .7下运行 import paramiko def ssh_connect(ip, port, user_name, passwd, shell):
ssh = paramiko.SSHClient()
# paramiko.util.log_to_file('/dev/null')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username=user_name, password=passwd)
stdin, stdout, stderr = ssh.exec_command(shell) # 执行命令 print stdout.readlines() # 输出执行命令的正确输出结果
print stderr.readlines() # 输出执行命令的错误输出结果 ip = '1.1.1.1' # 服务器ip地址
port = # 服务器ssh连接端口
user_name = 'xxxx' # ssh登陆帐号
passwd = 'xxxxx' # ssh登陆密码
shell = 'sudo ifconfig' # 执行命令
ssh_connect(ip, port, user_name, passwd, shell)