Raw Sockets on Linux
suggest changeFirst you disable your network card’s automatic checksumming:
sudo ethtool -K eth1 tx off
Then send your packet, using a SOCK_RAW socket:
#!/usr/bin/env python
from socket import socket, AF_PACKET, SOCK_RAW
s = socket(AF_PACKET, SOCK_RAW)
s.bind(("eth1", 0))
# We're putting together an ethernet frame here,
# but you could have anything you want instead
# Have a look at the 'struct' module for more
# flexible packing/unpacking of binary data
# and 'binascii' for 32 bit CRC
src_addr = b"\x01\x02\x03\x04\x05\x06"
dst_addr = b"\x01\x02\x03\x04\x05\x06"
payload = (b"[" * 30) + b"PAYLOAD" + (b"]" * 30)
checksum = b"\x1a\x2b\x3c\x4d"
ethertype = b"\x08\x01"
s.send(dst_addr + src_addr + ethertype + payload + checksum)
socket.send requires a bytes-like object. AF_PACKET is not available on Windows. This script was not run here.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents