How do I send a raw ethernet frame in python?

后端 未结 2 1881
北海茫月
北海茫月 2021-01-03 08:42

I need to have a project done in a few days, its a basic client and server interface. The catch is that it needs to be all raw sockets. I have no problem with creating that,

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 09:38

    thanks to the comments for this question, i managed to get a connection going with a server. all it took was changing the address family to af_packet in linux. then i binded it to my nic and sent it. it worked. thanks for the help people! here is some example code:

    s = socket(AF_PACKET, SOCK_RAW)
    s.bind(("en1", 0))
    pckt = packet() #my class that initializes the raw hex
    data = pckt.getpacket()
    s.send(data)
    message = s.recv(4096)
    print s
    print s.decode('hex')
    

    It needs to be in linux or debian. to my knowldege it doesnt work in mac osx. idk about windows. if u have a mac use pycap or scapy, they work fine.

提交回复
热议问题