1、先说遇到的问题
pip install python-nmap
安装python-nmap 之后,遇到如下问题:
>>>import namp >>> nm = nmap.PortScanner() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Python/2.7/site-packages/nmap/nmap.py", line 131, in __init__ os.getenv(‘PATH‘) nmap.nmap.PortScannerError: ‘nmap program was not found in path. PATH is : /Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/lib/python2.7/site-packages/python_nmap-0.6.1-py2.7.egg:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin‘
查看PortScanner类源码发现,__init__(self, nmap_search_path=(‘nmap‘, ‘/usr/bin/nmap‘, ‘/usr/local/bin/nmap‘, ‘/sw/bin/nmap‘, ‘/opt/local/bin/nmap‘)) 初始化设置了默认查找路径, 还有段代码是出现以上错误的源程序,如下:
#‘Nmap version 5.0 ( http://nmap.org )‘ # This is for Mac OSX. When idle3 is launched from the finder, PATH is not set so nmap was not found for nmap_path in nmap_search_path: try: if sys.platform.startswith(‘freebsd‘) or sys.platform.startswith(‘linux‘) or sys.platform.startswith(‘darwin‘): p = subprocess.Popen([nmap_path, ‘-V‘], bufsize=10000, stdout=subprocess.PIPE, close_fds=True) else: p = subprocess.Popen([nmap_path, ‘-V‘], bufsize=10000, stdout=subprocess.PIPE) except OSError: pass else: self._nmap_path = nmap_path # save path break else: raise PortScannerError( ‘nmap program was not found in path. PATH is : {0}‘.format( os.getenv(‘PATH‘) ) )
这段代码简要解释下功能: 就是使用初始路径在命令行中执行: 路径 -V , 上面的代码就是执行:nmap -V
在命令行中执行: namp -V , 如果出现 nmap: command not found, 就是你没有安装nmap , 查了些资料说python-nmap 依赖nmap , 需要先安装
nmap 才行。
PS: 路径上的nmap 不是文件夹,也不是namp.py文件,而是二进制可执行文件, 如果相应路径找不到nmap可执行文件,说明namp没安装或路径不对
2、 brew install nmap
这个我试很多次才成功, 老是出现找不到资源的错误,
curl: (28) Operation timed out after 0 milliseconds with 0 out of 0 bytes received
Error: Failed to download resource "nmap"
可能是因为网络不行。
到此nmap安装已经安装好了。
但是执行: nmap 命令还是出现:
-bash: nmap: command not found
需要按照命令提示执行一下命令:
brew link --overwrite nmap
再执行namp -V , 成功!
安装nmap成功后命令行中有个警告:
==> Caveats Python modules have been installed and Homebrew‘s site-packages is not in your Python sys.path, so you will not be able to import the modules this formula installed. If you plan to develop with these modules, please run: mkdir -p /Users/jason/Library/Python/2.7/lib/python/site-packages echo ‘import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")‘ >> /Users/jason/Library/Python/2.7/lib/python/site-packages/homebrew.pth
这个python modeles就是指python-nmap , 按照命令执行下:(我的python-nmap 是先于nmap安装好的,所以有这个提示。)
mkdir -p /Users/jason/Library/Python/2.7/lib/python/site-packages echo ‘import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")‘ >> /Users/jason/Library/Python/2.7/lib/python/site-packages/homebrew.pth 至此,nmap.PortScanner() 问题就解决了。 PS: http://nmap.org/ , 可以直接下载nmap 安装包