-
-
Save cslarsen/11339448 to your computer and use it in GitHub Desktop.
| """Demonstrates how to construct and send raw Ethernet packets on the | |
| network. | |
| You probably need root privs to be able to bind to the network interface, | |
| e.g.: | |
| $ sudo python sendeth.py | |
| """ | |
| from socket import * | |
| def sendeth(src, dst, eth_type, payload, interface = "eth0"): | |
| """Send raw Ethernet packet on interface.""" | |
| assert(len(src) == len(dst) == 6) # 48-bit ethernet addresses | |
| assert(len(eth_type) == 2) # 16-bit ethernet type | |
| s = socket(AF_PACKET, SOCK_RAW) | |
| # From the docs: "For raw packet | |
| # sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])" | |
| s.bind((interface, 0)) | |
| return s.send(src + dst + eth_type + payload) | |
| if __name__ == "__main__": | |
| print("Sent %d-byte Ethernet packet on eth0" % | |
| sendeth("\xFE\xED\xFA\xCE\xBE\xEF", | |
| "\xFE\xED\xFA\xCE\xBE\xEF", | |
| "\x7A\x05", | |
| "hello")) |
def pack(byte_sequence):
"""Convert list of bytes to byte string."""
return b"".join(map(chr, byte_sequence))
ethernet_packet = [0x52, 0x54, 0x00, 0x12, 0x35, 0x02, 0xfe, 0xed, 0xfa,
0xce, 0xbe, 0xef, 0x08, 0x00]
print(pack(ethernet_packet))
Traceback (most recent call last):
File "convert.py", line 9, in
print(pack(ethernet_packet))
File convert.py", line 3, in pack
return b"".join(map(chr, byte_sequence))
TypeError: sequence item 0: expected a bytes-like object, str found
#python env 3.4
this code not run windwos ENV,
@cslarsen Could I use this to talk to the internet from an air gapped computer via QR code? I have the QR two way comm figured out...
Using code based on this I encountered this problem - when viewed in Wireshark, it seems Python is tacking some extra bytes onto the end of this "raw" data. Anyone else experiencing this?
@geajack If you send less than Ethernet's minimum frame size it will be padded. The number is (as I remember) 60 bytes. It is actually a good sign if your packet is padded.
i want send packet data on CANoe 18 using python also get data
Running it i get:
Traceback (most recent call last):
File "sendeth.py", line 52, in
r = sendeth(pack(ethernet_packet),
File "sendeth.py", line 23, in pack
return b"".join(map(chr, byte_sequence))
TypeError: sequence item 0: expected a bytes-like object, str found
which object do i need to convert from a str to bytes ?