ios-hooks/ios_wh.py
2025-09-05 18:48:22 +08:00

60 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import paramiko
import socket
import time
url = "http://192.168.9.11:8080/ios/top_selection/heartbeats"
data = """
{
"url": "/start"
}
""".encode('utf-8')
def restart(ip):
try:
# 实例化SSHClient
ssh_client = paramiko.SSHClient()
# 自动添加策略保存服务器的主机名和密钥信息如果不添加那么不再本地know_hosts文件中记录的主机将无法连接 此方法必须放在connect方法的前面
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接SSH服务端以用户名和密码进行认证 调用connect方法连接服务器
ssh_client.connect(hostname=ip, port=22, username='root', password='alpine', timeout=3)
# 打开一个Channel并执行命令 结果放到stdout中如果有错误将放到stderr中
stdin, stdout, stderr = ssh_client.exec_command('killall -9 SpringBoard', timeout=3)
print(stdout.read().decode('utf-8'))
print(stderr.read().decode('utf-8'))
ssh_client.close()
except Exception as e:
print(e)
def start(ip):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(5)
# 发送数据:
s.sendto(data, (ip.strip(), 6001))
# 接收数据:
print(s.recv(1024).decode('utf-8'))
s.close()
except Exception as e:
print(e)
ips = []
res = requests.get(url)
json = res.json()
for it in json:
if it["life"] and it["loadCount"] < 30:
ips.append(it["ip"])
print(ips)
for ip in ips:
print(ip)
restart(ip)
time.sleep(5)
for ip in ips:
print(ip)
start(ip)