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
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.
Related
The question
I created a package for maintaining HTTP requests - just for fun.
It runs perfectly in normal Python, however, when I try to run it in PyPy, the following error occurs while requesting over HTTPS:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/adam/python/httpy/httpy/__init__.py", line 2019, in request
http_version=http_version
File "/home/adam/python/httpy/httpy/__init__.py", line 1863, in _raw_request
proto.send_request(sock,method,defhdr,data,path,debug)
File "/home/adam/python/httpy/httpy/__init__.py", line 1339, in send_request
return self.sender(*args).send(sock)
File "/home/adam/python/httpy/httpy/__init__.py", line 1677, in send
sock.send(self.body)
File "/opt/pypy3/lib/pypy3.8/ssl.py", line 1173, in send
return self._sslobj.write(data)
File "/opt/pypy3/lib/pypy3.8/_cffi_ssl/_stdssl/__init__.py", line 546, in write
return self._write_with_length(_str_to_ffi_buffer(bytestring), len(bytestring))
File "/opt/pypy3/lib/pypy3.8/_cffi_ssl/_stdssl/__init__.py", line 603, in _write_with_length
raise pyssl_error(self, length)
_cffi_ssl._stdssl.error.SSLEOFError: EOF occurred in violation of protocol
Requests, for example, run well even in PyPy.
Could the issue be something with using ssl.wrap_socket() or it's an error on a deeper level?
System info
Python version: 3.10.5
PyPy version: 3.8.13
OS: x86_64 Linux 5.18.10-arch1-1
OK, I found it out. It was because of sending b'' after request if the request body was empty. I thought that sending empty string would be an empty operation, but obviously it is not - it is treated as an EOF. I am still not sure why this fails only using PyPy. It's maybe because it's older version than Python where the code was tested.
Anyway, if you get this error, make sure you are not accidentially sending b''.
scapy OSError: [Errno 9] Bad file descriptor
same error as this guy . using python 2.7.5 on windows.downloded all the extentions and followed everything step by step. still doesnt seem to work. every function i try wheter it's sr1 or send or anything else i get this error OSError: [Errno 9] Bad file descriptor..
and it mentiones lines in some of the scapy scripts (scapy\sendrecv.pyc as example..)
the simplest things i try running in the interpreter doesnt work. as in :
from scapy.all import *
p=sr1(IP(dst='190.200.2.5')/ICMP())
so i tried reinstalling this time everything according to the guide they provide int the website and using python 2.6 , same results ..
>>> p=sr1(IP(dst='192.168.1.1')/ICMP())
ERROR: --- Error sending packets
Traceback (most recent call last):
File "C:\Python26\lib\site-packages\scapy\arch\windows\__init__.py", line 374, in sndrcv
pks.send(p)
File "C:\Python26\lib\site-packages\scapy\arch\pcapdnet.py", line 237, in send
ifs = dnet.eth(iff)
File "dnet.pyx", line 112, in dnet.eth.__init__
OSError: Result too large
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
p=sr1(IP(dst='192.168.1.1')/ICMP())
File "C:\Python26\lib\site-packages\scapy\sendrecv.py", line 335, in sr1
a,b=sndrcv(s,x,*args,**kargs)
File "C:\Python26\lib\site-packages\scapy\arch\windows\__init__.py", line 431, in sndrcv
os.write(1, ".")
OSError: [Errno 9] Bad file descriptor
I've been learning to use python in astronomy and for that I'm following this notes. In the very beginning the author does the following example:
>>> im = pyfits.getdata('http://das.sdss.org/www/cgi-bin/drC?RUN=3630&RERUN=40&CAMCOL=3&FIELD=83&FILTER=r')
>>> numdisplay.display(im,z1=1000,z2=1500)
I try to replicate it and I get:
>>> numdisplay.display(im,z1=1000,z2=1500)
Image displayed with Z1: 1000 Z2: 1500
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
numdisplay.display(im,z1=1000,z2=1500)
File "C:\Mine\Python\lib\site-packages\numdisplay\__init__.py", line 446, in display
_d.writeImage(bpix,_wcsinfo)
File "C:\Mine\Python\lib\site-packages\numdisplay\displaydev.py", line 513, in writeImage
self.writeData(_lx,_ydisp,_fpix[block,:])
File "C:\Mine\Python\lib\site-packages\numdisplay\displaydev.py", line 379, in writeData
self._writeHeader(opcode,self._MEMORY, -nbytes, x, y, frame, 0)
File "C:\Mine\Python\lib\site-packages\numdisplay\displaydev.py", line 542, in _writeHeader
self._write(a.tostring())
File "C:\Mine\Python\lib\site-packages\numdisplay\displaydev.py", line 580, in _write
nwritten = self._socket.send(s[-n:])
error: [Errno 10054] An existing connection was forced to close by the remote host
I don't understand what I'm doing wrong. I mean if I write numdisplay.open() everything is fine... I'm thinking that it might be my antivirus or something that doesn't let python to communicate with ds9... Can somebody help me?
Edit: Well it doesn't seem to be the antivirus. I stopped it and run the script and I got the same error.
I was getting similar error messages, and I just tried adding a non-'None' argument to the bufname argument and it works (my image is about 4096.4096):
numdisplay.display(data,bufname='imt4096')
I'm trying a simple code, just sending an ARP packet:
from scapy.all import *
p = ARP()
send(p)
and I get this error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
send(p)
File "C:\Python26\scapy\sendrecv.py", line 251, in send
__gen_send(conf.L3socket(*args, **kargs), x, inter=inter, loop=loop,
count=count,verbose=verbose, realtime=realtime)
File "C:\Python26\scapy\sendrecv.py", line 234, in __gen_send
s.send(p)
File "C:\Python26\scapy\arch\pcapdnet.py", line 237, in send
ifs = dnet.eth(iff)
File "dnet.pyx", line 112, in dnet.eth.__init__
OSError: No error
My OS is windows 8 64bit. I tried in win7 64bit and got the same problem. I tried in win7 32bit and that works fine.
I can't understand why.. please help..
I guess you have to inform the interface ID, like this:
sendp(netpacket, iface='eth1')
I had the same trouble as yours, did like I said and got it!
Several times I've seen mention that for asynchronous ping is good to use twisted.
I written implementation on threads (like Ping a site in Python?)
But on ~200 threads i have crush
On page http://twistedmatrix.com/trac/wiki/ProjectsUsingTwisted i find
txNetTools - Ping, traceroute, icmp, etc., implemented in Twisted, and a library for those who want to build their own network tools.
Has anyone used this library?
I tried to use it ping.py:
Traceback (most recent call last):
File "ping.py", line 23, in <module>
class Pinger(ICMP):
NameError: name 'ICMP' is not defined
if change:
class Pinger(**ICMP**):
to
class Pinger():
Traceback (most recent call last):
File "ping.py", line 54, in <module>
reactor.listenICMP(0, Pinger())
File "./txnet/reactor.py", line 21, in listenICMP
p.startListening()
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/udp.py", line 102, in startListening
self._connectToProtocol()
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/udp.py", line 123, in _connectToProtocol
self.protocol.makeConnection(self)
AttributeError: Pinger instance has no attribute 'makeConnection'
Maybe I'm doing something wrong or not understand?
P.S. twisted 12.0
If you are running sandbox/ping.py, then the exception you report doesn't make sense. That file has this import near the beginning:
from txnet.icmp import ICMP, Packet, ECHO_REQUEST
This defines the ICMP name. The Pinger class comes later, extending it. I can successfully run this demo program (though it fails with an unrecognized message type when handling the response on my sytem).
Perhaps you have an old version of the code, or have modified it somehow, or are running a different ping.py?