一、paramiko模块有什么用?
paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。由于使用的是python这样的能够跨平台运行的语言,所以所有python支持的平台,如Linux, Solaris, BSD, MacOS X, Windows等,paramiko都可以支持,因此,如果需要使用SSH从一个平台连接到另外一个平台,进行一系列的操作时,比如:批量执行命令,批量上传文件等操作,paramiko是最佳工具之一。
目前新的版本,官网在此:
https://github.com/paramiko/paramiko
旧版本在此:
http://www.lag.net/paramiko/legacy.html
要安装新版本paramiko模块需要做以下准备:
1.Python2.5+ 版本(Linux, Unix, Windows都可以),这里就直接安装Python2.7
下载地址:http://www.python.org
2.PyCrypto 2.1+ 模块(PyCrypto是使用Python编写的加密工具包)
下载地址:https://www.dlitz.net/software/pycrypto/
3.easy_install 工具(是Python安装模块的一个工具,像yum,可以自动解决依赖)
下载地址: http://peak.telecommunity.com/dist/ez_setup.py
如果大家感觉安装paramiko还是略有麻烦的话,当使用到paramiko提供的方便时便会觉得这是十分值得的。
二、Linux上安装paramiko(以CentOS 5.8 32位 为例)
1.查看Python的版本,是否满足要求
1.
[root
@server1
~]# python -V
2.
Python
2.4
.
3
3.
[root
@server1
~]#
Windows上亦是此命令,注意是大写V
是2.4.3版本,需要升级
2.下载安装python2.7
源码安装Python2.7
如果没有gcc没有安装,请先安装好gcc
01.
root
@server2
~]# wget http:
//www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
02.
[root
@server2
~]# tar jxf Python-
2.7
.
5
.tar.bz2
03.
[root
@server2
~]# cd Python-
2.7
.
5
04.
[root
@server2
Python-
2.7
.
5
]# ./configure --prefix=/usr/local/python2.
7
05.
[root
@server2
Python-
2.7
.
5
]# make && make install
06.
[root
@server2
Python-
2.7
.
5
]# echo
"PATH=/usr/local/python2.7/bin:$PATH"
>> /etc/profile
07.
[root
@server2
Python-
2.7
.
5
]# source /etc/profile
08.
[root
@server2
Python-
2.7
.
5
]# python -V
09.
Python
2.7
.
5
10.
[root
@server2
Python-
2.7
.
5
]# python
11.
Python
2.7
.
5
(
default
, Aug
16
2013
,
07
:
56
:
41
)
12.
[GCC
4.1
.
2
20080704
(Red Hat
4.1
.
2
-
52
)] on linux2
13.
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
14.
>>>
至此Python2.7 安装成功
3.安装easy_install 工具
下载该脚本
1.
[root
@server2
~]# wget http:
//peak.telecommunity.com/dist/ez_setup.py
2.
[root
@server2
~]# python ez_setup.py
只需要执行以上命令,即可安装好easy_install 工具
4.安装paramiko模块
使用以下命令,即可安装好paramiko模块
1.
[root
@server2
~]# easy_install paramiko
进入Python导入paramiko一下试试
1.
[root
@server2
~]# python
2.
Python
2.7
.
5
(
default
, Aug
16
2013
,
07
:
56
:
41
)
3.
[GCC
4.1
.
2
20080704
(Red Hat
4.1
.
2
-
52
)] on linux2
4.
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
5.
>>>
import
paramiko
6.
/usr/local/python2.
7
/lib/python2.
7
/site-packages/pycrypto-
2.6
-py2.
7
-linux-i686.egg/Crypto/Util/number.py:
57
: PowmInsecureWarning: Not using mpz_powm_sec. You should rebuild using libgmp >=
5
to avoid timing attack vulnerability.
7.
>>>
导入模块时,如果遇到以上问题,可能是由于gmp版本比较低造成的,gmp是啥,看官网的解释:
GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers.
看样子是跟精度有关系
可以用以下方法解决:
先卸载低版本
1.
root
@server2
~]# rpm -qa | grep gmp
2.
gmp-devel-
4.1
.
4
-
10
.el5
3.
gmp-
4.1
.
4
-
10
.el5
4.
[root
@server2
~]# rpm -e gmp-devel-
4.1
.
4
-
10
.el5.i386
5.
[root
@server2
~]# rpm -e gcc-gfortran-
4.1
.
2
-
52
.el5.i386
6.
[root
@server2
~]# rpm -e gmp-
4.1
.
4
-
10
.el5
7.
[root
@server2
~]# ldconfig
8.
[root
@server2
~]#
然后安装高版本的gmp
1.
[root
@server2
~]# wget http:
//ftp.gnu.org/gnu/gmp/gmp-5.1.2.tar.bz2
2.
[root
@server2
~]# tar jxf gmp-
5.1
.
2
.tar.bz2
3.
[root
@server2
~]# cd gmp-
5.1
.
2
4.
[root
@server2
gmp-
5.1
.
2
]# ./configure && make && make install
5.
[root
@server2
gmp-
5.1
.
2
]# echo
"/usr/local/lib"
>> /etc/ld.so.conf
6.
[root
@server2
gmp-
5.1
.
2
]# ldconfig
再次尝试导入paramiko模块:
1.
[root
@server2
gmp-
5.1
.
2
]# python
2.
Python
2.7
.
5
(
default
, Aug
16
2013
,
07
:
56
:
41
)
3.
[GCC
4.1
.
2
20080704
(Red Hat
4.1
.
2
-
52
)] on linux2
4.
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
5.
>>>
import
paramiko
6.
>>> dir(paramiko)
7.
[
'AUTH_FAILED'
,
'AUTH_PARTIALLY_SUCCESSFUL'
,
'AUTH_SUCCESSFUL'
,
'Agent'
,
'AgentKey'
,
'AuthHandler'
,
'AuthenticationException'
,
'AutoAddPolicy'
,
'BadAuthenticationType'
,
'BadHostKeyException'
,
'BaseSFTP'
,
'BufferedFile'
,
'Channel'
,
'ChannelException'
,
'ChannelFile'
,
'DSSKey'
,
'HostKeys'
,
'InteractiveQuery'
,
'Message'
,
'MissingHostKeyPolicy'
,
'OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED'
,
'OPEN_FAILED_CONNECT_FAILED'
,
'OPEN_FAILED_RESOURCE_SHORTAGE'
,
'OPEN_FAILED_UNKNOWN_CHANNEL_TYPE'
,
'OPEN_SUCCEEDED'
,
'PKey'
,
'Packetizer'
,
'Pass<a href="http://www.it165.net/edu/ebg/" target="_blank" class="keylink">word</a>RequiredException'
,
'ProxyCommand'
,
'ProxyCommandFailure'
,
'RSAKey'
,
'RejectPolicy'
,
'SFTP'
,
'SFTPAttributes'
,
'SFTPClient'
,
'SFTPError'
,
'SFTPFile'
,
'SFTPHandle'
,
'SFTPServer'
,
'SFTPServerInterface'
,
'SFTP_BAD_MESSAGE'
,
'SFTP_CONNECTION_LOST'
,
'SFTP_EOF'
,
'SFTP_FAILURE'
,
'SFTP_NO_CONNECTION'
,
'SFTP_NO_SUCH_FILE'
,
'SFTP_OK'
,
'SFTP_OP_UNSUPPORTED'
,
'SFTP_PERMISSION_DENIED'
,
'SSHClient'
,
'SSHConfig'
,
'SSHException'
,
'SecurityOptions'
,
'ServerInterface'
,
'SubsystemHandler'
,
'Transport'
,
'WarningPolicy'
,
'__all__'
,
'__author__'
,
'__builtins__'
,
'__doc__'
,
'__file__'
,
'__license__'
,
'__loader__'
,
'__name__'
,
'__package__'
,
'__path__'
,
'__version__'
,
'agent'
,
'auth_handler'
,
'ber'
,
'buffered_pipe'
,
'channel'
,
'client'
,
'common'
,
'compress'
,
'config'
,
'dsskey'
,
'file'
,
'hostkeys'
,
'io_sleep'
,
'kex_gex'
,
'kex_group1'
,
'message'
,
'packet'
,
'pipe'
,
'pkey'
,
'primes'
,
'proxy'
,
'resource'
,
'rsakey'
,
'server'
,
'sftp'
,
'sftp_attr'
,
'sftp_client'
,
'sftp_file'
,
'sftp_handle'
,
'sftp_server'
,
'sftp_si'
,
'ssh_exception'
,
'sys'
,
'transport'
,
'util'
]
8.
>>>
一切正常,至此paramiko模块在Linux上安装完成
三、Windows7 32上安装paramiko模块
1.下载安装python2.7
用下载工具下载这2个文件
http://www.python.org/ftp/python/2.7.5/python-2.7.5.msi
http://peak.telecommunity.com/dist/ez_setup.py
默认安装即可,默认安装路径 C:\Python27,就不演示安装过程了。
安装之后把C:\Python27添加到系统的Path变量里面,然后在命令行下输入python测试一下。
1.
C:\Users\TestAA>python
2.
Python
2.7
.
5
(
default
, May
15
2013
,
22
:
43
:
36
) [MSC v.
1500
32
bit (Intel)] on win32
3.
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
4.
>>>
看到以上结果,代表python已经安装成功。
2.安装easy_install工具
01.
C:\Users\TestAA\Desktop>python ez_setup.py
02.
Downloading http:
//pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-p
03.
y2.
7
.egg
04.
Processing setuptools-
0
.6c11-py2.
7
.egg
05.
Copying setuptools-
0
.6c11-py2.
7
.egg to c:\python27\lib\site-packages
06.
Adding setuptools
0
.6c11 to easy-install.pth file
07.
Installing easy_install-script.py script to C:\Python27\Scripts
08.
Installing easy_install.exe script to C:\Python27\Scripts
09.
Installing easy_install.exe.manifest script to C:\Python27\Scripts
10.
Installing easy_install-
2.7
-script.py script to C:\Python27\Scripts
11.
Installing easy_install-
2.7
.exe script to C:\Python27\Scripts
12.
Installing easy_install-
2.7
.exe.manifest script to C:\Python27\Scripts
13.
Installed c:\python27\lib\site-packages\setuptools-
0
.6c11-py2.
7
.egg
14.
Processing dependencies
for
setuptools==
0
.6c11
15.
Finished processing dependencies
for
setuptools==
0
.6c11
再把 C:\Python27\Scripts 该路径添加到 系统的Path 变量里面,这样就可以直接使用easy_install命令了。
3.安装PyCrypto模块
由于在Windows上平台上不管使用手工编译,还是用easy_install命令对该模块进行安装的话,非常
容易出现问题,而且特别的麻烦,所以已经有人帮我们编译好了Windows平台上对应的版本,只需要下载安装即可 www.it165.net
到这个位置下载对应的版本 pycrypto-2.6.win32-py2.7
http://www.voidspace.org.uk/python/modules.shtml#pycrypto
或者 到我的网盘进行下载
http://pan.baidu.com/share/link?shareid=3896105660&uk=839171987
安装过程,只需要下一步下一步即可。
4.使用easy_install安装paramiko模块
01.
C:\Users\TestAA>easy_install paramiko
02.
Searching
for
paramiko
03.
Reading http:
//pypi.python.org/simple/paramiko/
04.
Best match: paramiko
1.11
.
0
05.
Downloading https:
//pypi.python.org/packages/source/p/paramiko/paramiko-1.11.0.t
06.
ar.gz#md5=a2c55dc04904bd08d984533703177084
07.
Processing paramiko-
1.11
.
0
.tar.gz
08.
Running paramiko-
1.11
.
0
\setup.py -q bdist_egg --dist-dir c:\users\testaa\appdata
09.
\local\temp\easy_install-rla6aa\paramiko-
1.11
.
0
\egg-dist-tmp-aynqgv
10.
zip_safe flag not set; analyzing archive contents...
11.
Adding paramiko
1.11
.
0
to easy-install.pth file
12.
Installed c:\python27\lib\site-packages\paramiko-
1.11
.
0
-py2.
7
.egg
13.
Processing dependencies
for
paramiko
14.
Finished processing dependencies
for
paramiko
15.
C:\Users\TestAA>
进入Python导入paramiko一下看看,
01.
C:\Users\TestAA>python
02.
Python
2.7
.
5
(
default
, May
15
2013
,
22
:
43
:
36
) [MSC v.
1500
32
bit (Intel)] on win
03.
32
04.
Type
"help"
,
"copyright"
,
"credits"
or
"license"
for
more information.
05.
>>>
import
paramiko
06.
>>> dir(paramiko)
07.
[
'AUTH_FAILED'
,
'AUTH_PARTIALLY_SUCCESSFUL'
,
'AUTH_SUCCESSFUL'
,
'Agent'
, 'AgentK
08.
ey
', '
AuthHandler
', '
AuthenticationException
', '
AutoAddPolicy
', '
BadAuthenticati
09.
onType
', '
BadHostKeyException
', '
BaseSFTP
', '
BufferedFile
', '
Channel
', '
ChannelE
10.
xception
', '
ChannelFile
', '
DSSKey
', '
HostKeys
', '
InteractiveQuery
', '
Message
', '
11.
MissingHostKeyPolicy
', '
OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
', '
OPEN_FAILED_C
12.
ONNECT_FAILED
', '
OPEN_FAILED_RESOURCE_SHORTAGE
', '
OPEN_FAILED_UNKNOWN_CHANNEL_TY
13.
PE
', '
OPEN_SUCCEEDED
', '
PKey
', '
Packetizer
', '
Pass<a href=
"http://www.it165.net/edu/ebg/"
target=
"_blank"
class
=
"keylink"
>word</a>RequiredException
', '
Proxy
14.
Command
', '
ProxyCommandFailure
', '
RSAKey
', '
RejectPolicy
', '
SFTP
', '
SFTPAttribut
15.
es
', '
SFTPClient
', '
SFTPError
', '
SFTPFile
', '
SFTPHandle
', '
SFTPServer
', '
SFTPSer
16.
verInterface
', '
SFTP_BAD_MESSAGE
', '
SFTP_CONNECTION_LOST
', '
SFTP_EOF
', '
SFTP_FAI
17.
LURE
', '
SFTP_NO_CONNECTION
', '
SFTP_NO_SUCH_FILE
', '
SFTP_OK
', '
SFTP_OP_UNSUPPORTE
18.
D
', '
SFTP_PERMISSION_DENIED
', '
SSHClient
', '
SSHConfig
', '
SSHException
', '
Securit
19.
yOptions
', '
ServerInterface
', '
SubsystemHandler
', '
Transport
', '
WarningPolicy',
20.
'__all__'
,
'__author__'
,
'__builtins__'
,
'__doc__'
,
'__file__'
,
'__license__'
, '
21.
__loader__
', '
__name__
', '
__package__
', '
__path__
', '
__version__
', '
agent
', '
aut
22.
h_handler
', '
ber
', '
buffered_pipe
', '
channel
', '
client
', '
common
', '
compress
', '
23.
config
', '
dsskey
', '
file
', '
hostkeys
', '
io_sleep
', '
kex_gex
', '
kex_group1
', '
mes
24.
sage
', '
packet
', '
pipe
', '
pkey
', '
primes
', '
proxy
', '
resource
', '
rsakey
', '
serve
25.
r
', '
sftp
', '
sftp_attr
', '
sftp_client
', '
sftp_file
', '
sftp_handle
', '
sftp_server
26.
', '
sftp_si
', '
ssh_exception
', '
sys
', '
transport
', '
util']
27.
>>>
看到以上情况,说明paramiko已经成功安装了
转自http://www.it165.net/admin/html/201308/1667.html