最近想做一个python的linux文件比较工具
要用到ssh功能,希望直接使用python的ssh client而不是使用 expect之类的工具
安装ssh扩展花费了不少时间,目前实验成功了win32的版本。。把要点记录下来,供大家分享。
关于python的ssh有如下几种 :
参考:http://wiki.python.org/moin/SecureShell
目前比较活跃的是就是 http://pypi.python.org/pypi/ssh - in pypi as ssh
如果直接下载ssh模块编译,安装完import ssh的时候会提示找不到Crpyto
需要安装pycrypto 2.6,而使用python setup.py install 安装会提示没有 gmp 库,编译这个就麻烦了。。。
只能通过python自己的disttools来帮忙编译,然后使用mingw32 自己生成安装包,就不用在所有机器上都编译一次了,直接执行安装包就好了。
编译参考如下
http://www.razorvine.net/blog/user/irmen/article/2009-12-05/166
执行
python setup.py build -c mingw32
python setup.py bdist_wininst
在dist目录下会生成 exe,安装后就可以了
win 7 64bit 生成如下:
http://yorickdowne.wordpress.com/2010/12/22/compiling-pycrypto-on-win7-64/
Preparing to compile:
- Install VC++ 2008 SP1 Express. Needs to be 2008, not 2010 – that goes back to an old tradition that modules need to be compiled with the version that Python was compiled with, which happens to be 2008 right now. The Express version of VC++ 2008 is still available: http://www.microsoft.com/express/Downloads/#2008-Visual-CPP. If you are nervous about whether it will remain available, download the ISO.
- You now also need the Windows SDK so you have the amd64 compiler, which isn’t included in the Express version. This needs to be the Win7 + Net 3.5 SDK, not the Win7 + Net 4.0 SDK. You can find it here: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c17ba869-9671-4330-a63e-1fd44e0e2505&displaylang=en , and again download the ISO (the one for amd64 support though!) if nervous it may disappear. Install and make sure you install the “Visual C++ Compilers”.(记得直接下64位的iso,要不还是有问题)
Python 2.7.1 has a bug where it will get confused withthe paths that MS use for Express, whichhappen to be different than the paths used in Pro, which breaks vcvarsall.bat when attempting to build 64-bit binaries. This is a limitation of VC++ 2008 Express. The Python distutils team offers a work-around. Check the bug athttp://bugs.python.org/issue7511 to see whether the issue has been resolved already in your version of Python. If not so, grab the diff attached to that bug report and apply it to your Python main directory using “patch”. The command line for this is “patch -p0 <vcvars4.diff”.
If you don’t have a copy of “patch” already, the Cygwin default install + patch (not patchutils) will give you patch.
Now you’re ready to compile. This is pretty simple from here on out:
- pycrypto should be unpacked somewhere other than your Python dir, say c:\temp
- Then, from within the pycrypto directory, run these commands:
- python setup.py build -c msvc (编译的时候会遇到 unistd.h 等两个 头文件不在,直接到python目录下注释掉就好了。。)
- python setup.py install
- python setup.py test
最后成功生成 pycrypto amd64的安装包。。其他的就跟这个一样了
win 7 如果没有安好 vc的话,会有如下错误,乱七八糟的。。。
E:\dev\python\soft\pycrypto-2.6>C:\MinGW\bin\gcc.exe -shared -s build\temp.win-amd64-2.7\Release\src\winrand.o build\temp.win-amd6
4-2.7\Release\src\winrandom.def -Lc:\python27\libs -Lc:\python27\PCbuild\amd64 -lpython27 -o build\lib.win-amd64-2.7\Crypto\Random
\OSRNG\winrandom.pyd
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x4c): undefined reference to `_imp__PyArg_ParseTupleAndKeywords'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x90): undefined reference to `_imp___PyObject_New'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0xce): undefined reference to `_imp__PyExc_SystemError'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0xd9): undefined reference to `_imp__PyErr_Format'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x139): undefined reference to `_imp__PyArg_ParseTuple'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x163): undefined reference to `_imp__PyMem_Malloc'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x1ac): undefined reference to `_imp__PyString_FromStringAndSize'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x1b9): undefined reference to `_imp__PyMem_Free'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x1d9): undefined reference to `_imp__PyExc_TypeError'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x1e4): undefined reference to `_imp__PyErr_Format'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x206): undefined reference to `_imp__PyExc_SystemError'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x211): undefined reference to `_imp__PyErr_Format'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x21a): undefined reference to `_imp__PyMem_Free'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x22a): undefined reference to `_imp__PyErr_NoMemory'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x239): undefined reference to `_imp__PyExc_ValueError'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x244): undefined reference to `_imp__PyErr_SetString'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x288): undefined reference to `_imp__Py_FindMethod'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x29c): undefined reference to `_imp__PyInt_FromLong'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x2b1): undefined reference to `_imp__PyExc_TypeError'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x2bc): undefined reference to `_imp__PyErr_Format'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x2e6): undefined reference to `_imp__PyExc_TypeError'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x2f1): undefined reference to `_imp__PyErr_Format'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x322): undefined reference to `_imp__PyObject_Free'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x33e): undefined reference to `_imp__PyExc_SystemError'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x349): undefined reference to `_imp__PyErr_Format'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x356): undefined reference to `_imp__PyType_Type'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x388): undefined reference to `_imp__Py_InitModule4'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x3a3): undefined reference to `_imp__PyModule_AddIntConstant'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x4cf): undefined reference to `_imp__PyModule_AddStringConstant'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x555): undefined reference to `_imp__PyErr_Occurred'
build\temp.win-amd64-2.7\Release\src\winrand.o:winrand.c:(.text+0x566): undefined reference to `_imp__Py_FatalError'
collect2: ld returned 1 exit status
E:\dev\python\soft\pycrypto-2.6>python setup.py bdist_wininst
running bdist_wininst
running build
running build_py
running build_ext
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Random.OSRNG.winrandom' extension
Traceback (most recent call last):
File "setup.py", line 456, in <module>
core.setup(**kw)
File "c:\python27\lib\distutils\core.py", line 152, in setup
dist.run_commands()
File "c:\python27\lib\distutils\dist.py", line 953, in run_commands
self.run_command(cmd)
File "c:\python27\lib\distutils\dist.py", line 972, in run_command
cmd_obj.run()
File "c:\python27\lib\distutils\command\bdist_wininst.py", line 131, in run
self.run_command('build')
File "c:\python27\lib\distutils\cmd.py", line 326, in run_command
self.distribution.run_command(command)
File "c:\python27\lib\distutils\dist.py", line 972, in run_command
cmd_obj.run()
File "c:\python27\lib\distutils\command\build.py", line 127, in run
self.run_command(cmd_name)
File "c:\python27\lib\distutils\cmd.py", line 326, in run_command
self.distribution.run_command(command)
File "c:\python27\lib\distutils\dist.py", line 972, in run_command
cmd_obj.run()
File "setup.py", line 253, in run
build_ext.run(self)
File "c:\python27\lib\distutils\command\build_ext.py", line 339, in run
self.build_extensions()
File "setup.py", line 150, in build_extensions
build_ext.build_extensions(self)
File "c:\python27\lib\distutils\command\build_ext.py", line 448, in build_extensions
self.build_extension(ext)
File "c:\python27\lib\distutils\command\build_ext.py", line 498, in build_extension
depends=ext.depends)
File "c:\python27\lib\distutils\msvc9compiler.py", line 473, in compile
self.initialize()
File "c:\python27\lib\distutils\msvc9compiler.py", line 383, in initialize
vc_env = query_vcvarsall(VERSION, plat_spec)
File "c:\python27\lib\distutils\msvc9compiler.py", line 299, in query_vcvarsall
raise ValueError(str(list(result.keys())))
ValueError: [u'path']
http://www.velocityreviews.com/forums/t867906-pycrypto-builds-neither-with-msvc-nor-mingw.html