# 发送UDP数据包
def send_udp_command(ip, port, command):
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
server_address = (ip, int(port))
sock.sendto(command.encode(), server_address)
print(f"对 {ip}:{port} 已发送开机指令")
except Exception as e:
print(f"发送数据到 {ip}:{port} 时出错: {str(e)}")
# 主循环
while True:
current_time = get_current_time()
if current_time >= shutdown_time:
print("到达预设关闭时间,程序即将退出。")
os._exit(0) # 安全退出程序
for key, command in ip_commands.items():
ip, port = key.split(':')
if ip not in pinged_ips:
# 尝试ping IP地址
try:
response = call(['ping', '-n', '1', ip], stdout=PIPE, stderr=PIPE)
if response == 0: # ping成功
pinged_ips.append(ip)
send_udp_command(ip, port, command)
# 根据IP打印特定的启动信息
if ip == "10.111.92.202":
print("6号厅还音柜启动")
elif ip == "10.111.96.114":
print("2号厅启动")
elif ip == "10.111.96.115":
print("3号厅启动")
time.sleep(2) # 等待2秒
except Exception as e:
print(f"尝试ping {ip} 时出错: {str(e)}")