|
| 1 | +import paramiko |
| 2 | + |
| 3 | +servers = [ |
| 4 | + |
| 5 | + {"name": "美国", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456", "domain": "yuming.com"}, |
| 6 | + {"name": "不丹", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456", "domain": "yuming.com"}, |
| 7 | + {"name": "毛里求斯", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456", "domain": "yuming.com"}, |
| 8 | + # 添加更多服务器 |
| 9 | + |
| 10 | +] |
| 11 | + |
| 12 | +# 定义更新操作 |
| 13 | +def update_server(name, hostname, port, username, password, domain): |
| 14 | + try: |
| 15 | + |
| 16 | + # 连接服务器 |
| 17 | + client = paramiko.SSHClient() |
| 18 | + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 19 | + client.connect(hostname, port=port, username=username, password=password) |
| 20 | + |
| 21 | + |
| 22 | + print(f" {name} 更新") |
| 23 | + stdin, stdout, stderr = client.exec_command("apt update -y && apt install -y curl wget sudo socat") |
| 24 | + |
| 25 | + print(f"正在更新:") |
| 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 | + print(f"{name} 安装 Docker") |
| 39 | + stdin, stdout, stderr = client.exec_command("curl -fsSL https://get.docker.com | sh") |
| 40 | + |
| 41 | + print(f"正在安装 Docker:") |
| 42 | + while not stdout.channel.exit_status_ready(): |
| 43 | + if stdout.channel.recv_ready(): |
| 44 | + print(stdout.channel.recv(1024).decode(), end="") |
| 45 | + |
| 46 | + # 检查执行状态 |
| 47 | + if stderr.channel.recv_exit_status() == 0: |
| 48 | + print(f"安装 Docker 成功") |
| 49 | + else: |
| 50 | + print(f"安装 Docker 失败") |
| 51 | + |
| 52 | + print() |
| 53 | + |
| 54 | + print(f"{name} 安装 Docker Compose") |
| 55 | + stdin, stdout, stderr = client.exec_command('curl -L "https://github.com/docker/compose/releases/download/v2.18.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose') |
| 56 | + |
| 57 | + print(f"正在安装 Docker Compose:") |
| 58 | + while not stdout.channel.exit_status_ready(): |
| 59 | + if stdout.channel.recv_ready(): |
| 60 | + print(stdout.channel.recv(1024).decode(), end="") |
| 61 | + |
| 62 | + # 检查执行状态 |
| 63 | + if stderr.channel.recv_exit_status() == 0: |
| 64 | + print(f"安装 Docker Compose 成功") |
| 65 | + else: |
| 66 | + print(f"安装 Docker Compose 失败") |
| 67 | + |
| 68 | + print() |
| 69 | + |
| 70 | + |
| 71 | + print(f"{name} 创建web目录") |
| 72 | + stdin, stdout, stderr = client.exec_command("cd /home && mkdir -p web/html web/mysql web/certs web/redis && touch web/nginx.conf web/docker-compose.yml") |
| 73 | + |
| 74 | + while not stdout.channel.exit_status_ready(): |
| 75 | + if stdout.channel.recv_ready(): |
| 76 | + print(stdout.channel.recv(1024).decode(), end="") |
| 77 | + |
| 78 | + # 检查执行状态 |
| 79 | + if stderr.channel.recv_exit_status() == 0: |
| 80 | + print(f"创建目录成功") |
| 81 | + else: |
| 82 | + print(f"创建目录失败") |
| 83 | + |
| 84 | + print() |
| 85 | + |
| 86 | + print(f"{name} 申请证书") |
| 87 | + stdin, stdout, stderr = client. exec_command( "curl https://get.acme.sh | sh && ~/.acme.sh/acme.sh --register-account -m [email protected] && ~/.acme.sh/acme.sh --issue -d {} --standalone". format( domain)) |
| 88 | + print(f"正在申请中:") |
| 89 | + while not stdout.channel.exit_status_ready(): |
| 90 | + if stdout.channel.recv_ready(): |
| 91 | + print(stdout.channel.recv(1024).decode(), end="") |
| 92 | + |
| 93 | + # 检查执行状态 |
| 94 | + if stderr.channel.recv_exit_status() == 0: |
| 95 | + print(f"申请成功") |
| 96 | + else: |
| 97 | + print(f"申请失败") |
| 98 | + |
| 99 | + print() |
| 100 | + |
| 101 | + print(f"{name} 下载证书") |
| 102 | + stdin, stdout, stderr = client.exec_command("~/.acme.sh/acme.sh --installcert -d {} --key-file /home/web/certs/key.pem --fullchain-file /home/web/certs/cert.pem".format(domain)) |
| 103 | + while not stdout.channel.exit_status_ready(): |
| 104 | + if stdout.channel.recv_ready(): |
| 105 | + print(stdout.channel.recv(1024).decode(), end="") |
| 106 | + |
| 107 | + # 检查执行状态 |
| 108 | + if stderr.channel.recv_exit_status() == 0: |
| 109 | + print(f"下载证书成功") |
| 110 | + else: |
| 111 | + print(f"下载证书失败") |
| 112 | + |
| 113 | + print() |
| 114 | + |
| 115 | + print(f"{name} 配置nginx") |
| 116 | + stdin, stdout, stderr = client.exec_command('wget -O /home/web/nginx.conf https://raw.githubusercontent.com/kejilion/nginx/main/nginx7.conf && sed -i "s/yuming.com/' + domain + '/g" /home/web/nginx.conf') |
| 117 | + while not stdout.channel.exit_status_ready(): |
| 118 | + if stdout.channel.recv_ready(): |
| 119 | + print(stdout.channel.recv(1024).decode(), end="") |
| 120 | + |
| 121 | + # 检查执行状态 |
| 122 | + if stderr.channel.recv_exit_status() == 0: |
| 123 | + print(f"配置成功") |
| 124 | + else: |
| 125 | + print(f"配置失败") |
| 126 | + |
| 127 | + print() |
| 128 | + |
| 129 | + print(f"{name} 配置docker-compose.yml") |
| 130 | + stdin, stdout, stderr = client.exec_command('wget -O /home/web/docker-compose.yml https://raw.githubusercontent.com/kejilion/docker/main/LNMP-docker-compose-2.yml') |
| 131 | + while not stdout.channel.exit_status_ready(): |
| 132 | + if stdout.channel.recv_ready(): |
| 133 | + print(stdout.channel.recv(1024).decode(), end="") |
| 134 | + |
| 135 | + # 检查执行状态 |
| 136 | + if stderr.channel.recv_exit_status() == 0: |
| 137 | + print(f"配置成功") |
| 138 | + else: |
| 139 | + print(f"配置失败") |
| 140 | + |
| 141 | + print() |
| 142 | + |
| 143 | + print(f"{name} 下载网站源码-发卡网站") |
| 144 | + stdin, stdout, stderr = client.exec_command('cd /home/web/html && wget https://github.com/assimon/dujiaoka/releases/download/2.0.6/2.0.6-antibody.tar.gz && apt install -y tar && tar -zxvf 2.0.6-antibody.tar.gz && rm 2.0.6-antibody.tar.gz') |
| 145 | + while not stdout.channel.exit_status_ready(): |
| 146 | + if stdout.channel.recv_ready(): |
| 147 | + print(stdout.channel.recv(1024).decode(), end="") |
| 148 | + |
| 149 | + # 检查执行状态 |
| 150 | + if stderr.channel.recv_exit_status() == 0: |
| 151 | + print(f"配置成功") |
| 152 | + else: |
| 153 | + print(f"配置失败") |
| 154 | + |
| 155 | + |
| 156 | + print() |
| 157 | + |
| 158 | + print(f"{name} 启动环境") |
| 159 | + stdin, stdout, stderr = client.exec_command('cd /home/web && docker-compose up -d') |
| 160 | + print(f"启动中:") |
| 161 | + while not stdout.channel.exit_status_ready(): |
| 162 | + if stdout.channel.recv_ready(): |
| 163 | + print(stdout.channel.recv(1024).decode(), end="") |
| 164 | + |
| 165 | + # 检查执行状态 |
| 166 | + if stderr.channel.recv_exit_status() == 0: |
| 167 | + print(f"启动成功") |
| 168 | + else: |
| 169 | + print(f"启动失败") |
| 170 | + |
| 171 | + print() |
| 172 | + |
| 173 | + |
| 174 | + print(f"{name} 赋予文件权限") |
| 175 | + stdin, stdout, stderr = client.exec_command('docker exec nginx chmod -R 777 /var/www/html && docker exec php chmod -R 777 /var/www/html') |
| 176 | + while not stdout.channel.exit_status_ready(): |
| 177 | + if stdout.channel.recv_ready(): |
| 178 | + print(stdout.channel.recv(1024).decode(), end="") |
| 179 | + |
| 180 | + # 检查执行状态 |
| 181 | + if stderr.channel.recv_exit_status() == 0: |
| 182 | + print(f"赋予成功") |
| 183 | + else: |
| 184 | + print(f"赋予失败") |
| 185 | + |
| 186 | + print() |
| 187 | + |
| 188 | + print(f"{name} 安装PHP依赖") |
| 189 | + stdin, stdout, stderr = client.exec_command('docker exec php apt update && docker exec php apt install -y libmariadb-dev-compat libmariadb-dev libzip-dev') |
| 190 | + print(f"安装中:") |
| 191 | + while not stdout.channel.exit_status_ready(): |
| 192 | + if stdout.channel.recv_ready(): |
| 193 | + print(stdout.channel.recv(1024).decode(), end="") |
| 194 | + |
| 195 | + # 检查执行状态 |
| 196 | + if stderr.channel.recv_exit_status() == 0: |
| 197 | + print(f"安装成功") |
| 198 | + else: |
| 199 | + print(f"安装失败") |
| 200 | + |
| 201 | + print() |
| 202 | + |
| 203 | + print(f"{name} 安装PHP扩展") |
| 204 | + stdin, stdout, stderr = client.exec_command('docker exec php docker-php-ext-install pdo_mysql zip && docker exec php pecl install redis && docker exec php sh -c \'echo "extension=redis.so" > /usr/local/etc/php/conf.d/docker-php-ext-redis.ini\' && docker restart php') |
| 205 | + print(f"安装中:") |
| 206 | + while not stdout.channel.exit_status_ready(): |
| 207 | + if stdout.channel.recv_ready(): |
| 208 | + print(stdout.channel.recv(1024).decode(), end="") |
| 209 | + |
| 210 | + # 检查执行状态 |
| 211 | + if stderr.channel.recv_exit_status() == 0: |
| 212 | + print(f"安装成功") |
| 213 | + else: |
| 214 | + print(f"安装失败") |
| 215 | + |
| 216 | + print() |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + print(f"搭建完成\nhttps://{domain}") |
| 222 | + print() |
| 223 | + print() |
| 224 | + |
| 225 | + |
| 226 | + print() |
| 227 | + print() |
| 228 | + |
| 229 | + # 关闭 SSH 连接 |
| 230 | + client.close() |
| 231 | + |
| 232 | + |
| 233 | + except Exception as e: |
| 234 | + print(f"连接 {name} 失败") |
| 235 | + |
| 236 | + |
| 237 | +# 遍历服务器列表,逐一更新 |
| 238 | +for server in servers: |
| 239 | + name = server["name"] |
| 240 | + hostname = server["hostname"] |
| 241 | + port = server["port"] |
| 242 | + username = server["username"] |
| 243 | + password = server["password"] |
| 244 | + domain = server["domain"] |
| 245 | + update_server(name, hostname, port, username, password, domain) |
| 246 | + |
| 247 | +# 等待用户按下任意键后关闭窗口 |
| 248 | +input("按任意键关闭窗口...") |
| 249 | + |
| 250 | + |
0 commit comments