DDNS 腾讯云解决方案-基于linux shell的动态域名解析
前提条件
- 已经在qcloud上注册了域名,并随意定义了域名解析地址;
- 拥有云API秘钥,SecretId、SecretKey;
- 拥有可外网访问的IP地址;
- Linux可以访问外网。
获取解析记录
#!/bin/bash
domain='你的域名,例如qq.com'
subDomain='你希望解析的子域名,例如wx.qq.com,则只输入wx'
sId='你的云API秘钥SecretId'
sKey='你的云API秘钥SecretKey'
signatureMethod='HmacSHA1'
timestamp=`date +%s`
nonce=`head -200 /dev/urandom | cksum | cut -f2 -d" "`
region=bj
url="https://cns.api.qcloud.com/v2/index.php"
action='RecordList'
src=`printf "GETcns.api.qcloud.com/v2/index.php?Action=%s&Nonce=%s&Region=%s&SecretId=%s&SignatureMethod=%s&Timestamp=%s&domain=%s" $action $nonce $region $sId $signatureMethod $timestamp $domain`
signature=`echo -n $src|openssl dgst -sha1 -hmac $sKey -binary |base64`
params=`printf "Action=%s&domain=%s&Nonce=%s&Region=%s&SecretId=%s&Signature=%s&SignatureMethod=%s&Timestamp=%s" $action $domain $nonce $region $sId "$signature" $signatureMethod $timestamp `
curl -G -d "$params" --data-urlencode "Signature=$signature" "$url"
修改解析记录
#!/bin/bash
recordId='根据上面返回结果,手工填入,好像是几位数字'
domain='你的域名,例如qq.com'
subDomain='你希望解析的子域名,例如wx.qq.com,则只输入wx'
sId='你的云API秘钥SecretId'
sKey='你的云API秘钥SecretKey'
signatureMethod='HmacSHA1'
timestamp=`date +%s`
nonce=`head -200 /dev/urandom | cksum | cut -f2 -d" "`
region=bj
url="https://cns.api.qcloud.com/v2/index.php"
ip=`curl http://2019.ip138.com/ic.asp >/dev/null|iconv -fgb2312 -t utf-8 |sed 's/.*\[\(.*\)\].*/\1/g'`
action='RecordModify'
recordType='A'
recordLine='默认'
value=$ip
timestamp=`date +%s`
nonce=`head -200 /dev/urandom | cksum | cut -f2 -d" "`
src=`printf "GETcns.api.qcloud.com/v2/index.php?Action=%s&Nonce=%s&Region=%s&SecretId=%s&SignatureMethod=%s&Timestamp=%s&domain=%s&recordId=%s&recordLine=%s&recordType=%s&subDomain=%s&value=%s" $action $nonce $region $sId $signatureMethod $timestamp $domain $recordId $recordLine $recordType $subDomain $value`
signature=`echo -n $src|openssl dgst -sha1 -hmac $sKey -binary |base64`
params=`printf "Action=%s&Nonce=%s&Region=%s&SecretId=%s&SignatureMethod=%s&Timestamp=%s&domain=%s&recordId=%s&recordLine=%s&recordType=%s&subDomain=%s&value=%s" $action $nonce $region $sId $signatureMethod $timestamp $domain $recordId $recordLine $recordType $subDomain $value`
curl -G -d "$params" --data-urlencode "Signature=$signature" "$url"
定时更新任务
*/10 * * * * ddns >/dev/null 2>&