jxyblog

jxyblog

email

OpenWrt router automatically updates WAN domain resolution for Huawei domain API through scripts.

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.

1111
After creating it, open the domain resolution modification interface in the console. For multiple domains, you need to open one domain at a time to obtain the zone_id for that domain. Each domain has a separate zone_id, as shown in the image below.
Extract the domain zone_id for later use.

22222

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 address for API debugging requires you to log in to your Huawei account first. The interface is as follows; fill in the zone_id you just obtained.

333
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.

44444
3. The data that needs to be prepared is roughly 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' Domain name to modify the resolution, I directly use the root domain.
4. First, perform router port IP address acquisition debugging
Otherwise, the script will throw an error when running later.
SSH into the router interface and run the following commands to see if you can normally obtain the WAN port's IPv6 address; mine is using PPPoE dialing.
ifconfig pppoe-pppoe | grep 'inet6' | grep 'Globa' | awk '{print $3}' | cut -d'/' -f1
ip -6 addr show pppoe-pppoe | grep 'inet6' | grep 'dynamic'| grep -oE 'inet6 [0-9a-fA-F:]+(/[0-9]+)?' | awk '{print $2}' | cut -d'/' -f1
Both of the above can obtain the WAN IP. The first uses the system's built-in ifconfig command, and the second uses the curl installed above.

If everything is normal, the output should look like the image below, just a single complete IP. There are many ways to print the IP; you can research the grep command yourself.

5555

If not, first enter ifconfig to view 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. You can modify it slowly to 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)    # Here we have previously debugged whether we can obtain the IP normally; if not, just change 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 the .sh suffix, save it to the router, for example, I saved it to /mnt, and run the following code
/bin/bash /tmp/ddns.sh
If it returns normally, it should look like the image below.

666
At this point, the resolution record on the server has been updated.

7. Set up scheduled updates for the resolution record or regularly check for IP changes to update the resolution record.
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

777

If you set it to check for IP changes every 10 minutes for domain resolution, I previously posted a script for detecting 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) 【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; if the IP changes, directly ddns.
fi

That's all. If this helps you, that would be great!

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.