|
| 1 | +import paramiko |
| 2 | + |
| 3 | +servers = [ |
| 4 | + |
| 5 | + {"name": "吉隆坡", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456", "dbrootpasswd": "webroot", "dbuse": "one", "dbusepasswd": "yyds", "domain": "a1.yuming.com", "dbname": "db1"}, |
| 6 | + |
| 7 | +] |
| 8 | + |
| 9 | + |
| 10 | +# 定义更新操作 |
| 11 | +def update_server(name, hostname, port, username, password, domain, dbrootpasswd, dbuse, dbname ): |
| 12 | + try: |
| 13 | + |
| 14 | + # 连接服务器 |
| 15 | + client = paramiko.SSHClient() |
| 16 | + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 17 | + client.connect(hostname, port=port, username=username, password=password) |
| 18 | + |
| 19 | + |
| 20 | + stdin, stdout, stderr = client.exec_command("docker stop nginx") |
| 21 | + stdout.channel.recv_exit_status() # 等待命令执行完成 |
| 22 | + |
| 23 | + |
| 24 | + cert_command = ( |
| 25 | + "curl https://get.acme.sh | sh && " |
| 26 | + "~/.acme.sh/acme.sh --register-account -m [email protected] --issue -d {} " |
| 27 | + "--standalone --key-file /home/web/certs/{}_key.pem --cert-file /home/web/certs/{}_cert.pem --force" |
| 28 | + ).format(domain, domain, domain) |
| 29 | + |
| 30 | + # 使用 client.exec_command 执行命令,并将标准输出和标准错误重定向到 /dev/null |
| 31 | + stdin, stdout, stderr = client.exec_command(f"{cert_command} > /dev/null 2>&1") |
| 32 | + |
| 33 | + # 等待命令执行完成 |
| 34 | + stdout.channel.recv_exit_status() |
| 35 | + |
| 36 | + |
| 37 | + stdin, stdout, stderr = client.exec_command("docker start nginx") |
| 38 | + stdout.channel.recv_exit_status() # 等待命令执行完成 |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + stdin, stdout, stderr = client.exec_command('wget -O /home/web/conf.d/' + domain + '.conf https://raw.githubusercontent.com/kejilion/nginx/main/discuz.com.conf && \ |
| 43 | + sed -i "s/yuming.com/' + domain + '/g" /home/web/conf.d/' + domain + '.conf') |
| 44 | + |
| 45 | + stdout.channel.recv_exit_status() # 等待命令执行完成 |
| 46 | + |
| 47 | + |
| 48 | + # 使用字符串拼接来组织命令,增加可读性 |
| 49 | + command = ( |
| 50 | + 'cd /home/web/html/ && ' |
| 51 | + 'mkdir {} && ' |
| 52 | + 'cd {} && ' |
| 53 | + 'wget https://github.com/kejilion/Website_source_code/raw/main/Discuz_X3.5_SC_UTF8_20230520.zip && ' |
| 54 | + 'unzip Discuz_X3.5_SC_UTF8_20230520.zip && ' |
| 55 | + 'rm Discuz_X3.5_SC_UTF8_20230520.zip' |
| 56 | + ).format(domain, domain) |
| 57 | + |
| 58 | + command = f"{command} > /dev/null 2>&1" |
| 59 | + |
| 60 | + stdin, stdout, stderr = client.exec_command(command) |
| 61 | + |
| 62 | + stdout.channel.recv_exit_status() |
| 63 | + |
| 64 | + stdin, stdout, stderr = client.exec_command('docker exec nginx chmod -R 777 /var/www/html && docker exec php chmod -R 777 /var/www/html && docker exec php74 chmod -R 777 /var/www/html') |
| 65 | + stdout.channel.recv_exit_status() # 等待命令执行完成 |
| 66 | + |
| 67 | + |
| 68 | + command = "docker exec mysql mysql -u root -p'{}' -e 'CREATE DATABASE {}; GRANT ALL PRIVILEGES ON {}.* TO \"{}\"@\"%\";'".format(dbrootpasswd,dbname,dbname,dbuse) |
| 69 | + stdin, stdout, stderr = client.exec_command(command) |
| 70 | + stdout.channel.recv_exit_status() # 等待命令执行完成 |
| 71 | + |
| 72 | + |
| 73 | + stdin, stdout, stderr = client.exec_command("docker restart php && docker restart php74 && docker restart nginx ") |
| 74 | + stdout.channel.recv_exit_status() # 等待命令执行完成 |
| 75 | + |
| 76 | + |
| 77 | + print(f"您的discuz论坛搭建好啦!\nhttps://{domain}") |
| 78 | + print() |
| 79 | + |
| 80 | + # 关闭 SSH 连接 |
| 81 | + client.close() |
| 82 | + |
| 83 | + |
| 84 | + except Exception as e: |
| 85 | + print(f"连接 {name} 失败") |
| 86 | + |
| 87 | + |
| 88 | +# 遍历服务器列表,逐一更新 |
| 89 | +for server in servers: |
| 90 | + name = server["name"] |
| 91 | + hostname = server["hostname"] |
| 92 | + port = server["port"] |
| 93 | + username = server["username"] |
| 94 | + password = server["password"] |
| 95 | + domain = server["domain"] |
| 96 | + dbname = server["dbname"] |
| 97 | + dbrootpasswd = server["dbrootpasswd"] |
| 98 | + dbuse = server["dbuse"] |
| 99 | + update_server(name, hostname, port, username, password, domain, dbrootpasswd, dbuse, dbname ) |
| 100 | + |
| 101 | +# 等待用户按下任意键后关闭窗口 |
| 102 | +input("按任意键关闭窗口...") |
| 103 | + |
| 104 | + |
0 commit comments