| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- import time
- import requests
- import smtplib
- from email.mime.text import MIMEText
- # --- CONFIGURATION ---
- CHECK_INTERVAL = 300 # seconds
- NOTIFY_METHOD = "email" # "email", "telegram", or "nextcloud"
- LAST_IP_FILE = "last_ip.txt"
- # Email settings
- EMAIL_FROM = "larry1chan@qq.com"
- EMAIL_TO = "larry1chan@gmail.com"
- SMTP_SERVER = "smtp.qq.com"
- SMTP_PORT = 465
- SMTP_USER = "larry1chan@qq.com"
- SMTP_PASS = "xhybazbbucdyceed"
- # Telegram settings
- TELEGRAM_BOT_TOKEN = "258792305:AAEF1WvtLOPj2DlXecT1fIBMKOm4OaXnMOU"
- TELEGRAM_CHAT_ID = "262500746"
- # Nextcloud Talk settings
- NEXTCLOUD_URL = "https://1984.algometic.com"
- NEXTCLOUD_TOKEN = "your_nextcloud_token"
- NEXTCLOUD_ROOM = "your_room_token"
- def get_public_ip():
- try:
- return requests.get("https://api.ipify.org").text.strip()
- except Exception as e:
- print(f"Error getting public IP: {e}")
- return None
- def send_email(subject, body):
- try:
- msg = MIMEText(body)
- msg["Subject"] = subject
- msg["From"] = EMAIL_FROM
- msg["To"] = EMAIL_TO
- with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server:
- server.set_debuglevel(1)
- #server.starttls()
- server.login(SMTP_USER, SMTP_PASS)
- server.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
- except Exception as e:
- print(f"Error sending email: {e}")
- return False
-
- def send_telegram(message):
- url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
- data = {"chat_id": TELEGRAM_CHAT_ID, "text": message}
- requests.post(url, data=data)
- def send_nextcloud_talk(message):
- url = f"{NEXTCLOUD_URL}/ocs/v2.php/apps/spreed/api/v1/chat/{NEXTCLOUD_ROOM}"
- headers = {
- "OCS-APIRequest": "true",
- "Authorization": f"Bearer {NEXTCLOUD_TOKEN}"
- }
- data = {"message": message}
- requests.post(url, headers=headers, data=data)
- def notify(ip):
- message = f"Public IP changed to: {ip}"
- if NOTIFY_METHOD == "email":
- send_email("IP Address Changed", message)
- elif NOTIFY_METHOD == "telegram":
- send_telegram(message)
- elif NOTIFY_METHOD == "nextcloud":
- send_nextcloud_talk(message)
- else:
- print("Unknown notification method.")
- def load_last_ip():
- try:
- with open(LAST_IP_FILE, "r") as f:
- return f.read().strip()
- except FileNotFoundError:
- return None
- def save_last_ip(ip):
- with open(LAST_IP_FILE, "w") as f:
- f.write(ip)
- def main():
- last_ip = load_last_ip()
- while True:
- ip = get_public_ip()
- if ip and ip != last_ip:
- notify(ip)
- save_last_ip(ip)
- last_ip = ip
- time.sleep(CHECK_INTERVAL)
- if __name__ == "__main__":
- main()
|