Scapy installation fails on osx with dnet import error

EDIT (30.10.19)

Install Scapy

Scapy's official documentation lists the possible bundles:

  • Default, only Scapy:
    • $ pip install scapy
  • Recommended, Scapy and iPython:
    • $ pip install --pre scapy[basic]
  • Complete, Scapy & all its main dependencies:
    • $ pip install --pre scapy[complete]

Install optional dependencies

Scapy requires certain dependencies for some special features, such as for plotting, 2D & 3D graphics, WEP decryption, PKI operations and TLS decryption, fingerprinting and VOIP. Most of these software are installable via pip. Scapy's official documentation presents them along with some examples that test whether the installation was successful.

Configure libpcap integration

Scapy's official documentation states that it works natively since the recent versions but it's possible to configure it to use libpcap, which may be installed using either Homebrew or MacPorts. Both installation methods work fine, yet Homebrew is used to run unit tests with Travis CI. Note that Libpcap might already be installed, for example if tcpdump is installed, such as in the case of OSX.

Install using Homebrew

$ brew update  # update Homebrew
$ brew install libpcap  # install libpcap

Enable it in Scapy via from scapy.config import conf; conf.use_pcap = True.

Install using MacPorts

$ sudo port -d selfupdate  # update MacPorts
$ sudo port install libpcap  # install libpcap

Enable it in Scapy via from scapy.config import conf; conf.use_pcap = True.


EDIT (27.05.15)

This answer states that all mentioned issues were fixed, and provides a much simpler installation method. However, its comments suggest that although it seems to work on OS X 10.10 Yosemite and OS X 10.11 El Capitan, it might fail for certain other versions.

$ brew install libdnet --with-python
$ pip install pcapy
$ pip install scapy

If Homebrew's site-packages is not in Python's sys.path variable, the following should be executed (see this for more information), with the actual username replacing the placeholder <USERNAME>:

$ mkdir -p /Users/<USERNAME>/Library/Python/2.7/lib/python/site-packages
$ echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/<USERNAME>/Library/Python/2.7/lib/python/site-packages/homebrew.pth

The original answer

You have not completed the installation of libdnet and its Python wrapper, as stated in Scapy's installation guide:

$ wget https://github.com/dugsong/libdnet/archive/libdnet-1.12.tar.gz
$ tar xfz libdnet-1.12.tgz
$ ./configure
$ make
$ sudo make install
$ cd python
$ python2.5 setup.py install

If your system is 64 bit, use these compilation commands instead:

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

Moreover, please verify that you've installed the correct version, i.e. 1.12 rather than 1.11.

If that fails as well, try installing via MacPorts and use its dnet.so file, as described 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

That link also recommends changing some code in /Library/Python/2.7/site-packages/scapy/arch/unix.py (fix OSError: Device not configured).

Change line 34 from:

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

to:

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

as follows:

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

If you still get the error OSError: Device not configured, then try performing similar changes to the other branches of the if clause (specifically, to its else branch), as described in this answer.


(This is a comment to Tim Wu's Answer, but I'm missing the reputation to do so)

Keep in mind to do what brew is actually telling you:

==> 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

So just re-run the brew command and execute the two lines it's telling you there to use all installed brew python packages.


All issues mentioned above seem all fixed. I'm running OS X Yosemite. I got a working scapy by simply the following three commands.

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

I'm running OSX 10.5.9 - spent forever trying to get scapy working - after installing dnet/pcap libraries I got the "OSError:Device not configured" too. Tried replacing line 34 in unix.py with

"netstat -rn | grep -v vboxnet"

Still got the same error. But when I change line 37 instead in the "else" part of the if block:

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

Works like a charm!