2014-10-07 101 views
18

在安装Scapy时遇到问题,它需要依赖项。我花了一些时间搜索解决方案,但所有'解决方案'似乎都会影响旧版本的Python,或者根本不起作用。scapy安装在dx导入错误的osx上失败

脚本:

#!/usr/bin/python 

import threading 
import Queue 
import time 
from scapy.all import * 

class WorkerThread(threading.Thread) : 

    def __init__(self, queue, tid) : 
     threading.Thread.__init__(self) 
     self.queue = queue 
     self.tid = tid 
     print 'Worker: %d' %self.tid 

    def run(self) : 
     total_ports = 0 
     while True : 
      port = 0 
      try : 
       port = self.queue.get(timeout=1) 
      except Queue.Empty : 
       print 'Worker %d exiting. %d ports scanned' %(self.tid, total_ports) 

       return 

       #Scanning begins 

      ip = sys.argv[1] 
      response = sr1(IP(dst=ip)/TCP(dport=port, flags='S'), verbose=False, timeout=.2) 


      if response : 
       if response[TCP].flags == 18 : 

        print 'ThreadID: %d: Got port no. %d status: OPEN' %(self.tid, port) 
       self.queue.task_done() 
       total_ports += 1 

queue = Queue.Queue() 

threads = [] 
for i in range(1, 10) : 
    print 'Creating WorkerThread : %d' %i 
    worker = WorkerThread(queue, i) 
    worker.setDaemon(True) 
    worker.start() 
    threads.append(worker) 
    print 'WorkerThread %d created' %i 

for j in range(1, 100) : 
    queue.put(j) 

queue.join() 

for item in threads : 
    item.join() 

print 'Scanning complete' 

Python版本是2.7.5和路径到Python验证。

which python 
/usr/bin/python 

当执行脚本我收到以下错误:

./multi-threaded-scanner.py 
Traceback (most recent call last): 
    File "./multi-threaded-scanner.py", line 6, in <module> 
    from scapy.all import * 
    File "/Library/Python/2.7/site-packages/scapy/all.py", line 16, in <module> 
    from arch import * 
    File "/Library/Python/2.7/site-packages/scapy/arch/__init__.py", line 75, in <module> 
    from bsd import * 
    File "/Library/Python/2.7/site-packages/scapy/arch/bsd.py", line 12, in <module> 
    from unix import * 
    File "/Library/Python/2.7/site-packages/scapy/arch/unix.py", line 20, in <module> 
    from pcapdnet import * 
    File "/Library/Python/2.7/site-packages/scapy/arch/pcapdnet.py", line 160, in <module> 
    import dnet 
ImportError: No module named dnet 

我可以同时使用Scapy的和Python交互式解释和Python解释器运行import scapy产生任何错误。 当脚本最初运行时pcapy模块丢失,但是我安装了 ,然后问题切换到dnet,我找不到解决方案。 A similar post,似乎描述了相同的问题,但解决方法无效。 任何人都可以解释这个问题吗?

命令用来安装pcapy和libdnet:

libdnet-1.11.tar.gz(2005年1月19日)

` ~/Downloads/libdnet-1.11  
chmod a+x configure 
~/Downloads/libdnet-1.11  
./configure && make` 

退出成功

Pcapy:最新稳定版(0.10 0.8),更新2010年8月26日

~/Downloads/pcapy-0.10.8 
sudo python setup.py install Password: running install running build running build_ext running build_scripts running install_lib running install_scripts changing mode of /usr/local/bin/96pings.pcap to 777 changing mode of /usr/local/bin/pcapytests.py to 777 running install_data running install_egg_info Removing /Library/Python/2.7/site-packages/pcapy-0.10.8-py2.7.egg-info Writing /Library/Python/2.7/site-packages/pcapy-0.10.8-py2.7.egg-info ~/Downloads/pcapy-0.10.8 

个结果,并与新的标志

~/Downloads/libdnet-1.12  
sudo CFLAGS='-arch i386 -arch x86_64' ./configure --prefix=/usr and archargs='-arch i386 -arch x86_64' make 
configure: WARNING: you should use --build, --host, --target 
configure: WARNING: you should use --build, --host, --target 
checking for a BSD-compatible install... /usr/bin/install -c 
checking whether build environment is sane... yes 
/Users/richardcurteis/Downloads/libdnet-1.12/config/missing: Unknown `--is-lightweight' option 
Try `/Users/richardcurteis/Downloads/libdnet-1.12/config/missing --help' for more information 
configure: WARNING: 'missing' script is too old or missing 
checking for a thread-safe mkdir -p... config/install-sh -c -d 
checking for gawk... no 
checking for mawk... no 
checking for nawk... no 
checking for awk... awk 
checking whether make sets $(MAKE)... yes 
checking whether make supports nested variables... yes 
checking whether to enable maintainer-specific portions of Makefiles... no 
checking build system type... Invalid configuration `and': machine `and' not recognized 
configure: error: /bin/sh config/config.sub and failed 
~/Downloads/libdnet-1.12  
+1

