本文想定时上报orangepi的WLAN网络接口的IP地址,因为orangepi连接的是校园网且通过DHCP服务器分配IP地址,所以IP地址是会变化的,但我又不想每次都打开终端通过以太网连接到orangepi再查看WLAN网络接口的IP地址,因此有了本文的尝试。
linux 系统设置开机自启脚本方式 | Thee
flask 框架视图函数错误 TypeError: The view function did not return a valid response. -CSDN 博客
crontab 可视化分析网站:Crontab 在线生成器 | 菜鸟工具
云服务器配置
香橙派服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| from flask import Flask, request
app = Flask(__name__) API_KEY = "secret_key"
@app.route('/update_ip', methods=['POST']) def update_ip(): data = request.json if data.get('key') != API_KEY: return "Unauthorized", 401 with open('/var/log/latest_orangepi_ip.txt', 'w') as f: f.write(data['ip']+'\n') return "OK", 200
@app.route('/update_ip', methods=['GET']) def get_ip(): """处理 GET 查询IP""" client_key = request.get_json().get('key')
if client_key != API_KEY: return "Unauthorized", 401
return "OK", 200
|
使用 Gunicorn 启动 Flask 应用
- 创建/etc/systemd/system/orangepi_ip_reporter.service 文件
1 2 3 4 5 6 7 8 9 10 11 12
| [Unit] Description=Gunicorn instance for IP Reporter After=network.target
[Service] User=root WorkingDirectory=/home/thee/code_sources ExecStart=/usr/local/bin/gunicorn -w 2 -b 127.0.0.1:5000 orangepi2_ip_server:app Restart=always
[Install] WantedBy=multi-user.target
|
1 2 3
| $ sudo systemctl start orangepi_ip_reporter $ sudo systemctl enable orangepi_ip_reporter $ sudo systemctl daemon-reload
|
nginx 反向代理
- 创建
/etc/nginx/conf.d/orangepi.conf
文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| server { listen 80; server_name example.com; return 301 https://$host$request_uri; }
server { listen 443 ssl; server_name example.com;
ssl_certificate /path/fullchain.pem; ssl_certificate_key /path/privkey.pem;
location /update_ip { proxy_pass http://127.0.0.1:5000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
|
香橙派配置
上传 IP 地址代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| import requests import json import logging from datetime import datetime import socket import fcntl import struct
API_URL = "https://example.com/update_ip" API_KEY = "secret_key" NET_INTERFACE = "wlan0" LOG_FILE = "/var/log/orangepi_report_ip.log"
logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler(LOG_FILE), logging.StreamHandler() ] )
def get_ip_address(interface): """获取指定网络接口的IP地址""" try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ip = socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, struct.pack('256s', interface[:15].encode('utf-8')) )[20:24]) return ip except Exception as e: logging.error(f"获取 {interface} 接口IP失败: {str(e)}") return None
def report_ip(): """上报IP到服务器""" ip_addr = get_ip_address(NET_INTERFACE) if not ip_addr: return False
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
data = { "ip": ip_addr, "timestamp": timestamp, "key": API_KEY }
try: response = requests.post( API_URL, headers={"Content-Type": "application/json"}, data=json.dumps(data), timeout=10 )
if response.status_code == 200: logging.info(f"IP上报成功: {ip_addr}") return True else: logging.error(f"服务器返回错误: {response.status_code} - {response.text}") return False
except requests.exceptions.RequestException as e: logging.error(f"请求服务器失败: {str(e)}") return False except Exception as e: logging.error(f"发生未知错误: {str(e)}") return False
if __name__ == "__main__": success = report_ip()
with open(LOG_FILE, 'a') as f: status = "成功" if success else "失败" log_entry = f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - {NET_INTERFACE} IP上报{status}\n" f.write(log_entry)
|
cron 定时服务
- cron 服务配置,执行
crontab -e
命令后配置定时任务:
1 2
| 0 0 * * * /path/to/orangepi_report_ip.py
|