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)