Linux上安裝paramiko模塊
一、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
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
三、Linux上安裝paramiko(以CentOS release 6.4爲例)
1.查看Python的版本,是否滿足要求
[root@mongo02 ~]# python -V Python 2.6.6
符合,不需要升級
若不符合,升級步驟如下:
下載安裝python2.7(如果gcc沒有安裝,請先安裝好gcc)
源碼安裝python2.7
[root@mongo02 ~]# wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 [root@mongo02 ~]# tar jxf Python-2.7.5.tar.bz2 [root@mongo02 ~]# cd Python-2.7.5 [root@mongo02 Python-2.7.5]# ./configure --prefix=/usr/local/python2.7 [root@mongo02 Python-2.7.5]# make && make install [root@mongo02 Python-2.7.5]# echo "PATH=/usr/local/python2.7/bin:$PATH" >> /etc/profile [root@mongo02 Python-2.7.5]# source /etc/profile [root@mongo02 Python-2.7.5]# python -V Python 2.7.5 [root@mongo02 Python-2.7.5]# 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. >>>
至此Python2.7 安裝成功
2.安裝easy_install 工具
[root@mongo02 ~]# wget http://peak.telecommunity.com/dist/ez_setup.py [root@mongo02 ~]# python ez_setup.py
3.安裝paramiko模塊
[root@mongo02 ~]# easy_install paramiko
安裝時報錯
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath. src/MD2.c:31:20: error: Python.h: No such file or directory src/MD2.c:131: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token In file included from src/MD2.c:147: src/hash_template.c:48: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ src/hash_template.c:59: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PyTypeObject’ src/hash_template.c: In function ‘newALGobject’: src/hash_template.c:69: warning: implicit declaration of function ‘PyObject_New’ src/hash_template.c:69: error: expected expression before ‘ALGobject’ src/hash_template.c: At top level: src/hash_template.c:76: error: expected ‘)’ before ‘*’ token src/hash_template.c:91: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token src/hash_template.c:110: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token src/hash_template.c:122: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token src/hash_template.c:162: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token src/hash_template.c:188: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token src/hash_template.c:190: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ALG_methods’ src/hash_template.c:199: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token src/hash_template.c:225: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ALGtype’ src/hash_template.c:271: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token src/hash_template.c:304: error: array type has incomplete element type src/hash_template.c:305: error: ‘PyCFunction’ undeclared here (not in a function) src/hash_template.c:305: error: expected ‘}’ before ‘ALG_new’ src/hash_template.c: In function ‘init_MD2’: src/hash_template.c:339: error: ‘PyObject’ undeclared (first use in this function) src/hash_template.c:339: error: (Each undeclared identifier is reported only once src/hash_template.c:339: error: for each function it appears in.) src/hash_template.c:339: error: ‘m’ undeclared (first use in this function) src/hash_template.c:351: error: ‘ALGtype’ undeclared (first use in this function) src/hash_template.c:351: error: ‘PyType_Type’ undeclared (first use in this function) src/hash_template.c:352: warning: implicit declaration of function ‘Py_InitModule’ src/hash_template.c:356: error: ‘o’ undeclared (first use in this function) src/hash_template.c:356: warning: implicit declaration of function ‘PyInt_FromLong’ src/hash_template.c:356: warning: implicit declaration of function ‘PyDict_SetItemString’ src/hash_template.c:356: warning: implicit declaration of function ‘PyModule_GetDict’ src/hash_template.c:356: warning: implicit declaration of function ‘Py_DECREF’ src/hash_template.c:360: warning: implicit declaration of function ‘PyErr_Occurred’ src/hash_template.c:361: warning: implicit declaration of function ‘Py_FatalError’ error: Setup script exited with error: command 'gcc' failed with exit status 1
解決辦法:
yum -y install python-dev* yum -y install libxml2 libxml2-dev* yum -y install libxslt1.1* libxslt*
再次執行easy_install成功
[root@test1 ~]# easy_install paramiko Installed /usr/lib/python2.6/site-packages/pycrypto-2.6.1-py2.6-linux-x86_64.egg Finished processing dependencies for paramiko
4.驗證導入paramiko模塊
[root@mongo02 ~]# python Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import paramiko >>> paramiko. Display all 126 possibilities? (y or n) >>> paramiko. Display all 126 possibilities? (y or n) paramiko.AUTH_FAILED paramiko.SFTPServerInterface( paramiko.__version_info__ paramiko.AUTH_PARTIALLY_SUCCESSFUL paramiko.SFTP_BAD_MESSAGE paramiko._version paramiko.AUTH_SUCCESSFUL paramiko.SFTP_CONNECTION_LOST paramiko.agent paramiko.Agent( paramiko.SFTP_EOF paramiko.auth_handler paramiko.AgentKey( paramiko.SFTP_FAILURE paramiko.ber paramiko.AuthHandler( paramiko.SFTP_NO_CONNECTION paramiko.buffered_pipe
導入模塊時,如果遇到以上問題,可能是由於gmp版本比較低造成的,gmp是什麼,看官網的解釋:
GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers.
看樣子是跟精度有關係
[root@mongo02 ~]# 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 /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. >>>
可以用以下方法解決:
先卸載低版本
[root@mongo02 ~]# rpm -qa | grep gmp gmp-devel-4.1.4-10.el5 gmp-4.1.4-10.el5 [root@mongo02 ~]# rpm -e gmp-devel-4.1.4-10.el5.i386 [root@mongo02 ~]# rpm -e gcc-gfortran-4.1.2-52.el5.i386 [root@mongo02 ~]# rpm -e gmp-4.1.4-10.el5 [root@mongo02 ~]# ldconfig
然後安裝高版本的gmp
[root@server2 ~]# wget http://ftp.gnu.org/gnu/gmp/gmp-5.1.2.tar.bz2 [root@server2 ~]# tar jxf gmp-5.1.2.tar.bz2 [root@server2 ~]# cd gmp-5.1.2 [root@server2 gmp-5.1.2]# ./configure && make && make install [root@server2 gmp-5.1.2]# echo "/usr/local/lib" >> /etc/ld.so.conf [root@server2 gmp-5.1.2]# ldconfig
再次嘗試導入paramiko模塊:
>>> import paramiko >>> dir(paramiko) ['AUTH_FAILED', 'AUTH_PARTIALLY_SUCCESSFUL', 'AUTH_SUCCESSFUL', 'Agent', 'AgentKey', 'AuthHandler', 'AuthenticationException', 'AutoAddPolicy', 'BadAuthenticationType', 'BadHostKeyException', 'BaseSFTP', 'BufferedFile', 'Channel', 'ChannelException', 'ChannelFile', 'DSSKey', 'ECDSAKey', 'GSSAuth', 'GSS_AUTH_AVAILABLE', '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__', '__name__', '__package__', '__path__', '__version__', '__version_info__', '_version', 'agent', 'auth_handler', 'ber', 'buffered_pipe', 'channel', 'client', 'common', 'compress', 'config', 'dsskey', 'ecdsakey', 'file', 'hostkeys', 'io_sleep', 'kex_gex', 'kex_group1', 'kex_group14', 'kex_gss', 'message', 'packet', 'pipe', 'pkey', 'primes', 'proxy', 'py3compat', 'resource', 'rsakey', 'server', 'sftp', 'sftp_attr', 'sftp_client', 'sftp_file', 'sftp_handle', 'sftp_server', 'sftp_si', 'ssh_exception', 'ssh_gss', 'sys', 'transport', 'util'] >>>
一切正常,至此paramiko模塊在Linux上安裝完成