I have below code to send command, but python script never exits. it hangs. i am using RHEL 6.4 x86_64. scapy srp1 also hangs.
from socket import *
from scapy.all import *
from myproto import *
def sendeth(REQUEST, interface = "eth0"):
"""Send raw Ethernet packet on interface."""
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
s.bind((interface, 0))
s.send(REQUEST)
data = s.recv(2048)
hexdump(data)
p = Ether()/MYPROTO()/MYPROTO1()
hexdump(p)
if __name__ == "__main__":
print "Sent %d-byte Ethernet packet on eth3" % sendeth(str(p), 'eth3')
But after execution, I see the frame on tcpdump, but the python code never exits and need a control^C.
tcpdump: WARNING: eth3: no IPv4 address assigned
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth3, link-type EN10MB (Ethernet), capture size 65535 bytes
17:10:37.122445 00:00:00:00:00:00 (oui Ethernet) > Broadcast, ethertype Unknown (0x8096), length 34:
0x0000: ffff ffff ffff 0000 0000 0000 8096 0001
0x0010: 0001 1500 0000 0000 0000 0000 0000 ffff
0x0020: eafe
17:10:37.133248 00:04:25:1c:a0:02 (oui Unknown) > Broadcast, ethertype Unknown (0x8096), length 76:
0x0000: ffff ffff ffff 0004 251c a002 8096 0001
0x0010: 0001 9500 0028 0000 0000 0000 0000 0000
0x0020: 0000 f1f0 f100 0000 0000 0000 0000 0000
0x0030: 0000 0000 0000 0803 0087 1634 8096 8096
0x0040: e4f2 0000 0f21 fffc 5427 ffff
By the time you call recv()
on the socket, the network stack in the kernel already dispatched the frame you are sending, and noticing that nothing is waiting for it on the local host, discarded it.
Try running sender and receiver as different processes.
Try adding your specific protocol number as the third parameter to socket()
. Here is an example that grabs ten ethernet frames off my eth0
on Debian (needs root
privileges):
import socket
# from linux/if_etheer.h, all ethernet protocols, be very careful
ETH_P_ALL=0x0003
s=socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
s.bind(('eth0', 0))
for n in range(10):
print n, len(s.recv(2048))