j0h

open_groov_find

j0h
Jul 21st, 2025
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from scapy.all import sniff, Ether, get_if_hwaddr
  3. '''
  4. Notes:
  5. We are assuimg that the network structure is something like  the PC and the OPTO direct connection.
  6. We are also assuming the device is an opto. realisticly, this code will find any other
  7. attached device mac address and assume it is an opto.
  8. We're also assuming our interface name as Ethernet4, rather than trying to define it using any OS
  9. specific determinations. will fix that later.
  10. '''
  11. # Format the peer MAC address to the OPTO hostname format
  12. def format_opto(mac_str):
  13.     parts = mac_str.lower().split(":")[-3:]
  14.     return f"https://opto-{parts[0]}-{parts[1]}-{parts[2]}"
  15.  
  16. def find_peer_mac(interface):
  17.     my_mac = get_if_hwaddr(interface).lower()
  18.     print(f"My MAC on {interface}: {my_mac}")
  19.  
  20.     found = {"mac": None}
  21.  
  22.     def handle(pkt):
  23.         if Ether in pkt:
  24.             src = pkt[Ether].src.lower()
  25.             if src != my_mac:
  26.                 found["mac"] = src  # store the MAC to print later
  27.         return
  28.  
  29.     def should_stop(pkt):
  30.         return Ether in pkt and pkt[Ether].src.lower() != my_mac
  31.  
  32.     print("Searching for peer (OPTO) device")
  33.     sniff(iface=interface, prn=handle, stop_filter=should_stop, timeout=10)
  34.  
  35.     if found["mac"]:
  36.         print(f"Peer MAC address: {found['mac']}")
  37.         print(f"Opto hostname:\n{format_opto(found['mac'])}")
  38.     else:
  39.         print("No device found.")
  40.  
  41. find_peer_mac("Ethernet 4")
  42.  
Advertisement
Add Comment
Please, Sign In to add comment