|
| 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("DEBIAN_FRONTEND=noninteractive apt update -y && DEBIAN_FRONTEND=noninteractive apt full-upgrade -y") |
| 24 | + stdin.write('Y\n') # 将默认响应设置为 Y(安装软件包维护者的版本) |
| 25 | + stdin.flush() # 刷新输入流 |
| 26 | + |
| 27 | + print(f"{name} 开始更新") |
| 28 | + while not stdout.channel.exit_status_ready(): |
| 29 | + if stdout.channel.recv_ready(): |
| 30 | + print(stdout.channel.recv(1024).decode(), end="") |
| 31 | + |
| 32 | + # 检查更新状态 |
| 33 | + if stderr.channel.recv_exit_status() == 0: |
| 34 | + print(f"更新成功") |
| 35 | + else: |
| 36 | + print(f"更新失败") |
| 37 | + |
| 38 | + print() |
| 39 | + |
| 40 | + stdin, stdout, stderr = client.exec_command("cat > /etc/apt/sources.list << EOF\ndeb http://deb.debian.org/debian testing main\ndeb-src http://deb.debian.org/debian testing main\n\ndeb http://deb.debian.org/debian-security/ testing-security main\ndeb-src http://deb.debian.org/debian-security/ testing-security main\n\ndeb http://deb.debian.org/debian testing-updates main\ndeb-src http://deb.debian.org/debian testing-updates main\nEOF") |
| 41 | + |
| 42 | + print(f"{name} 导出官方更新源") |
| 43 | + while not stdout.channel.exit_status_ready(): |
| 44 | + if stdout.channel.recv_ready(): |
| 45 | + print(stdout.channel.recv(1024).decode(), end="") |
| 46 | + |
| 47 | + # 检查更新状态 |
| 48 | + if stderr.channel.recv_exit_status() == 0: |
| 49 | + print(f"导入成功") |
| 50 | + else: |
| 51 | + print(f"导入失败") |
| 52 | + |
| 53 | + print() |
| 54 | + |
| 55 | + stdin, stdout, stderr = client.exec_command("DEBIAN_FRONTEND=noninteractive apt update -y && DEBIAN_FRONTEND=noninteractive apt full-upgrade -y && apt install -y curl wget sudo socat htop") |
| 56 | + stdin.write('Y\n') # 将默认响应设置为 Y(安装软件包维护者的版本) |
| 57 | + stdin.flush() # 刷新输入流 |
| 58 | + |
| 59 | + print(f"{name} 开始升级") |
| 60 | + while not stdout.channel.exit_status_ready(): |
| 61 | + if stdout.channel.recv_ready(): |
| 62 | + print(stdout.channel.recv(1024).decode(), end="") |
| 63 | + |
| 64 | + # 检查更新状态 |
| 65 | + if stderr.channel.recv_exit_status() == 0: |
| 66 | + print(f"升级成功") |
| 67 | + else: |
| 68 | + print(f"升级失败") |
| 69 | + |
| 70 | + print() |
| 71 | + |
| 72 | + stdin, stdout, stderr = client.exec_command("apt autoremove --purge -y && apt clean -y && apt autoclean -y && apt remove --purge $(dpkg -l | awk '/^rc/ {print $2}') -y && journalctl --rotate && journalctl --vacuum-time=1s && journalctl --vacuum-size=50M && apt remove --purge $(dpkg -l | awk '/^ii linux-(image|headers)-[^ ]+/{print $2}' | grep -v $(uname -r | sed 's/-.*//') | xargs) -y") |
| 73 | + |
| 74 | + print(f"{name} 开始清理") |
| 75 | + while not stdout.channel.exit_status_ready(): |
| 76 | + if stdout.channel.recv_ready(): |
| 77 | + print(stdout.channel.recv(1024).decode(), end="") |
| 78 | + |
| 79 | + # 检查更新状态 |
| 80 | + if stderr.channel.recv_exit_status() == 0: |
| 81 | + print(f"清理成功") |
| 82 | + else: |
| 83 | + print(f"清理失败") |
| 84 | + |
| 85 | + print() |
| 86 | + |
| 87 | + # 检查更新状态 |
| 88 | + stdin, stdout, stderr = client.exec_command("reboot") |
| 89 | + print(f"{name} 重启服务器") |
| 90 | + print() |
| 91 | + print() |
| 92 | + |
| 93 | + # 关闭 SSH 连接 |
| 94 | + client.close() |
| 95 | + |
| 96 | + |
| 97 | + except Exception as e: |
| 98 | + print(f"连接 {name} 失败") |
| 99 | + |
| 100 | + print() |
| 101 | + print() |
| 102 | + |
| 103 | +# 遍历服务器列表,逐一更新 |
| 104 | +for server in servers: |
| 105 | + name = server["name"] |
| 106 | + hostname = server["hostname"] |
| 107 | + port = server["port"] |
| 108 | + username = server["username"] |
| 109 | + password = server["password"] |
| 110 | + update_server(name, hostname, port, username, password) |
| 111 | + |
| 112 | +# 等待用户按下任意键后关闭窗口 |
| 113 | +input("按任意键关闭窗口...") |
0 commit comments