Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import socket
  2. import struct
  3. import textwrap
  4.  
  5. def main():
  6. conn = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
  7.  
  8. while True:
  9. raw_data, addr = conn.recvfrom(65535)
  10. dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
  11. print("\nEthernet Frame:")
  12. print("Destination: {}, Source: {}, Protocol: {}".format(dest_mac, src_mac, eth_proto))
  13.  
  14. def get_mac_addr(bytes_addr):
  15. bytes_str = map("{:0.2x}".format, bytes_addr)
  16. return ":".join(bytes_str).upper()
  17.  
  18. def ethernet_frame(data):
  19. dest_mac, src_mac, proto = struct.unpack("! 6s 6s H", data[:14])
  20. return get_mac_addr(dest_mac), get_mac_addr(src_mac), socket.htons(proto), data[14:]
  21.  
  22. if __name__ == "__main__":
  23. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement