找回密码
 立即注册
查看: 17052|回复: 9

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

[复制链接]
发表于 2012-11-17 14:36:42 | 显示全部楼层 |阅读模式
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.dyndns.com | 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://dnsapi.cn/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根证书否则运行报错,有需要回帖咨询!
游客,如果您要查看本帖隐藏内容请回复
 楼主| 发表于 2013-4-18 01:47:34 | 显示全部楼层
域名ID、记录ID,可以到这里查询:http://www.dh.vg/tools/dnspod.html

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

使用道具 举报

 楼主| 发表于 2013-2-24 21:26:07 | 显示全部楼层
php和python版(注意要修改里面的帐号域名为你自己的,转自:夜的第七章

php版首先安装lighttpd或nginxd配合php搭建路由环境,论坛上有介绍方法自己搜!
贴出php脚本:
[mw_shl_code=php,true]
<?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.leadnt.com/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://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(roy@leadnt.com)');
                $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();

[/mw_shl_code]

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

使用道具 举报

 楼主| 发表于 2013-2-24 21:44:38 | 显示全部楼层
安装python(确保之前已经安装ipkg管理器,不懂参考:http://bbs.swdyz.com/thread53sw1dyz2.shtml),ssh或Telnet连接tomato或ddwrt路由执行命令:ipkg install python

贴上python代码:

[mw_shl_code=python,true]
#!/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.leadnt.com/tools/ip.php')
                        return u.read()
                except HTTPError as e:
                        print e.read()
                        return None;
       
       
               
        def api_call(self,api,data):
                try:
                        api = 'https://dnsapi.cn/' + 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(roy@leadnt.com)',
                                        '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()
[/mw_shl_code]

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

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

使用道具 举报

发表于 2013-5-3 22:26:09 | 显示全部楼层
谢谢分享  
回复

使用道具 举报

发表于 2013-6-23 15:18:32 | 显示全部楼层
看了LZ的帖子,我只想说一句很好很强大!
回复

使用道具 举报

发表于 2013-8-6 22:43:01 | 显示全部楼层
看了LZ的帖子,我只想说一句很好很强大!
回复

使用道具 举报

发表于 2014-1-21 19:17:44 | 显示全部楼层
感谢分享      
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2024-4-19 00:03 , Processed in 0.131504 second(s), 32 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表