Hello to everyone. That’s the scenario
import socket
import os
import logging
import psutil
WOL_PORT = 9999
INTERFACE_NAME = "My Lan"
INTERFACE_NAME_ARCH = "enp37s0"
logging.basicConfig(format="%(levelname)s: %(asctime)s %(message)s", level=logging.INFO)
logger = logging.getLogger(__name__)
def get_ip_mac_address(interface_name: str) -> tuple:
ip_addr = mac_addr = None
for item in psutil.net_if_addrs()[interface_name]:
addr = item.address
if "." in addr:
ip_addr = addr
elif ("-" in addr or ":" in addr) and "::" not in addr:
mac_addr = addr.replace(":", "-").upper()
if not ip_addr or not mac_addr or ip_addr == "127.0.0.1":
raise "Не удалось получить IP или MAC-адрес сетевого интерфейса"
return ip_addr, mac_addr
def assemble_wol_packet(mac_address: str) -> str:
return f'{"FF-" * 6}{(mac_address + "-") * 16}'
def check_is_wol_packet(raw_bytes: bytes, assembled_wol_packet: str) -> int:
decoded_packet_data = "-".join(f"{byte:02x}" for byte in raw_bytes).upper() + "-"
if decoded_packet_data == assembled_wol_packet:
return 1
return 0
def run_udp_port_listener(port: int, interface_name: str):
ip_addr, mac_addr = get_ip_mac_address(interface_name)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind((ip_addr, port))
logger.info(f"Listening on {ip_addr}:{port}")
assembled_wol_packet = assemble_wol_packet(mac_addr)
while True:
data, _ = server_socket.recvfrom(1024)
is_wol_packet = check_is_wol_packet(data, assembled_wol_packet)
if is_wol_packet == 1:
if os.name == "posix":
os.system("reboot")
elif os.name == "nt":
os.system(
"shutdown -r -t 0 -f"
)
if os.name == "posix":
run_udp_port_listener(WOL_PORT, INTERFACE_NAME_ARCH)
elif os.name == "nt":
run_udp_port_listener(WOL_PORT, INTERFACE_NAME)
It works great in Windows. That is, the essence is, I start the script, it listens to the port 9999 (WOL). From the phone, I send a package to this port and, accordingly, depending on the OS (this in the scenario), a command is executed. So in Arch I run, netstat -tulpan shows that the port is listening, I send a package from the phone, and nothing happens, although there should be restart. I checked the command separately - os.system (“reboot”) is working.
Tell me what is the problem?
PS. The firewall is not installed, iptables -L everything on ACCEPT.
Sorry for my English, this is not my native language.