初始化1
This commit is contained in:
106
scripts/clean_mokee.py
Normal file
106
scripts/clean_mokee.py
Normal file
@@ -0,0 +1,106 @@
|
||||
"""
|
||||
清理 10.1.1.160 上所有 mokee.com 证书+Nginx配置+数据库记录
|
||||
用法: python3 clean_mokee.py
|
||||
"""
|
||||
import paramiko
|
||||
import pymysql
|
||||
import sys
|
||||
|
||||
HOST = '10.1.1.160'
|
||||
USER = 'root'
|
||||
PASS = 'mokee.'
|
||||
|
||||
DB_HOST = '10.1.1.122'
|
||||
DB_USER = 'nginxserver'
|
||||
DB_PASS = 'mokee.'
|
||||
DB_NAME = 'nginxserver'
|
||||
|
||||
|
||||
def ssh_cmd(ssh, cmd, desc=''):
|
||||
"""执行远程命令并打印结果"""
|
||||
print(f"\n{'='*60}")
|
||||
print(f">>> {desc or cmd}")
|
||||
print('='*60)
|
||||
stdin, stdout, stderr = ssh.exec_command(cmd)
|
||||
out = stdout.read().decode().strip()
|
||||
err = stderr.read().decode().strip()
|
||||
if out:
|
||||
print(out)
|
||||
if err:
|
||||
print('[stderr]', err)
|
||||
return out
|
||||
|
||||
|
||||
def main():
|
||||
step = sys.argv[1] if len(sys.argv) > 1 else ''
|
||||
|
||||
if step == '':
|
||||
# 第一步:查看
|
||||
print('>>> 第一步:查看 mokee.com 相关文件 <<<')
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect(HOST, username=USER, password=PASS, timeout=10)
|
||||
|
||||
ssh_cmd(ssh, 'ls -la /etc/nginx/ssl/*mokee* 2>/dev/null', 'mokee.com 证书文件')
|
||||
ssh_cmd(ssh, 'ls -la /etc/nginx/conf.d/*mokee* 2>/dev/null', 'mokee.com Nginx配置')
|
||||
|
||||
ssh.close()
|
||||
|
||||
print('\n>>> 确认无误后执行: python3 clean_mokee.py delete')
|
||||
|
||||
elif step == 'delete':
|
||||
# 第二步:清理
|
||||
print('>>> 第二步:清理 mokee.com 证书+Nginx配置+数据库 <<<')
|
||||
|
||||
# --- 服务器端 ---
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect(HOST, username=USER, password=PASS, timeout=10)
|
||||
|
||||
# 删证书
|
||||
ssh_cmd(ssh, 'rm -fv /etc/nginx/ssl/*mokee*.pem /etc/nginx/ssl/*mokee*.key /etc/nginx/ssl/*mokee*.crt 2>&1', '删除证书文件')
|
||||
|
||||
# 删Nginx配置
|
||||
ssh_cmd(ssh, 'rm -fv /etc/nginx/conf.d/*mokee*.conf 2>&1', '删除Nginx配置')
|
||||
|
||||
# 语法检查+重载
|
||||
ssh_cmd(ssh, 'nginx -t 2>&1 && nginx -s reload 2>&1', 'Nginx语法检查+重载')
|
||||
|
||||
ssh.close()
|
||||
print('\n>>> 服务器端清理完成')
|
||||
|
||||
# --- 数据库端 ---
|
||||
print('\n>>> 清理数据库记录')
|
||||
conn = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASS,
|
||||
database=DB_NAME, charset='utf8mb4')
|
||||
cur = conn.cursor()
|
||||
|
||||
# 先看
|
||||
cur.execute("SELECT id, domain, cert_type, status FROM certificates WHERE domain LIKE '%mokee.com'")
|
||||
certs = cur.fetchall()
|
||||
print(f'证书记录: {len(certs)} 条')
|
||||
for r in certs:
|
||||
print(f' {r}')
|
||||
|
||||
cur.execute("SELECT id, domain, protocol, target FROM forwarding_rules WHERE domain LIKE '%mokee.com'")
|
||||
rules = cur.fetchall()
|
||||
print(f'转发规则: {len(rules)} 条')
|
||||
for r in rules:
|
||||
print(f' {r}')
|
||||
|
||||
# 删除
|
||||
cur.execute("DELETE FROM certificates WHERE domain LIKE '%mokee.com'")
|
||||
print(f'已删除证书记录: {cur.rowcount} 条')
|
||||
|
||||
cur.execute("DELETE FROM forwarding_rules WHERE domain LIKE '%mokee.com'")
|
||||
print(f'已删除转发规则: {cur.rowcount} 条')
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
print('\n>>> 全部清理完成!')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user