|
| 1 | + |
| 2 | + |
| 3 | +import paramiko |
| 4 | + |
| 5 | +# 定义服务器列表,包括服务器名称、IP地址、端口号、用户名和密码 |
| 6 | +servers = [ |
| 7 | + {"name": "美国", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456"}, |
| 8 | + {"name": "不丹", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456"}, |
| 9 | + {"name": "毛里求斯", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456"}, |
| 10 | + # 添加更多服务器 |
| 11 | +] |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +# 定义更新操作 |
| 16 | +def update_server(name, hostname, port, username, password): |
| 17 | + try: |
| 18 | + # 连接服务器 |
| 19 | + client = paramiko.SSHClient() |
| 20 | + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 21 | + client.connect(hostname, port=port, username=username, password=password) |
| 22 | + |
| 23 | + stdin, stdout, stderr = client.exec_command("apt update && apt full-upgrade -y") |
| 24 | + |
| 25 | + print(f"{name} 开始更新") |
| 26 | + while not stdout.channel.exit_status_ready(): |
| 27 | + if stdout.channel.recv_ready(): |
| 28 | + print(stdout.channel.recv(1024).decode(), end="") |
| 29 | + |
| 30 | + # 检查更新状态 |
| 31 | + if stderr.channel.recv_exit_status() == 0: |
| 32 | + print(f"更新成功") |
| 33 | + else: |
| 34 | + print(f"更新失败") |
| 35 | + |
| 36 | + print() |
| 37 | + |
| 38 | + stdin, stdout, stderr = client.exec_command("sudo apt autoremove --purge -y && sudo apt clean -y && sudo apt autoclean -y && sudo apt remove --purge $(dpkg -l | awk '/^rc/ {print $2}') -y && sudo journalctl --rotate && sudo journalctl --vacuum-time=1s && sudo journalctl --vacuum-size=50M && sudo apt remove --purge $(dpkg -l | awk '/^ii linux-(image|headers)-[^ ]+/{print $2}' | grep -v $(uname -r | sed 's/-.*//') | xargs) -y") |
| 39 | + |
| 40 | + print(f"{name} 开始清理") |
| 41 | + while not stdout.channel.exit_status_ready(): |
| 42 | + if stdout.channel.recv_ready(): |
| 43 | + print(stdout.channel.recv(1024).decode(), end="") |
| 44 | + |
| 45 | + # 检查更新状态 |
| 46 | + if stderr.channel.recv_exit_status() == 0: |
| 47 | + print(f"清理成功") |
| 48 | + else: |
| 49 | + print(f"清理失败") |
| 50 | + |
| 51 | + |
| 52 | + # 关闭 SSH 连接 |
| 53 | + client.close() |
| 54 | + |
| 55 | + print() |
| 56 | + print() |
| 57 | + |
| 58 | + except Exception as e: |
| 59 | + print(f"连接 {name} 失败") |
| 60 | + |
| 61 | + print() |
| 62 | + print() |
| 63 | + |
| 64 | +# 遍历服务器列表,逐一更新 |
| 65 | +for server in servers: |
| 66 | + name = server["name"] |
| 67 | + hostname = server["hostname"] |
| 68 | + port = server["port"] |
| 69 | + username = server["username"] |
| 70 | + password = server["password"] |
| 71 | + update_server(name, hostname, port, username, password) |
| 72 | + |
| 73 | +# 等待用户按下任意键后关闭窗口 |
| 74 | +input("按任意键关闭窗口...") |
0 commit comments