分享

tomato、ddwrt、linux Dnspod动态域名sh版本设置及动态更新方法(新增php和python版)

 Dead n Gone 2015-07-06
tomato、ddwrt、linux Dnspod动态域名sh版本设置及动态更新方法(新增php和python版)

sh脚本版

鉴于国内域名政策等各种天朝无奈损人不利己的制度,所以最近几天在godaddy花了50左右RMB购买了一个.com的域名。(价格还可以吧?)
考虑godaddy服务器在国外延迟高 而且全英文网站不利于研究动态IP更新 所以将域名转交给Dnspod.cn解析
Dnspod 很多网站使用 比如 58 快播(咳咳) 电驴 暴风影音 手机之家等 而且永久免费 免费送 短信宕机监控

首先 opt环境是必须的 具体方法在本版 ZD呕心力作 然后安装libcurlipkg install libcurl

安装完后 将以下代码编辑一下 存放在/opt/dnspod

域名ID和记录ID下载dnspod官方windows平台的工具查看
#!/bin/sh
echo Updata DnsPod.cn http://www.dnspod.cn

xlogin_email="******"                #用户账号

xlogin_password="******"                #用户密码

xdomain_id="******"                        #域名 ID
xrecord_id="******"                        #记录 ID

xrecord_line1="默认"                        #记录线路

#xvalue1=$(curl -s http://checkip. | sed -n 's/.*: \([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/p')   #通过外网网站获取外网IP作为解析ip

xvalue1=$(nvram get wan_ipaddr)                #WAN1 IP地址

#xvalue2=$(nvram get wan2_ipaddr)        #WAN2 IP地址

xsub_domain1="router-1"                        #主机记录名

xrecord_type="A"                        #记录类型

xmx=""                                        #MX优先级   不是MX记录不用填

xttl="600"                                #TTL

# @默认---------------------------------------------------------------------------------------------------------

curl  -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -d"login_email="${xlogin_email}"&login_password="${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:///Record.Modify


将上面的文件编辑好后 保存在/opt/dnspod 并给予777执行权限
然后在路由器 系统管理 脚本设置 当Wan联机 里填写
  • /opt/dnspod >> /opt/var/log/dnspod/dnspod.log

    这样 每次动态更新后 都会生成日志放在 /opt/var/log/dnspod/dnspod.log 这个文件里 如果不需要的话 就只在当Wan联机 里填写
  • /opt/dnspod

    即可

ps.还要添加dnspod根证书否则运行报错,有需要回帖咨询!



域名ID、记录ID,可以到这里查询:http://www./tools/dnspod.html

如果看不明白,可以登录里面的php版来查询。ps.php版是站长修改的作品,仔细查看了每一行代码,但使用php版还是怕被盗号的用户请及时去官网修改密码就行了,这只是个api接口做的程序没有任何危险!




php和python版(注意要修改里面的账号域名为你自己的,转自:夜的第七章

php版首先安装lighttpd或nginxd配合php搭建路由环境,论坛上有介绍方法自己搜!
贴出php脚本:
[PHP] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
header("Content-type: text/html; charset=utf8");
class Dns
{
        #Dnspod账户
        private $dnspod_user = 'user@example.com';
        #Dnspod密码
        private $dnspod_pwd = 'password';
        #Dnspod主域名,注意:是你注册的域名
        private $domain = 'example.com';
        #子域名,如www,如果要使用根域名,用@
        private $sub_domain = 'www';
         
        function getMyIp()
        {
                try
                {
                        $ip = file_get_contents('http://www./tools/ip.php');
                        return $ip;
                }
                catch(Exception $e)
                {
                        echo $e->getMessage();
                        return null;
                }
        }
         
        function api_call($api, $data)
        {
                if ($api == '' || !is_array($data)) {
                exit('内部错误:参数错误');
                }
                $api = 'https:///' . $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([url=mailto:roy@]roy@[/url])');
                $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' => '3600'
                                        ));
                }
                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();


以上代码另存为ddns.php,拷贝到tomato路由jffs或挂载的u盘上,给予777权限
在计划任务里面添加每1小时左右运行一次如下代码:
php /mnt/sda1/ddns.php   #注意ddns.php的路径要正确
这样路由就能自动更新ip到dnspod了!



安装python(确保之前已经安装ipkg管理器,不懂参考:http://bbs./thread53sw1dyz2.shtml),ssh或Telnet连接tomato或ddwrt路由执行命令:ipkg install python

贴上python代码:

[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import urllib2,urllib,json
class Dns:
    #Dnspod账户
    _dnspod_user = 'user@example.com'
    #Dnspod密码
    _dnspod_pwd = 'password'
    #Dnspod主域名,注意:是你注册的域名
    _domain = 'example.com'
    #子域名,如www,如果要使用根域名,用@
    _sub_domain = 'www'
     
    def getMyIp(self):
        try:
            u = urllib2.urlopen('http://www./tools/ip.php')
            return u.read()
        except HTTPError as e:
            print e.read()
            return None;
     
     
         
    def api_call(self,api,data):
        try:
            api = 'https:///' + api
            data['login_email'] = self._dnspod_user
            data['login_password'] = self._dnspod_pwd
            data['format'] ='json'
            data['lang'] =  'cn'
            data['error_on_empty'] = 'no'
             
            data = urllib.urlencode(data)
            req = urllib2.Request(api,data,
                headers = {
                    'UserAgent' : 'LocalDomains/1.0.0([email]roy@[/email])',
                    'Content-Type':'application/x-www-form-urlencoded;text/html; charset=utf8',
                    })
            res = urllib2.urlopen(req)
            html = res.read()
            results = json.loads(html)
            return results
        except Exception as e:
            print e
             
     
    def main(self):
        ip = self.getMyIp()
        dinfo = self.api_call('domain.info',{'domain' : self._domain})
        domainId = dinfo['domain']['id']
        rs = self.api_call('record.list',
            {
                'domain_id': domainId,
                'offset' :'0',
                'length' : '1',
                'sub_domain' : self._sub_domain
            })
             
        if rs['info']['record_total'] == 0:
            self.api_call('record.create',
                {
                    'domain_id' : domainId,
                    'sub_domain' : self._sub_domain,
                    'record_type' : 'A',
                    'record_line' : '默认',
                    'value' : ip,
                    'ttl' : '3600'
                })
            print 'Success.'
        else:
            if rs['records'][0]['value'].strip() != ip.strip():
                self.api_call('record.modify',
                {
                    'domain_id' : domainId,
                    'record_id' : rs['records'][0]['id'],
                    'sub_domain' : self._sub_domain,
                    'record_type' : 'A',
                    'record_line' : '默认',
                    'value' : ip
                    })
            else:
                print 'Success.'
         
if __name__ == '__main__':
    d = Dns();
    d.main()


以上代码另存为ddns.py,拷贝到tomato路由jffs或挂载的u盘上用winscp连接或ssh修改文件权限为777可执行,

在计划任务里面添加每1小时左右运行一次如下代码:
python2.6  /mnt/sda1/ddns.py   #注意ddns.py的路径要正确
这样路由就能自动更新ip到dnspod了!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多