LAN WOL 대상 판정 정리

This commit is contained in:
seo committed 2026-08-29 02:29:52 +09:00
1 parent c22f84e430
commit 4fb9af695c
4 files changed
+218 -45

No files matched your search

+38 -4
View File
@@ -28,7 +28,7 @@ NET_GROUPS = {
}
PC_IP = "192.168.50.73"
PC_MAC = "74:56:3C:CD:CD:79"
PC_DHCP_LEASE_FILE = "/var/lib/misc/dnsmasq.leases"
SYNOLOGY_PING_HOST = "chaegeon.com"
SYNOLOGY_SSH = [
"/usr/bin/ssh",
@@ -339,15 +339,46 @@ def compact_count(value):
return str(value)
def send_magic_packet(mac):
def normalize_mac(mac):
clean = mac.replace(":", "").replace("-", "")
if len(clean) != 12:
return ""
try:
bytes.fromhex(clean)
except ValueError:
return ""
return ":".join(clean[i:i + 2] for i in range(0, 12, 2)).lower()
def dhcp_mac_for_ip(ip):
for line in read_text(PC_DHCP_LEASE_FILE).splitlines():
parts = line.split()
if len(parts) >= 3 and parts[2] == ip:
mac = normalize_mac(parts[1])
if mac:
return mac
return ""
def broadcast_targets_for_ip(ip):
parts = ip.split(".")
targets = []
if len(parts) == 4 and all(part.isdigit() and 0 <= int(part) <= 255 for part in parts):
targets.append(".".join(parts[:3] + ["255"]))
targets.append("255.255.255.255")
return list(dict.fromkeys(targets))
def send_magic_packet(mac, ip=PC_IP):
mac = normalize_mac(mac)
if not mac:
return False
clean = mac.replace(":", "")
packet = bytes.fromhex("ff" * 6 + clean * 16)
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
for target in ["192.168.50.255", "255.255.255.255"]:
for target in broadcast_targets_for_ip(ip):
sock.sendto(packet, (target, 9))
return True
except OSError:
@@ -532,10 +563,13 @@ class PcWakeButton(StatusTile):
self.error_until = 0.0
self.error_dot.set_steady(False)
self.detail.setText("깨우는 중")
if not send_magic_packet(PC_MAC):
mac = dhcp_mac_for_ip(PC_IP)
if not mac or not send_magic_packet(mac, PC_IP):
self.wait_until = 0.0
self.error_until = now + 5
self.error_dot.set_steady(True)
self.write_dot.set_steady(False)
self.detail.setText("MAC 없음" if not mac else "실패")
super().mousePressEvent(event)
def tick(self):