tomato、ddwrt、linux Dnspod动态域名sh版本设置及动态更新方法(新增php和python版)
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/.*: \(\{1,3\}\.\{1,3\}\.\{1,3\}\.\{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根证书否则运行报错,有需要回帖咨询!
**** Hidden Message ***** 域名ID、记录ID,可以到这里查询:http://www.dh.vg/tools/dnspod.html
如果看不明白,可以登录里面的php版来查询。ps.php版是站长修改的作品,仔细查看了每一行代码,但使用php版还是怕被盗号的用户请及时去官网修改密码就行了,这只是个api接口做的程序没有任何危险! php和python版(注意要修改里面的帐号域名为你自己的,转自:夜的第七章)
php版首先安装lighttpd或nginxd配合php搭建路由环境,论坛上有介绍方法自己搜!
贴出php脚本:
<?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']['value'] != $ip)
{
$this->api_call('record.modify',
array(
'domain_id' => $domainId,
'record_id' => $record['records']['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管理器,不懂参考:https://bbs.swdyz.com/thread53sw1dyz2.shtml),ssh或Telnet连接tomato或ddwrt路由执行命令:ipkg install python
贴上python代码:
#!/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']['value'].strip() != ip.strip():
self.api_call('record.modify',
{
'domain_id' : domainId,
'record_id' : rs['records']['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了! 谢谢分享 看了LZ的帖子,我只想说一句很好很强大! 看了LZ的帖子,我只想说一句很好很强大! 感谢分享 支持分享!!!!!!!
页:
[1]