Files
gfil-blog/add_robots_v2.py

119 lines
3.7 KiB
Python

"""Safely add robots.txt location block to gfil-lab.com Nginx config"""
import paramiko
import os
JD_HOST = "111.228.37.165"
JD_USER = "root"
JD_PASS = "Liudecai110"
LAB_HOST = "216.144.233.14"
LAB_USER = "root"
LAB_PASS = "Kt9V72Tx2c48ChikKU"
# Write the Python fix script to a temp file
FIX_SCRIPT = r'''
import shutil
src = "/etc/nginx/sites-enabled/gfil"
bak = "/etc/nginx/sites-enabled/gfil.bak.before-robots"
shutil.copy2(src, bak)
with open(src, "r") as f:
content = f.read()
robots_block = " location = /robots.txt {\n alias /var/www/gfil-lab/robots.txt;\n default_type text/plain;\n }\n\n"
if "robots.txt" not in content:
content = content.replace(" location / {", robots_block + " location / {")
with open(src, "w") as f:
f.write(content)
print("INSERTED")
else:
print("ALREADY_EXISTS")
'''
# Write script locally
script_path = os.path.join(os.environ.get("TEMP", "/tmp"), "fix_nginx.py")
with open(script_path, "w") as f:
f.write(FIX_SCRIPT)
jd = paramiko.SSHClient()
jd.set_missing_host_key_policy(paramiko.AutoAddPolicy())
jd.connect(JD_HOST, port=22, username=JD_USER, password=JD_PASS,
timeout=20, banner_timeout=60, allow_agent=False, look_for_keys=False)
# Upload fix script to JD Cloud
sftp = jd.open_sftp()
sftp.put(script_path, "/tmp/fix_nginx.py")
sftp.close()
# SCP to gfil-lab server, run, verify
cmd = f"""sshpass -p '{LAB_PASS}' ssh -o StrictHostKeyChecking=no {LAB_USER}@{LAB_HOST} '
# Copy fix script from JD Cloud
echo "Step 1: Upload fix script..."
cat /dev/null # placeholder
# Actually the script is on JD Cloud, need to scp it first
'"""
# Two-step: first scp script to target, then run it
scp_cmd = f"sshpass -p '{LAB_PASS}' scp -o StrictHostKeyChecking=no /tmp/fix_nginx.py {LAB_USER}@{LAB_HOST}:/tmp/fix_nginx.py"
stdin, stdout, stderr = jd.exec_command(scp_cmd, timeout=15)
stdout.channel.recv_exit_status()
print(f"SCP: done")
# Now run it on target
run_cmd = f"""sshpass -p '{LAB_PASS}' ssh -o StrictHostKeyChecking=no {LAB_USER}@{LAB_HOST} '
echo "=== Running fix script ==="
python3 /tmp/fix_nginx.py
echo "=== Updated config ==="
cat /etc/nginx/sites-enabled/gfil
echo "=== Nginx test ==="
nginx -t 2>&1
if [ $? -eq 0 ]; then
systemctl reload nginx
echo "SUCCESS: Nginx reloaded"
else
echo "FAILED: rolling back"
cp /etc/nginx/sites-enabled/gfil.bak.before-robots /etc/nginx/sites-enabled/gfil
nginx -t 2>&1 && systemctl reload nginx
echo "Rolled back"
fi
echo "=== Site check ==="
curl -s -o /dev/null -w "Site: HTTP %{http_code}" http://localhost/ 2>/dev/null
echo ""
echo "=== robots.txt check ==="
curl -s http://localhost/robots.txt 2>/dev/null | head -8
rm -f /tmp/fix_nginx.py
'"""
stdin, stdout, stderr = jd.exec_command(run_cmd, timeout=30)
print(stdout.read().decode())
jd.close()
# External verify
print("\n=== External verification ===")
import urllib.request
for domain in ['gfil-lab.com', 'gfil-intel.xyz']:
try:
req = urllib.request.Request(f'https://{domain}/robots.txt',
headers={'User-Agent': 'Mozilla/5.0'})
r = urllib.request.urlopen(req, timeout=10)
content = r.read().decode()
has_gptbot = 'GPTBot' in content
print(f" {domain}: {r.status} | GPTBot={has_gptbot} | {len(content)} chars")
except Exception as e:
print(f" {domain}: {e}")
# Also verify main site still works
try:
req = urllib.request.Request('https://gfil-lab.com/',
headers={'User-Agent': 'Mozilla/5.0'})
r = urllib.request.urlopen(req, timeout=10)
print(f" gfil-lab.com homepage: {r.status} OK")
except Exception as e:
print(f" gfil-lab.com homepage: ERROR {e}")