你如何安装'scapy'和'pcapy'?你使用过哪些命令?是否有任何错误报告?你有没有安装['libdnet'](http://libdnet.sourceforge.net)? – Yoel 2014-10-07 17:06:19

+0

添加到原始帖子。所有退出成功,脚本在刷新终端中重新运行,但出现相同错误 – 2014-10-07 23:53:06

+0

您有什么操作系统? – Yoel 2014-10-07 23:59:22

回答

14

编辑编译 - This answer below指出,所有提到的问题是固定的,并提供了一个更简单的安装方法。然而,其评论表明,虽然它似乎在OS X 10.10优胜美地OS X 10.11埃尔卡皮坦,它可能会失败的某些其他版本。


您尚未完成的libdnet安装和Python包装,如Scapy's installation guide说:

$ wget http://libdnet.googlecode.com/files/libdnet-1.12.tgz 
$ tar xfz libdnet-1.12.tgz 
$ ./configure 
$ make 
$ sudo make install 
$ cd python 
$ python2.5 setup.py install 

如果你的系统是64位的,使用这些编辑命令来代替:

$ CFLAGS='-arch i386 -arch x86_64' ./configure 
$ archargs='-arch i386 -arch x86_64' make 

此外,请确认您已安装正确版本,即1.12而不是1.11。

如果失败,那么尝试通过macports安装和使用它的dnet.so文件,描述here

$ port selfupdate 
$ port upgrade outdated 
$ port install py27-libdnet 
$ port install libdnet 
$ cp /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dnet.so /Library/Python/2.7/site-packages 

该链接还建议/Library/Python/2.7/site-packages/scapy/arch/unix.py改变一些代码(修正OSError: Device not configured)。

更改行34:

f=os.popen("netstat -rn") # -f inet 

到:

f=os.popen("netstat -rn | grep -v vboxnet") # -f inet 

如下:

def read_routes(): 
    if scapy.arch.SOLARIS: 
#  f=os.popen("netstat -rvn") # -f inet 
     f=os.popen("netstat -rn | grep -v vboxnet") # -f inet 

如果仍然出现错误OSError: Device not configured,然后尝试进行到类似的变化if条款的其他分支(具体来说,就是其else分支),如this answer中所述。

+0

是的,我也跑了,再次,退出罚款,但同样的错误导入dnet – 2014-10-08 00:54:16

+0

这似乎是错误的一部分。 '从pcapdnet进口* 文件 “/Library/Python/2.7/site-packages/scapy/arch/pcapdnet.py”,线路160,在 进口DNET 导入错误:没有模块名为dnet' – 2014-10-08 01:07:32

+0

获得一个配置错误,我已将输出添加到原始帖子。 – 2014-10-08 01:31:20

2

我正在运行OSX 10.5.9 - 花费永远试图让scapy工作 - 在安装dnet/pcap库之后我也得到了“OSError:Device not configured”。试图与

"netstat -rn | grep -v vboxnet"

仍然得到了同样的错误替换unix.py线34。但是,当我改变行37,而不是在if的其他部分:

def read_routes(): 
    if scapy.arch.SOLARIS: 
     f=os.popen("netstat -rvn") # -f inet 
    elif scapy.arch.FREEBSD: 
     f=os.popen("netstat -rnW") # -W to handle long interface names 
    else: 
     # f=os.popen("netstat -rn") # -f inet 
     f=os.popen("netstat -rn | grep -v vboxnet") # -f inet 

工程就像一个魅力!

16

上述所有问题似乎都是固定的。我正在运行OS X Yosemite。我通过以下三个命令得到了一个有效的scapy。

brew install --with-python libdnet 
pip install pcapy 
pip install scapy 
+2

但是缺少dnet“ImportError:没有名为dnet的模块” – PiyusG 2015-04-25 02:19:09

+0

在这里相同。你是如何摆脱缺少的dnet错误的? – 2015-08-27 21:30:42

+1

尽管如此,仍然是dnet错误。这个答案应该被拒绝。 – jeffehobbs 2016-05-31 22:29:10

2

(这是Tim Wu的答案评论,但我错过了声誉这样做)

记住做什么酿造实际上是在告诉你:

==> 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/YourUsernameHere/Library/Python/2.7/lib/python/site-packages 
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/YourUsernameHere/Library/Python/2.7/lib/python/site-packages/homebrew.pth 

所以,只需重新运行brew命令并执行它告诉你在那里使用所有已安装的brew python软件包的两行。

+0

注意:如果您复制粘贴上述脚本,请记得编辑“YourUsernameHere”! – 2016-10-31 08:48:45

0

在OSX埃尔卡皮坦,什么工作对我来说是迫使PIP使用sudo重新安装pcapy: sudo pip install --user pcapy -I

相关问题