OpenWrt 增加 crontab 任務
在/root/crontab/ 目錄下, 創建腳本 ddns.sh
#!/bin/sh # 遠程php腳本的URL地址 SERVICE_URL=http://www.rockbb.com/my_ddns.php # 修改的目標記錄 RECORD_NAME=homeop.rockbb.com # DNS類型, IPv6為AAAA RECORD_TYPE=AAAA # 這里改成自己的密文, 與遠程的php腳本要一致, 用於校驗md5 PRIVATE_KEY=helloworld # 抽取遠程IP, 個人感覺grep正則比 cut -d更好用, 在我的timecloud上, cut -d ' ' 工作貌似不正常 if nslookup $RECORD_NAME|grep Address >/dev/null; then remote_ip=`nslookup $RECORD_NAME|grep -Eo 'Address\s+\d:\s+[0-9a-f:]+'|grep -Eo '[0-9a-f]+(:[0-9a-f:]+)+'` echo "Remote IP: "$remote_ip else exit 1 fi # 抽取本地IPv6地址 if ifconfig br-lan>/dev/null ; then local_ip=`ifconfig br-lan|grep 'inet6 addr:\s24.*'|grep -Eo '24[0-9a-f:]+'` echo "Local IP : "$local_ip else echo "Command: 'ifconfig br-lan' failed" exit 1; fi if [ $remote_ip != $local_ip ]; then # 得到當前的 時分秒, 用於計算MD5 local_ts="$(date +'%H%M%S')" md5=$(echo -n $PRIVATE_KEY$local_ts | md5sum -| cut -d' ' -f1) wget -q -O- "$SERVICE_URL?recordname=$RECORD_NAME&recordtype=$RECORD_TYPE&ts=$local_ts&s=$md5&content=$local_ip" else echo "Same IPs, skip unchanged" exit 0 fi
添加crontab任務
crontab -e # 內容 */10 * * * * /root/crontab/ddns.sh > /tmp/log/ddns.log 2>&1
.啟動cron, 並確認有cron進程
root@Timecloud1:~# ps |grep cron 23791 root 1220 S /usr/sbin/crond -f -c /etc/crontabs -l 5 # 如果不存在則啟動 /etc/init.d/cron start
處理請求的PHP腳本
安全起見, 處理前先校驗md5
<?php
# 代理號
$client = "agent9527";
# 在IP白名單管理處填寫的密碼, 不是代理登錄密碼, 用於計算checksum
$password = 'yourpwd';
# 根域名
$name = 'rockbb.com';
# Private Key
$private_key = 'helloworld';
# 要改DNS解析的域名記錄
$recordname = $_GET['recordname'];
# 要改DNS解析的域名記錄類型
$type= $_GET['recordtype'];
# 新解析值
$content = $_GET['content'];
# TTL
$ttl = 600;
# 編碼, E:英文, G:中文
$enc = 'E';
# 字符集編碼 utf-8 或 gb2312
$charset = 'utf-8';
# 時間, 用於校驗簽名值
$ts = $_GET['ts'];
# 簽名值
$s = $_GET['s'];
print_r($_GET);exit;
# 校驗簽名
$dummy = md5($private_key . $ts);
if ($dummy != $s) {
echo 'Incorrect input: ' . $dummy;
exit;
}
#### 獲取舊值 ####
$method = 'DomainDNSQuery';
$checksum = md5($method.$client.$password.$name);
$api_url = 'http://api.xinnet.com/domain/api.gb?method=' . $method . '&charset=' . $charset;
$parameters = '&enc='.$enc.'&client='.$client.'&checksum='.$checksum.'&name='.$name.'&type='.$type;
#echo $api_url."\n"; echo $parameters."\n";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
$data = curl_exec($curl);
curl_close($curl);
#echo $data."\n";
$data_array = explode('&',$data);
$result = array();
foreach ($data_array as $data_item) {
$data_pair = explode('=', $data_item);
$result[$data_pair[0]] = $data_pair[1];
}
print_r($result);exit;
if ($result['ret'] != '100') {
echo $data;
exit;
}
$found = false;
$original_content = '';
for ($i = 1; $i < 10; $i++) {
// recordnamen 記錄名, contentn 解析內容, ttln 生存時間, prion 優先級, reason 錯誤信息, domain-auth-Failed
if (!array_key_exists('recordname'.$i, $result)) {
echo 'No more results at index:'.$i."\n";
break;
}
if ($result['recordname'.$i] == $recordname) {
echo "Record found at index:$i\n";
$found = true;
$original_content = $result['content' . $i];
if ($original_content == $content) {
echo "Values are the same, exit.\n";
exit;
} else {
break;
}
}
}
if (!$found) {
echo "Record not found, exit\n";
exit;
}
$method = 'DomainDNSMod';
$checksum = md5($method.$client.$password.$name);
echo $checksum . "\n";
$api_url = 'http://api.xinnet.com/domain/api.gb?method=' . $method . '&charset=' . $charset;
$parameters = '&enc=' . $enc
. '&client=' . $client
. '&checksum=' . $checksum
. '&name=' . $name
. '&type=' . $type
. '&recordname=' . $recordname
. '&content=' . $content
. '&oldcontent=' . $original_content
. '&ttl=' . $ttl;
echo $api_url."\n";
echo $parameters."\n";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
$data = curl_exec($curl);
curl_close($curl);
echo $data."\n";
?>
