家里面有公网IP可以把机顶盒拿来做24小时运行的服务器。
公网IP会时不时的发生改变,也就是动态公网IP。
所以买个阿里云的域名绑定公网IP,当服务器检测到IP变化后自动更新域名解析。
目前域名已经是配置完解析,可以通过域名访问服务器的状态了,实现过程不多赘述。
ps: 将设备暴露在公网下是很危险的行为,请务必设置复杂的用户密码以防暴力破解
获取阿里云账号的AccessKey ID,AccessKey Secret
让服务器自己打开浏览器登录阿里云平台修改域名解析不大现实。
这里使用阿里云提供的SDK,配合用户的AccessKey ID,AccessKey Secret进行更新域名解析。
将AccessKey ID,AccessKey Secret写入到服务器的环境变量当中
root@hi3798mv100:~# vi ~/.bashrc
root@hi3798mv100:~# source ~/.bashrc
添加内容
export ALIBABA_CLOUD_ACCESS_KEY_ID=LTAI5tN9xxxxxxxxxxxxx
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=zpZDgxxxxxxxxxxxx
安装SDK
- ubuntu20.04桌面版有完备的根文件系统,按照官方说明安装就可以了,这里指定用阿里源会比较好
pip install alibabacloud_alidns20150109==3.5.7 -i https://mirrors.aliyun.com/pypi/simple/
- 本次使用的服务器是机顶盒,用的是ubuntu20.04-base根文件系统,缺少了一些依赖所以比较麻烦
root@hi3798mv100:~# apt update
root@hi3798mv100:~# apt install python3-pip
root@hi3798mv100:~# apt install pkg-config
root@hi3798mv100:~# apt install build-essential libssl-dev libffi-dev python-dev
安装SDK时需要安装cryptography,而cryptography又依赖rust,还需要手动安装rust
root@hi3798mv100:~# apt install curl
root@hi3798mv100:~# export RUSTUP_DIST_SERVER=https://rsproxy.cn #提高下载速度
root@hi3798mv100:~# export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup #提高下载速度
root@hi3798mv100:~# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh #下载完成后默认安装
root@hi3798mv100:~# . "$HOME/.cargo/env" #更新环境变量
root@hi3798mv100:~# rust --version
root@hi3798mv100:~# pip install alibabacloud_alidns20150109==3.5.7 -i https://mirrors.aliyun.com/pypi/simple/
使用SDK更新域名解析记录
获取域名解析记录的ID
查看官方的获取域名记录示例代码
输入购买的阿里云的域名后在右侧会生成相应的python代码,将代码拷贝到服务器后稍作修改再执行
下面是拷贝下来的部分代码
runtime = util_models.RuntimeOptions()
try:
# Copy the code to run, please print the return value of the API by yourself.
client.describe_domain_records_with_options(describe_domain_records_request, runtime)
except Exception as error:
# Only a printing example. Please be careful about exception handling and do not ignore exceptions directly in engineering projects.
# print error message
下面修改后的代码,把请求到的结果打印出来
runtime = util_models.RuntimeOptions()
try:
# Copy the code to run, please print the return value of the API by yourself.
result = client.describe_domain_records_with_options(describe_domain_records_request, runtime)
print(result)
except Exception as error:
# Only a printing example. Please be careful about exception handling and do not ignore exceptions directly in engineering projects.
# print error message
然后执行python脚本,会输出一个json字符串,找到类似下面这样的字段,记录这个值,这个要填入下一步操作中
'RecordId': '1888489735240922112'
更新域名解析记录
查看官方的修改域名解析记录的示例代码
这里面我把绑定的IP地址设置为101.101.101.101
将代码拷贝到服务器直接执行,如果没有任何输出信息则完成修改域名解析记录。
验证下结果,域名绑定的IP的确被修改成了101.101.101.101。有时候不会立即更新,需要等待几分钟。
自动更新域名解析记录
- 更改域名解析的python脚本,将之前写的固定值101.101.101.101改成args[0],这样就可以在调用python脚本的时候把要绑定的IP地址作为参数传入到python脚本中
ps:这个python脚本文件名我设置为updateIP.py
def main(
args: List[str],
) -> None:
client = Sample.create_client()
update_domain_record_request = alidns_20150109_models.UpdateDomainRecordRequest(
record_id='1888489735240922112',
rr='www',
type='A',
value=args[0]
)
runtime = util_models.RuntimeOptions()
- 创建shell脚本检测ip地址变化
root@hi3798mv100:~/updataIP# vi updateIP.sh
#!/bin/bash
# File path to store the last IP address
ip_file="/root/updateIP/last_ip.txt"
# Use curl to fetch IP information from cip.cc
ip_info=$(curl -s cip.cc)
# Extract the IP address using grep and awk
ip_address=$(echo "$ip_info" | grep "IP" | awk '{print $3}')
# Check if the file exists; if not, create it and write the current IP
if [ ! -f "$ip_file" ]; then
echo "$ip_address" > "$ip_file"
fi
# Read the last recorded IP address
last_ip=$(cat "$ip_file")
# Compare the current IP with the last recorded IP
if [ "$ip_address" != "$last_ip" ]; then
# If the IP address has changed, send an email and update the recorded IP
echo "Hi3798mv100 ip address changed to $ip_address"
echo "$ip_address" > "$ip_file"
echo "IP Address Changed: $ip_address $(date)"
python3 /root/updateIP/updateIP.py "$ip_address"
else
# If the IP address remains unchanged, do not send an email
echo "IP Address Unchanged: $ip_address $(date)"
fi
- 定期执行shell脚本
root@hi3798mv100:~/updateIP# apt install cron
root@hi3798mv100:~/updateIP# crontab -e
编辑定时任务,这里每10分钟检测一次ip地址变化,保存退出即可
# m h dom mon dow command
*/10 * * * * . /root/.bashrc && /bin/bash /root/updateIP/updateIP.sh >> /root/updateIP/updateIP.log 2>&1
ps:使用cron定时执行任务,需要手动加载环境变量,并且执行任务时出现的所有文件都需要使用绝对路径