dynip.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import time
  2. import requests
  3. import smtplib
  4. from email.mime.text import MIMEText
  5. # --- CONFIGURATION ---
  6. CHECK_INTERVAL = 300 # seconds
  7. NOTIFY_METHOD = "email" # "email", "telegram", or "nextcloud"
  8. LAST_IP_FILE = "last_ip.txt"
  9. # Email settings
  10. EMAIL_FROM = "larry1chan@qq.com"
  11. EMAIL_TO = "larry1chan@gmail.com"
  12. SMTP_SERVER = "smtp.qq.com"
  13. SMTP_PORT = 465
  14. SMTP_USER = "larry1chan@qq.com"
  15. SMTP_PASS = "xhybazbbucdyceed"
  16. # Telegram settings
  17. TELEGRAM_BOT_TOKEN = "258792305:AAEF1WvtLOPj2DlXecT1fIBMKOm4OaXnMOU"
  18. TELEGRAM_CHAT_ID = "262500746"
  19. # Nextcloud Talk settings
  20. NEXTCLOUD_URL = "https://1984.algometic.com"
  21. NEXTCLOUD_TOKEN = "your_nextcloud_token"
  22. NEXTCLOUD_ROOM = "your_room_token"
  23. def get_public_ip():
  24. try:
  25. return requests.get("https://api.ipify.org").text.strip()
  26. except Exception as e:
  27. print(f"Error getting public IP: {e}")
  28. return None
  29. def send_email(subject, body):
  30. try:
  31. msg = MIMEText(body)
  32. msg["Subject"] = subject
  33. msg["From"] = EMAIL_FROM
  34. msg["To"] = EMAIL_TO
  35. with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server:
  36. server.set_debuglevel(1)
  37. #server.starttls()
  38. server.login(SMTP_USER, SMTP_PASS)
  39. server.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
  40. except Exception as e:
  41. print(f"Error sending email: {e}")
  42. return False
  43. def send_telegram(message):
  44. url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
  45. data = {"chat_id": TELEGRAM_CHAT_ID, "text": message}
  46. requests.post(url, data=data)
  47. def send_nextcloud_talk(message):
  48. url = f"{NEXTCLOUD_URL}/ocs/v2.php/apps/spreed/api/v1/chat/{NEXTCLOUD_ROOM}"
  49. headers = {
  50. "OCS-APIRequest": "true",
  51. "Authorization": f"Bearer {NEXTCLOUD_TOKEN}"
  52. }
  53. data = {"message": message}
  54. requests.post(url, headers=headers, data=data)
  55. def notify(ip):
  56. message = f"Public IP changed to: {ip}"
  57. if NOTIFY_METHOD == "email":
  58. send_email("IP Address Changed", message)
  59. elif NOTIFY_METHOD == "telegram":
  60. send_telegram(message)
  61. elif NOTIFY_METHOD == "nextcloud":
  62. send_nextcloud_talk(message)
  63. else:
  64. print("Unknown notification method.")
  65. def load_last_ip():
  66. try:
  67. with open(LAST_IP_FILE, "r") as f:
  68. return f.read().strip()
  69. except FileNotFoundError:
  70. return None
  71. def save_last_ip(ip):
  72. with open(LAST_IP_FILE, "w") as f:
  73. f.write(ip)
  74. def main():
  75. last_ip = load_last_ip()
  76. while True:
  77. ip = get_public_ip()
  78. if ip and ip != last_ip:
  79. notify(ip)
  80. save_last_ip(ip)
  81. last_ip = ip
  82. time.sleep(CHECK_INTERVAL)
  83. if __name__ == "__main__":
  84. main()