test_conn.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. """验证 MySQL / Redis 连通性与密码候选值"""
  3. import socket
  4. import pymysql
  5. CANDIDATES = ["@//qinys12346..", "@ @//qinys12346..", "@ @//qinys12346../", "@//qinys12346../", "@ @//qinys12346../"]
  6. print("=== MySQL 192.168.1.177:3306 ===")
  7. for pwd in CANDIDATES:
  8. try:
  9. conn = pymysql.connect(host="192.168.1.177", port=3306, user="root",
  10. password=pwd, database="shop", connect_timeout=5)
  11. with conn.cursor() as c:
  12. c.execute("SELECT VERSION()")
  13. ver = c.fetchone()[0]
  14. c.execute("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='shop'")
  15. tables = c.fetchone()[0]
  16. conn.close()
  17. print(f"[OK] 密码={pwd!r} MySQL {ver}, shop 库表数量={tables}")
  18. break
  19. except Exception as e:
  20. print(f"[FAIL] 密码={pwd!r} -> {e}")
  21. print("=== Redis 192.168.1.177:6379 ===")
  22. def redis_test(pwd):
  23. s = socket.create_connection(("192.168.1.177", 6379), timeout=5)
  24. def cmd(*args):
  25. line = f"*{len(args)}\r\n".encode()
  26. for a in args:
  27. b = a.encode()
  28. line += f"${len(b)}\r\n".encode() + b + b"\r\n"
  29. s.sendall(line)
  30. return s.recv(200)
  31. if pwd:
  32. r = cmd("AUTH", pwd)
  33. if not r.startswith(b"+OK"):
  34. s.close()
  35. return f"AUTH失败: {r!r}"
  36. r = cmd("PING")
  37. s.close()
  38. return r
  39. for pwd in CANDIDATES:
  40. res = redis_test(pwd)
  41. if b"PONG" in str(res).encode() or res == b"+PONG\r\n":
  42. print(f"[OK] 密码={pwd!r} PING->PONG")
  43. break
  44. print(f"[FAIL] 密码={pwd!r} -> {res!r}")