Search code examples
pythonscapy

Can't send packets (Errno 9: Bad file descriptor)


Hi I'm a beginner with Python and Scapy. When I try to use sendp() on a basic Layer 2 packet, I get a traceback error.

Using Python 3.8 and the latest development version of Scapy from https://scapy.readthedocs.io/en/latest/installation.html#platform-specific-instructions

This is what I'm inputting into the Python shell:

import scapy
from scapy.all import *
a=Ether()/IP(dst="www.google.ca")/ICMP()/"Hello world"
sendp(a)

This is the error message:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    sendp(a)
  File "C:\Users\hoang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\scapy-git_archive.dev304758016-py3.8.egg\scapy\sendrecv.py", line 336, in sendp
    results = __gen_send(socket, x, inter=inter, loop=loop,
  File "C:\Users\hoang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\scapy-git_archive.dev304758016-py3.8.egg\scapy\sendrecv.py", line 296, in __gen_send
    os.write(1, b".")
OSError: [Errno 9] Bad file descriptor

Solution

  • As pointed by @furas, this is indeed an issue with your console (IDLE?). Scapy tries to display that the packet was sent, which fails.

    You can always use

    sendp(p, verbose=False)
    

    To disable the logs, therefore working around the issue.

    However I must say that if os.write(1, ..) was the only option back in the days, it's a bit outdated nowadays. This could probably be fixed on upstream.