Hello everyone, dynamic domain name resolution should be familiar to you. Many routers even integrate DDNS dynamic domain name resolution services, but there is no resolution service for Huawei domains, so we can only use scripts to call the API for Huawei's domain resolution.
Here I recommend a software for managing routers, WinSCP tool which is available on Baidu Cloud.
With this software, many operations can be done without coding, and it's very convenient to log in using the SCP protocol.
1. Preparation Work
Before starting, we need to obtain some necessary parameters. First, log in to your cloud console and create an IAM account. It seems that the administrator account doesn't work; I kept getting errors while debugging in the unified identity authentication service.
After creating it, open the console to modify the domain resolution interface. For multiple domains, you need to open one domain at a time to obtain the zone_id for that domain. Each domain has its own zone_id, as shown in the figure below.
Extract the domain zone_id for later use.
2. Obtain recordset_id
Since there are many resolution records under each domain, each resolution record has a recordset_id. We need to open Huawei's API debugging tool to check the ID of each resolution record under the domain. The API debugging address requires you to log in to your Huawei account first. The interface is as follows; fill in the zone_id you just obtained.
Enter the zone_id and click debug. In the right interface, select the recordset_id for the domain you need to resolve, which is the one at the top. Be careful not to select the wrong one. I have set it for IPv6 resolution, which is AAAA.
3. The data you need to prepare is approximately as follows:
adminname='hw123456789' Administrator account
username='123456' Newly created account
password='123456' Password for the newly created account
zoneid='ff80808287asdadadasd7d1b56243'
recordsetid='ff80808287asdasdad3b1d031404c4'
domain='jxyblog.top' The domain name that needs to be modified, I directly use the root domain.
4. First, perform router port IP address acquisition debugging
Otherwise, the script will run into errors later.
SSH into the router interface and run the following command to see if you can normally obtain the WAN port's IPv6 address. I am using PPPoE dialing.
ifconfig pppoe-pppoe | grep inet6 | awk 'NR==1{print $3}' | cut -d'/' -f1
If normal, the output should look like the image below.
If not, first enter ifconfig
to check all ports. Mine is pppoe-pppoe, then filter to only display lines with inet6, NR==1 prints the first line, print $3 prints the third column. Adjust it slowly until you debug out the individual IPv6 address.
5. Software to be installed on the router, open the router software package interface and install the following two software
curl
Debugging tool
bash
Script tool
After preparing the above steps, you should be able to run the script.
6. Below is the script code
#!/bin/bash
adminname='hw123456789' # This part has been mentioned above, just fill it in normally
username='123456'
password='123456'
zoneid='ff8080828732b7946445465b56243'
recordsetid='ff80808654665156173b1d031404c4'
domain='jxyblog.top'
token=$(curl -L -k -s -D - -X POST 'https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens' \
--header 'Content-Type: application/json;charset=UTF-8' \
--data-raw '{"auth":{"identity":{"methods":["password"],"password":{"user":{"domain":{"name":"'$adminname'"},"name":"'$username'","password":"'$password'"}}},"scope":{"domain":{"name":"'$adminname'"}}}}' | grep X-Subject-Token | awk '{print $2}')
wanip=$(ifconfig pppoe-pppoe | grep inet6 | awk 'NR==1{print $3}' | cut -d'/' -f1) # This was debugged above, if there are changes, just modify it.
curl -i --location --request PUT 'https://dns.myhuaweicloud.com/v2.1/zones/'$zoneid'/recordsets/'$recordsetid'' \
--header 'Content-Type: application/json;charset=UTF-8' \
--header 'X-Auth-Token: '$token'' --data-raw '{"name":"'$domain'.","type":"AAAA","ttl":300,"records":["'$wanip'"]}'
Create a file with a .sh suffix and save it to the router, for example, I saved it to /mnt, run the following code
/bin/bash /tmp/ddns.sh
If the return is normal, it should look like the image below.
At this point, the resolution record on the server has been updated.
7. Set up scheduled updates for resolution records or regularly check for IP changes to update resolution records.
For example, set the router to run the script every 12 hours, as shown in the image below.
0 */12 * * * /bin/bash /mnt/ddns1.sh
If you set it to check for IP changes every 10 minutes for domain name resolution, I previously posted a script for checking IP changes. The script is as follows; for details, you can refer to the previous article link:
【Detailed Tutorial】OpenWrt Public IP Address Change Automatic Email Notification (IPv4, IPv6 Supported) 【Reprinted and Reorganized】
#!/bin/bash
current_ipv6=$(ip -6 addr show br-lan | grep -oE 'inet6 [0-9a-fA-F:]+(/[0-9]+)?' | awk '{print $2}')
if [ ! -f /tmp/wan_ip.txt ]; then
echo "$current_ipv6" > /tmp/wan_ip.txt
echo -e "Subject: OpenWRT WAN IP\n\nFirst run of the script, WAN IPv6 address is: $current_ipv6" | msmtp -a default [email protected]
fi
saved_ip=$(cat /tmp/wan_ip.txt)
if [ "$current_ipv6" != "$saved_ip" ]; then
echo -e "Subject: OpenWRT WAN IP Changed\n\nWAN IPv6 has changed to: $current_ipv6\nPrevious IP address was: $saved_ip" | msmtp -a default [email protected]
echo "$current_ipv6" > /tmp/wan_ip.txt
/bin/bash /mnt/ddns1.sh # Here directly insert the domain resolution script, IP changes directly ddns
fi
That's it. I really hope this helps you.