一、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
1 [root@server2 ~]# wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 2 [root@server2 ~]# tar jxf Python-2.7.5.tar.bz2 3 [root@server2 ~]# cd Python-2.7.5 4 [root@server2 Python-2.7.5]# ./configure --prefix=/usr/local/python2.7 5 [root@server2 Python-2.7.5]# make && make install 6 [root@server2 Python-2.7.5]# echo "PATH=/usr/local/python2.7/bin:$PATH" >> /etc/profile 7 [root@server2 Python-2.7.5]# source /etc/profile 8 [root@server2 Python-2.7.5]# python -V 9 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 >>>
3.安裝easy_install 工具
1 [root@server2 ~]# wget http://peak.telecommunity.com/dist/ez_setup.py 2 [root@server2 ~]# python ez_setup.py 3 Downloading http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg 4 Processing setuptools-0.6c11-py2.7.egg 5 Copying setuptools-0.6c11-py2.7.egg to /usr/local/python2.7/lib/python2.7/site-packages 6 Adding setuptools 0.6c11 to easy-install.pth file 7 Installing easy_install script to /usr/local/python2.7/bin 8 Installing easy_install-2.7 script to /usr/local/python2.7/bin 9 Installed /usr/local/python2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg 10 Processing dependencies for setuptools==0.6c11 11 Finished processing dependencies for setuptools==0.6c11
只需要執行以上命令,即可安裝好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
2
3
4
5
6
7
8
|
[root@server2
gmp-5.1.2]# python Python
2.7.5 ( default ,
Aug 16 2013, 07:56:41) [GCC
4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Type "help" , "copyright" , "credits" or "license" for more
information. >>>
import paramiko >>>
dir(paramiko) [ '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' , 'PasswordRequiredException' , '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' ] >>> |
一切正常,至此paramiko模塊在Linux上安裝完成
原文鏈接:http://www.cnblogs.com/shangzekai/p/5212305.html
安裝過程中可能遇到的問題以及解決方法,請參考:
2》python下setuptools安裝( No module named setuptools 解決方案)
3》【centos】 error command 'gcc' failed with exit status 1
4》 CentOS 5.8(x86_64)中,Python-2.7.5交互模式下方向鍵、退格鍵等出現亂碼。