Cloudflare NoIP Alternative 高级功能:如何添加邮件通知和Webhook

Cloudflare NoIP Alternative 高级功能:如何添加邮件通知和Webhook Cloudflare NoIP Alternative 高级功能如何添加邮件通知和Webhook【免费下载链接】cloudflare-noipfree alternative to paid dynamic DNS services like NoIP.com项目地址: https://gitcode.com/gh_mirrors/cl/cloudflare-noipCloudflare NoIP是一款免费的动态DNS服务替代方案可帮助用户将动态IP地址与域名自动关联无需依赖付费服务如NoIP.com。本文将详细介绍如何为该工具添加邮件通知和Webhook功能让你实时掌握IP地址变更情况。为什么需要邮件通知和Webhook动态DNS服务的核心价值在于IP地址变更时的自动同步但默认情况下Cloudflare NoIP只会在日志中记录变更信息。添加邮件通知和Webhook功能后你可以即时获取IP变更提醒 自动触发其他系统操作如更新防火墙规则构建完整的自动化工作流 ⚙️准备工作在开始前请确保你已成功安装Cloudflare NoIP并完成基础配置拥有一个可发送邮件的SMTP服务器如Gmail、企业邮箱准备好接收Webhook的服务端点如Zapier、自定义API核心配置文件路径主程序main.py记录配置records.json密钥文件~/.cloudflare-noip/keys.json步骤1添加邮件通知功能修改配置文件首先编辑密钥文件添加邮件配置{ api_key: your_cloudflare_api_key, email: your_cloudflare_email, zone_ids: { website_name: zone_id }, smtp: { server: smtp.example.com, port: 587, username: your_emailexample.com, password: your_email_password, recipient: recipientexample.com } }添加邮件发送函数在main.py中添加以下代码import smtplib from email.mime.text import MIMEText def send_email_notification(subject, message): try: smtp_config credentials.get(smtp) if not smtp_config: logger.info(SMTP configuration not found, skipping email notification) return msg MIMEText(message) msg[Subject] subject msg[From] smtp_config[username] msg[To] smtp_config[recipient] with smtplib.SMTP(smtp_config[server], smtp_config[port]) as server: server.starttls() server.login(smtp_config[username], smtp_config[password]) server.send_message(msg) logger.info(Email notification sent successfully) except Exception as e: logger.info(fFailed to send email notification: {e})调用邮件通知在IP变更成功后调用通知函数约156行附近if old_ip ! new_ip: logger.info(IP changed) update_records(new_ip) write_ip_to_file(new_ip) # 添加邮件通知 send_email_notification( Cloudflare NoIP IP Address Updated, fIP address has changed from {old_ip} to {new_ip} )步骤2实现Webhook通知添加Webhook配置继续编辑密钥文件添加Webhook配置{ api_key: your_cloudflare_api_key, email: your_cloudflare_email, zone_ids: { website_name: zone_id }, smtp: { // 邮件配置... }, webhook: { url: https://your-webhook-endpoint.com, secret: optional-webhook-secret } }添加Webhook发送函数在main.py中添加以下代码def send_webhook_notification(ip_change_data): try: webhook_config credentials.get(webhook) if not webhook_config: logger.info(Webhook configuration not found, skipping webhook notification) return headers { Content-Type: application/json, X-Webhook-Secret: webhook_config.get(secret, ) } response requests.post( webhook_config[url], jsonip_change_data, headersheaders ) if response.status_code 200: logger.info(Webhook notification sent successfully) else: logger.info(fWebhook request failed with status code: {response.status_code}) except Exception as e: logger.info(fFailed to send webhook notification: {e})调用Webhook通知同样在IP变更处添加Webhook调用if old_ip ! new_ip: logger.info(IP changed) update_records(new_ip) write_ip_to_file(new_ip) # 邮件通知 send_email_notification( Cloudflare NoIP IP Address Updated, fIP address has changed from {old_ip} to {new_ip} ) # Webhook通知 send_webhook_notification({ old_ip: old_ip, new_ip: new_ip, timestamp: time.strftime(%Y-%m-%d %H:%M:%S) })测试与验证完成上述修改后你可以通过以下方式测试功能手动修改~/.cloudflare-noip/IP.txt中的IP地址运行程序python main.py检查是否收到邮件通知查看Webhook服务是否接收到请求日志文件路径/tmp/cloudflare-noip.log可通过该文件排查问题。总结通过添加邮件通知和Webhook功能Cloudflare NoIP的实用性得到了显著提升。这些功能让你能够实时掌握IP地址变更情况并与其他系统集成构建更强大的自动化工作流。如果你有其他高级功能需求可以参考main.py的代码结构进行扩展比如添加短信通知、Slack消息等。【免费下载链接】cloudflare-noipfree alternative to paid dynamic DNS services like NoIP.com项目地址: https://gitcode.com/gh_mirrors/cl/cloudflare-noip创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考