0x00 获取公网IP
-
背景
- 台式机电源插在小米插座上
- 台式机设置上电自启动
- 通过远程控制小米插座的上电启动台式机
- 通过DDNS的域名远程连接台式机
-
问题
有时候DDNS不稳定(一直使用的花生壳),死活连不上。 -
解决
希望台式机启动后自动上报路由器的公网IP到手机上的微信,然后通过此IP连接台式机。
0x01 Python
用Python编写脚本实现上述逻辑:
-
查询ASUS路由器的公网IP
方案有很多:- 通过公共服务查询,如:
https://api.ipify.org/
- 通过命令查询,如:
traceroute -m 5 baidu.com
- 登录路由器直接获取
第一种方便,但是免费的接口调用有限制,太频繁会被封。第二种有的时候只能获取网关地址,获取不到路由器的IP。这里选择方案三。通过chrome页面上右击选择
inspect
>Network
查看登录的请求。模拟这个请求即可(Copy as curl
很好用)。 - 通过公共服务查询,如:
-
将查询到的公网IP发到QQ邮箱
这个使用SMTP即可,具体步骤可以参考这里。需要注意的是QQ邮箱是用授权码登录的,不是QQ密码。 -
微信接收QQ邮箱的新邮件提醒
打开微信:我
>设置
>通用
>辅助功能
>QQ邮箱提醒
打开即可。
代码如下:
#!/usr/bin python3
# coding:utf-8
import re
import time
import requests
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
global_sleep = 60
global_ip = None
def get_wan_ip():
login_url = 'http://router.asus.com/login.cgi'
main_url = 'http://router.asus.com/index.asp'
logout_url = 'http://router.asus.com/Logout.asp'
headers = {
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Origin': 'http://router.asus.com',
'Referer': 'http://router.asus.com/Main_Login.asp'
}
body = {
'action_wait': 5,
'current_page': 'Main_Login.asp',
'next_page': 'index.asp',
'login_authorization': 'your authorization key get from chrome'
}
wan_ip = None
try:
login_resp = requests.post(login_url, data=body, headers=headers)
cookie = login_resp.cookies
main_resp = requests.get(url=main_url, cookies=cookie)
main_text = main_resp.text
for line in main_text.splitlines():
if 'function wanlink_ipaddr' in line:
ips = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
if len(ips):
wan_ip = ips[0]
break
requests.get(url=logout_url, cookies=cookie)
except Exception as e:
print(e)
return wan_ip
def send_mail(ip):
print("Send mail: " + ip)
mail_addr = 'your_qq@qq.com'
mail_pwd = 'your auth key'
try:
context = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
msg = MIMEText(context, 'plain', 'UTF-8')
msg['From'] = formataddr(["WAN IP", mail_addr])
msg['To'] = formataddr(["ttdevs", mail_addr])
msg['Subject'] = ip
server = smtplib.SMTP_SSL("smtp.qq.com", 465)
server.login(mail_addr, mail_pwd)
server.sendmail(mail_addr, [mail_addr], msg.as_string())
server.quit()
print("Send mail success.")
except smtplib.SMTPException as e:
print(e)
if __name__ == '__main__':
while True:
ip = get_wan_ip()
print("New ip: %s, old ip: %s" % (ip, global_ip))
if ip is not None:
if global_ip is None or ip != global_ip:
send_mail(ip)
global_ip = ip
time.sleep(global_sleep)
0x02 Automator
- 创建
- Open
Automator
- New Document
- Application
- Run Shell Script
- Input:
$(which python3) /Users/ttdevs/Desktop/WAN_IP/wan_ip.py
- Save:
/Users/ttdevs/Desktop/WAN_IP/WAN_IP.app
- Open
- 配置权限
System Preference
>Privacy
>Full Disk Access
> CheckedWanIP.app
若不配置,执行时会报权限错误。
0x03 Configuration
-
创建 /Library/LaunchDaemons/com.ttdevs.WanIP.plist
➜ ~ cd /Library/LaunchDaemons ➜ LaunchDaemons sudo touch com.ttdevs.WanIP.plist Password: ➜ LaunchDaemons ls -l com.ttdevs.WanIP.plist -rw-r--r-- 1 root wheel 0 Jun 18 11:08 com.ttdevs.WanIP.plist ➜ LaunchDaemons
若所有者非root则需切换:
sudo chown root /Library/LaunchDaemons/com.ttdevs.WanIP.plist
-
配置 /Library/LaunchDaemons/com.ttdevs.WanIP.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.ttdevs.WanIP</string> <key>Program</key> <string>/Users/ttdevs/Desktop/WanIP/WanIP.app/Contents/MacOS/Automator Application Stub</string> <key>ProgramArguments</key> <array/> <key>KeepAlive</key> <true/> <key>RunAtLoad</key> <true/> </dict> </plist>
Program中需填绝对路径
-
加入自动项目
sudo launchctl load -w /Library/LaunchDaemons/com.ttdevs.WanIP.plist
-
取消自启动项
sudo launchctl unload -w /Library/LaunchDaemons/com.ttdevs.WanIP.plist
网友评论