RouteOS之利用DNSPOD的API接口实现DDNS解析

  • A+
所属分类:Linux

DNSPOD的API申请地址:https://support.dnspod.cn/Kb/showarticle/tsid/227/

完整的请求地址为:https://www.dwhd.org/dnspod_v2.php?code=(API ID)&token=(API Token)&domain_id=(域名ID)&record_id=(解析ID)&sub_domain[域名解解析名]&record_type=[域名解析类型]&record_line=[域名解析线路]&value=[域名解析地址]

强制赋值变量为:

code,token,domain_id,record_id

可选变量为:

sub_domain,record_type,record_line,value

当变量sub_domain为空时,PHP自动赋值为:『routeos』

当变量record_type为空时,php自动赋值为:『A』

当变量record_line为空时,PHP自动赋值为:『默认』

当变量value为空时,php自动获取当前请求的IP为其值

<?php

$realip      = getIP();
$code        = $_GET['code'];
$token       = $_GET['token'];
$domain_id   = $_GET['domain_id'];
$record_id   = $_GET['record_id'];
$sub_domain  = $_GET['sub_domain'];
$value       = $_GET['value'];
$record_type = $_GET['record_type'];
$record_line = $_GET['record_line'];

if (empty($code))  { exit('code empty.'); }
if (empty($token)) { exit('token empty.'); }
if (empty($domain_id)) { exit('domain_id empty.'); }
if (empty($record_id)) { exit('record_id empty.'); }
if (empty($sub_domain)) { $sub_domain = "routeos"; }
if (empty($record_type)) { $record_type = "A"; }
if (empty($record_line)) { $record_line = "默认"; }
if (empty($value)) { $value = $realip; }
$ip = checkIP($code, $token, $domain_id, $record_id);
if (empty($ip)) { exit('ip empty.'); }
if ($ip == $value) { exit("IP一致."); }

$ch            = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://dnsapi.cn/Record.Modify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "login_token=" . $code . "," . $token . "&format=json&domain_id=" . $domain_id . "&record_id=" . $record_id . "&sub_domain=" . $sub_domain . "&value=" . $value . "&record_type=" . $record_type . "&record_line=" . "$record_line");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
exit('ok');

function checkIP($code, $token, $domain_id, $record_id ) {
    $ch            = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://dnsapi.cn/Record.Info");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "login_token=" . $code . "," . $token . "&format=json&domain_id=" . $domain_id . "&record_id=" . $record_id . "");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec($ch);
    curl_close($ch);
    $json          = json_decode($server_output, TRUE);
    return $json['record']['value'];
}

function getIP(){
    if (@$_SERVER["HTTP_X_FORWARDED_FOR"])
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    else if (@$_SERVER["HTTP_CLIENT_IP"])
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    else if (@$_SERVER["REMOTE_ADDR"])
        $ip = $_SERVER["REMOTE_ADDR"];
    else if (@getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (@getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if (@getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else
        $ip = "Unknown";
    return $ip;
}

RouteOS 6.x

:global id "200000"
:global token "*****************25712a6430c5f2"
:global domainid "830000"
:global recordid "295000000"
:global subdomain "routeos"
:global xx "https://www.dwhd.org/dnspod_2.php?code=$id&token=$token&domain_id=$domainid&record_id=$recordid"

/tool fetch url=("$xx") mode=https keep-result=yes dst-path=ddns.txt

RouteOS 5.x

:global id "200000"
:global token "*****************25712a6430c5f2"
:global domainid "830000"
:global recordid "295000000"
:global subdomain "routeos"
:global xx "http://www.dwhd.org/dnspod_2.php?code=$id&token=$token&domain_id=$domainid&record_id=$recordid"

/tool fetch url=("$xx") mode=http keep-result=yes dst-path=ddns.txt
lookback

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: