一、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的版本,是否满足要求
[root@server1 ~]# python -V Python 2.4.3 [root@server1 ~]#
Windows上亦是此命令,注意是大写V
是2.4.3版本,需要升级
2.下载安装python2.7
源码安装Python2.7
如果没有gcc没有安装,请先安装好gcc
root@server2 ~]# wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 [root@server2 ~]# tar jxf Python-2.7.5.tar.bz2 [root@server2 ~]# cd Python-2.7.5 [root@server2 Python-2.7.5]# ./configure --prefix=/usr/local/python2.7 [root@server2 Python-2.7.5]# make && make install [root@server2 Python-2.7.5]# echo "PATH=/usr/local/python2.7/bin:$PATH" >> /etc/profile [root@server2 Python-2.7.5]# source /etc/profile [root@server2 Python-2.7.5]# python -V Python 2.7.5 [root@server2 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 安装成功
3.安装easy_install 工具
下载该脚本
[root@server2 ~]# wget http://peak.telecommunity.com/dist/ez_setup.py [root@server2 ~]# python ez_setup.py Downloading http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg Processing setuptools-0.6c11-py2.7.egg Copying setuptools-0.6c11-py2.7.egg to /usr/local/python2.7/lib/python2.7/site-packages Adding setuptools 0.6c11 to easy-install.pth file Installing easy_install script to /usr/local/python2.7/bin Installing easy_install-2.7 script to /usr/local/python2.7/bin Installed /usr/local/python2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg Processing dependencies for setuptools==0.6c11 Finished processing dependencies for setuptools==0.6c11
只需要执行以上命令,即可安装好easy_install 工具
4.安装paramiko模块
使用以下命令,即可安装好paramiko模块
[root@server2 ~]# easy_install paramiko
进入Python导入paramiko一下试试
[root@server2 ~]# 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. >>>
导入模块时,如果遇到以上问题,可能是由于gmp版本比较低造成的,gmp是啥,看官网的解释:
GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers.
看样子是跟精度有关系
可以用以下方法解决:
先卸载低版本
root@server2 ~]# rpm -qa | grep gmp gmp-devel-4.1.4-10.el5 gmp-4.1.4-10.el5 [root@server2 ~]# rpm -e gmp-devel-4.1.4-10.el5.i386 [root@server2 ~]# rpm -e gcc-gfortran-4.1.2-52.el5.i386 [root@server2 ~]# rpm -e gmp-4.1.4-10.el5 [root@server2 ~]# ldconfig [root@server2 ~]#
然后安装高版本的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模块:
[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上安装完成
5.如果觉着手动安装比较麻烦的话,请使用以下一键安装脚本(请下载附件)
请使用 < source 脚本名 > 的方式执行脚本
#!/bin/bash #-------------------------------------------------- # Name: one_key_install_paramiko # Purpose: 一键安装paramiko需要的环境 # Version: 1.0 # Author: LEO # BLOG: http://linux5588.blog.51cto.com # EMAIL: chanyipiaomiao@163.com # Created: 2013-09-06 # Copyright: (c) LEO 2013 #--------------------------------------------------- export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin # 检测GCC是否安装 checkGCC(){ if ! gcc -v &>/dev/null; then echo echo "GCC not Found! Please use < yum -y install gcc* > " echo exit 1 fi } # 安装新版本gmp包 installNewGMP(){ wget http://ftp.gnu.org/gnu/gmp/gmp-5.1.2.tar.bz2 tar jxf gmp-5.1.2.tar.bz2 cd gmp-5.1.2 ./configure && make && make install echo "/usr/local/lib" >> /etc/ld.so.conf ldconfig } # 安装easy_install工具 installEasy_install(){ if easy_install -h &>/dev/null ;then echo echo "easy_install already installed" echo else wget http://peak.telecommunity.com/dist/ez_setup.py python ez_setup.py fi } # 安装paramiko模块 installParamiko(){ easy_install paramiko } # 安装Python2.7 installPython27(){ wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 tar jxf Python-2.7.5.tar.bz2 cd Python-2.7.5 ./configure --prefix=/usr/local/python2.7 make && make install echo "export PATH=/usr/local/python2.7/bin:$PATH" >> /etc/profile source /etc/profile } # 检查Python版本是否符合要求 TEMP="tempfile" checkPython(){ python -V 2>${TEMP} VERSION=$(awk -F. '{print $2}' ${TEMP}) rm -f ${TEMP} if [[ $VERSION -lt 7 ]]; then echo echo "Python Version does not meet the requirements,We Will auto download and install." echo installPython27 installEasy_install installParamiko else installEasy_install installParamiko fi } # 调用函数检查GCC checkGCC # 调用函数安装新版本gmp installNewGMP # 调用函数检查Python checkPython
三、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测试一下。
C:\Users\TestAA>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
看到以上结果,代表python已经安装成功。
2.安装easy_install工具
C:\Users\TestAA\Desktop>python ez_setup.py Downloading http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-p y2.7.egg Processing setuptools-0.6c11-py2.7.egg Copying setuptools-0.6c11-py2.7.egg to c:\python27\lib\site-packages Adding setuptools 0.6c11 to easy-install.pth file Installing easy_install-script.py script to C:\Python27\Scripts Installing easy_install.exe script to C:\Python27\Scripts Installing easy_install.exe.manifest script to C:\Python27\Scripts Installing easy_install-2.7-script.py script to C:\Python27\Scripts Installing easy_install-2.7.exe script to C:\Python27\Scripts Installing easy_install-2.7.exe.manifest script to C:\Python27\Scripts Installed c:\python27\lib\site-packages\setuptools-0.6c11-py2.7.egg Processing dependencies for setuptools==0.6c11 Finished processing dependencies for setuptools==0.6c11
再把 C:\Python27\Scripts 该路径添加到 系统的Path 变量里面,这样就可以直接使用easy_install命令了。
3.安装PyCrypto模块
由于在Windows上平台上不管使用手工编译,还是用easy_install命令对该模块进行安装的话,非常
容易出现问题,而且特别的麻烦,所以已经有人帮我们编译好了Windows平台上对应的版本,只需要下载安装即可
到这个位置下载对应的版本 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模块
C:\Users\TestAA>easy_install paramiko Searching for paramiko Reading http://pypi.python.org/simple/paramiko/ Best match: paramiko 1.11.0 Downloading https://pypi.python.org/packages/source/p/paramiko/paramiko-1.11.0.t ar.gz#md5=a2c55dc04904bd08d984533703177084 Processing paramiko-1.11.0.tar.gz Running paramiko-1.11.0\setup.py -q bdist_egg --dist-dir c:\users\testaa\appdata \local\temp\easy_install-rla6aa\paramiko-1.11.0\egg-dist-tmp-aynqgv zip_safe flag not set; analyzing archive contents... Adding paramiko 1.11.0 to easy-install.pth file Installed c:\python27\lib\site-packages\paramiko-1.11.0-py2.7.egg Processing dependencies for paramiko Finished processing dependencies for paramiko C:\Users\TestAA>
进入Python导入paramiko一下看看,
C:\Users\TestAA>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> import paramiko >>> dir(paramiko) ['AUTH_FAILED', 'AUTH_PARTIALLY_SUCCESSFUL', 'AUTH_SUCCESSFUL', 'Agent', 'AgentK ey', 'AuthHandler', 'AuthenticationException', 'AutoAddPolicy', 'BadAuthenticati onType', 'BadHostKeyException', 'BaseSFTP', 'BufferedFile', 'Channel', 'ChannelE xception', 'ChannelFile', 'DSSKey', 'HostKeys', 'InteractiveQuery', 'Message', ' MissingHostKeyPolicy', 'OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED', 'OPEN_FAILED_C ONNECT_FAILED', 'OPEN_FAILED_RESOURCE_SHORTAGE', 'OPEN_FAILED_UNKNOWN_CHANNEL_TY PE', 'OPEN_SUCCEEDED', 'PKey', 'Packetizer', 'PasswordRequiredException', 'Proxy Command', 'ProxyCommandFailure', 'RSAKey', 'RejectPolicy', 'SFTP', 'SFTPAttribut es', 'SFTPClient', 'SFTPError', 'SFTPFile', 'SFTPHandle', 'SFTPServer', 'SFTPSer verInterface', 'SFTP_BAD_MESSAGE', 'SFTP_CONNECTION_LOST', 'SFTP_EOF', 'SFTP_FAI LURE', 'SFTP_NO_CONNECTION', 'SFTP_NO_SUCH_FILE', 'SFTP_OK', 'SFTP_OP_UNSUPPORTE D', 'SFTP_PERMISSION_DENIED', 'SSHClient', 'SSHConfig', 'SSHException', 'Securit yOptions', 'ServerInterface', 'SubsystemHandler', 'Transport', 'WarningPolicy', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__license__', ' __loader__', '__name__', '__package__', '__path__', '__version__', 'agent', 'aut h_handler', 'ber', 'buffered_pipe', 'channel', 'client', 'common', 'compress', ' config', 'dsskey', 'file', 'hostkeys', 'io_sleep', 'kex_gex', 'kex_group1', 'mes sage', 'packet', 'pipe', 'pkey', 'primes', 'proxy', 'resource', 'rsakey', 'serve r', 'sftp', 'sftp_attr', 'sftp_client', 'sftp_file', 'sftp_handle', 'sftp_server ', 'sftp_si', 'ssh_exception', 'sys', 'transport', 'util'] >>>
看到以上情况,说明paramiko已经成功安装了
转载于:https://blog.51cto.com/linux5588/1275180