美好的生活需要用心记录
今天家里把网络换成了联通的网络,和以前移动的网络相比,速度不用说,自然是快了很多,最重要的是联通的宽带ip是公网ip,这意味着我的电脑可以在互联网直接访问到,于是我决定弄一个动态域名。
因为的用的路由起是openwrt,上面有一个专门设置动态ip的页面,可是他默认不支持花生壳动态域名,于是我在网络上找了一下openwrt如何使用花生壳的动态域名,按照教程上的方法设置之后还是不可以,最终还是放弃了花生壳,本来想使用openwrt上的3322动态域名,发现注册竟然要实名,放弃,这个时候我发现dnspod竟然也提供动态域名的服务,下面我来介绍一下如何使用dnspod的动态域名。
1:首先你要有一个自己的顶级域名和dnspod的帐号,如何注册我就不说了,大家都会.
2:在dnspod的管理界面随便添加一个二级域名,例如aaa.xxxx.com
3:上传下面的php到任意一个php空间,如果担心安全问题可以在openwrt上安装php服务,下面是php文件的内容:
<?php error_reporting(0); set_time_limit(0); header('Content-type:text/html; charset=utf-8'); if(!$_GET['username']||!$_GET['password']||!$_GET['domain']){ exit(json_encode(array('msg'=>'username,password,domain 参数必选'))); } $domain = explode("@", $_GET['domain']); $config = array( 'login_email' => $_GET['username'], 'login_password' => $_GET['password'], 'sub_domain' => $domain[0], 'domain' => $domain[1], 'record_line' => $_GET['line']?$_GET['line']:'默认', 'ttl' => 120, 'format' => 'json', 'lang' => 'cn', 'error_on_empty' => 'no', ); $dnspod = new dnspod($config); $config['ip'] = $_GET['myip']?$_GET['myip']:$_SERVER['REMOTE_ADDR']; $dnspod->updateRecordIp($config['ip']); class dnspod { var $config; var $domain; var $sub_domain; var $record_line; function __construct($config) { $this->config = $config; $this->domain = $config['domain']; $this->sub_domain = $config['sub_domain']; $this->record_line = $config['record_line']; } public function api_call($api, $data) { if ($api == '' || !is_array($data)) { exit(json_encode(array('msg'=>'内部错误:参数错误'))); } $api = 'https://dnsapi.cn/' . $api; $data = array_merge($data,$this->config); $result = $this->post_data($api, $data); if (!$result) { exit(json_encode(array('msg'=>'内部错误:调用失败'))); } $results = @json_decode($result, 1); if (!is_array($results)) { exit(json_encode(array('msg'=>'内部错误:返回错误'))); } if ($results['status']['code'] != 1) { exit(json_encode(array('msg'=>$results['status']['message']))); } return $results; } public function updateRecordDdns(){ $record = $this->getRecordInfo(); //判断IP是否改变 if($record['records'][0]['value'] == $_SERVER['REMOTE_ADDR']){ exit(json_encode(array('msg'=>'记录不需要更新'))); } $response = $this->api_call('Record.Ddns', array('record_id'=>$record['records'][0]['id'],'record_line'=>$this->record_line)); if($response){ exit(json_encode($response)); }else{ exit(json_encode(array('msg'=>'更新失败'))); } } //获取域名信息 public function getDomainInfo(){ $response = $this->api_call('Domain.Info', array('domain' =>$this->domain)); return $response; } //获取记录 function getRecordInfo(){ $response = $this->api_call('Record.List', array('sub_domain'=>$this->sub_domain,'domain' =>$this->domain)); return $response; } //修改A记录IP function updateRecordIp($ip){ $record = $this->getRecordInfo(); //判断IP是否改变 if($record['records'][0]['value']==$ip){ exit(json_encode(array('msg'=>'记录不需要更新'))); } $response = $this->api_call('Record.Modify', array('record_id'=>$record['records'][0]['id'],'record_line'=>$this->record_line,'record_type'=>'A','value'=>$ip)); return $response; } private function post_data($url, $data) { if ($url == '' || !is_array($data)) { return false; } $ch = @curl_init(); if (!$ch) { exit(json_encode(array('msg'=>'内部错误:服务器不支持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, 'DNSPod MYDDNS/0.1 (i@biner.me)'); $result = curl_exec($ch); curl_close($ch); return $result; } }
4:修openwrt的/usr/lib/ddns/services
添加一下内容:
"dnspod.com" "http://www.xxx.com/api/dnspodupdate.php?username=[USERNAME]&password=[PASSWORD]&domain=[DOMAIN]&myip=[IP]"5:在openwrt的Web界面添加动态域名
登录openwrt的Web界面,点击服务-›动态DNS,点击添加,选中dnspod,其中用户名密码就是dnspod的登录用户名和密码,这里要注意的是主机名这里的格式是二级域名@顶级域名,如aaa@xxx.com
6:设置定时检测ip。
=======16年1月15号更新=====
今天因为网络升级导致openwrt无法发挥网络性能于是再上面加了一个一级路由,下面是openwrt作为二级路由进行ddns设置。
第一步:创建一个文本文件,例如dnspod-ddns.sh,在里面写入以下内容:
#!/bin/sh echo Updata DnsPod.cn http://www.dnspod.cn xlogin_email="xxx" #用户账号 xlogin_password="xxx" #用户密码 xdomain_id="xxx" #域名 ID xrecord_id="xxx" #记录 ID xrecord_line1="默认" #记录线路 xvalue1=$(curl -s http://checkip.dyndns.com | sed -nr 's/.*[^0-9](([0-9]+\.){3}[0-9]+).*/\1/p') #通过外网网站获取外网IP作为解析ip #xvalue1=$(nvram get wan_ipaddr) #WAN1 IP地址 #xvalue2=$(nvram get wan2_ipaddr) #WAN2 IP地址 xsub_domain1="xxx" #主机记录名 xrecord_type="A" #记录类型 xmx="" #MX优先级 不是MX记录不用填 xttl="600" #TTL # @默认--------------------------------------------------------------------------------------------------------- curl -k -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -d"login_token="${xlogin_email}","${xlogin_password}"&domain_id="${xdomain_id}"&record_id="${xrecord_id}"&sub_domain="${xsub_domain1}"&record_type="${xrecord_type}"&record_line="${xrecord_line1}"&value="${xvalue1}"&mx=""&ttl="${xttl}" " https://dnsapi.cn/Record.Modify
域名id的获取
curl 'https://dnsapi.cn/Domain.List' -d 'login_token=<your_login_token>&format=json'记录id的获取
curl 'https://dnsapi.cn/Record.List' -d 'login_token=<your_login_token>&format=json&domain_id=<your_domain_id>'
评论:
2013-10-24 06:32