linux下用dnspod实现动态域名解析(可用于Tomato等路由器)
Posted by he; tagged with DNS , 动态域名 , 动态解析 , Tomato
需要php环境。
getMessage(); return null; } } function api_call($api, $data) { if ($api == '' || !is_array($data)) { exit('内部错误:参数错误'); } $api = 'https://dnsapi.cn/' . $api; $data = array_merge($data, array('login_email' => $this->dnspod_user, 'login_password' => $this->dnspod_pwd, 'format' => 'json', 'lang' => 'cn', 'error_on_empty' => 'no')); $result = $this->post_data($api, $data); if (!$result) { exit('内部错误:调用失败'); } $results = @json_decode($result, 1); if (!is_array($results)) { exit('内部错误:返回错误'); } if ($results['status']['code'] != 1) { exit($results['status']['message']); } return $results; } private function post_data($url, $data) { if ($url == '' || !is_array($data)) { return false; } $ch = @curl_init(); if (!$ch) { exit('内部错误:服务器不支持CURL'); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_USERAGENT, 'LocalDomains_PHP/1.0.0([email protected])'); $result = curl_exec($ch); curl_close($ch); return $result; } public function exec() { $ip = $this->getMyIp(); $domainInfo = $this->api_call('domain.info',array('domain' => $this->domain)); $domainId = $domainInfo['domain']['id']; $record = $this->api_call('record.list',array('domain_id'=> $domainId,'offset' =>'0','length' => '1','sub_domain' =>$this->sub_domain)); if($record['info']['record_total'] == 0) { $this->api_call('record.create', array( 'domain_id' => $domainId, 'sub_domain' => $this->sub_domain, 'record_type' => 'A', 'record_line' => '默认', 'value' => $ip, 'ttl' => '120' )); } else { if($record['records'][0]['value'] != $ip) { $this->api_call('record.modify', array( 'domain_id' => $domainId, 'record_id' => $record['records'][0]['id'], 'sub_domain' => $this->sub_domain, 'record_type' => 'A', 'record_line' => '默认', 'value' => $ip )); } else { echo '指向正常'; } } } } $dns = new Dns(); $dns->exec(); ?>
保存为dns.php文件。
如Tomato路由器已经挂载php环境,执行:
/opt/bin/php /opt/dns.php
或将以上命令放在脚本设置---当WAN联机里。