`
sd8089730
  • 浏览: 251355 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
社区版块
存档分类
最新评论

linux dnspod客户端(适用于openwrt,ddwrt, centos, ubuntu等)

 
阅读更多

转载至:http://www.zhetenger.com/node/44

 

几个月前写过一个dnspod脚本,也是用来自动更新动态IP地址到DNSPOD解析顶级域名。周末闲着无聊,重新写了个脚本,在原来的基础上增加了一些功能,比如批量创建,批量更新,批量删除。脚本兼容支持centos, ubuntu这些使用bash的版本,也支持集成busybox的轻量发行版,如openwrt, ddwrt, tomato.但本人只在openwrt, centos和ubuntu上测试过。下面说下使用方法,首先你得有一个顶级域名并已转到dnspod解析,这步就省略了。

下载脚本

下载lino-dnspod或复制脚本内容到任意目录,保存为lino-dnspod, 推荐放在/usr/bin/下面,这样,直接输入文件名即可运行。复制粘贴时要注意dnspod的线路值“默认”,如果你的putty没有选择UTF-8,会导致粘贴后“默认”两字乱码。

配置帐号

打开脚本,填上你自己的dnspod帐号和密码以及要创建和更新的子域名,其它选项可可使用默认的。子域名用单个空格分开即可

email='admin@zhetenger.com'
password='password'
sub_domains='www ftp test'

创建域名和记录值

lino-dnspod -C zhetenger.com
把zhetenger.com换成你自己的域名,记住是不带www的主域名。如果已经创建过了就可以跳过这步
然后执行创建记录的命令
lino-dnspod -c zhetenger.com &
成功创建域名记录后,脚本会在后台运行,每五分钟检测一次IP是否变化,有变化就更新,这样可减少对dnspod服务的请求次数。

启用随机运行

openwrt的用户可以把下面的脚本拷贝到/etc/init.d/下面,保存为dnspod,然后chmod 755 /etc/init.d/dnspod

#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org

START=60

start() {
       lino-dnspod -u zhetenger.com &
}

stop() {
        killall lino-dnspod
}

然后,/etc/init.d/lino-dnspod enable
其它版本也可以按照此方法。

命令简介

创建域名命令
lino-dnspod -C example.com

创建记录命令
lino-dnspod -c example.com

删除记录命令
lino-dnspod -r example.com

更新记录命令
lino-dnspod -u example.com

#!/bin/sh
#written by benson huang
#admin@zhetenger.com

curl_status=`which curl 2>/dev/null`
[ -n "$curl_status" ] || { echo "curl is not installed";exit 3; }
#dnspod帐号名
email='admin@zhetenger.com'
#dnspod密码
password='xxxxxxx'
sub_domains='www ftp @'
API_url="https://dnsapi.cn"
pub_ip_addr=$(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')
format='json'
lang='en'
record_type='A'
offset="2"
length=""
os=$(uname -a | egrep -io 'openwrt' | tr [A-Z] [a-z])
shared_data="login_email=$email&login_password=$password&format=$format&lang=$lang&"
PROGRAM=$(basename $0)
#check for changed ip every 300 seconds
wait=300

getJsonValue(){
      params="$1"
 echo $json_data | sed 's/\\\\\//\//g' | sed 's/[{}]//g;s/\(\[\|\]\)//g' |\
 awk -F ',' '{ for (i=1;i<=NF;i++) { print $i }}' |\
 sed 's/":/\|/g;s/"//g' |\
 awk -v k="$params" -F'|' '{ if ($(NF - 1) == k )  print $NF }'
}

getDomainId() {
     local json_data=`curl -k -A 'xddns' $API_url/Domain.info -d ${shared_data}domain=$domainname`
          getJsonValue id
 }
 
getRecordId() {

  for sub_domain in $sub_domains;do

      local json_data
      json_data=$(curl -k -A 'xddns' $API_url/Record.List -d\ "${shared_data}record_type=$record_type&domain_id=$domain_id&sub_domain=${sub_domain}&offset=${offset}&length=${length}")
	  
	   #check if record type is NS
	   IS_NS=$(getJsonValue type | grep -o 'NS' | head -n1)
	   
      #if there are 3 @ subdomains, get the non-NS record id only
     if [ "$IS_NS" = "NS" ];then
	     
		 numofline=$(getJsonValue id | sed '/^[0-9]\{7\}$/d' | wc -l)
		 
		[ $numofline -eq 3 ] && tmp_result_id="$(getJsonValue id | sed '/^[0-9]\{7\}$/d' | head -n1 )" || continue

     else  
	       tmp_result_id="$(getJsonValue id | sed '/^[0-9]\{7\}$/d')"
	 fi	
	        #if result_id is not empty or unset, append a space to split record id
		   result_id="${result_id:+${result_id} }${tmp_result_id}"
		
  done
	  
      echo $result_id
      exit
}

createDomain() {
      curl -k -A 'xddns' $API_url/Domain.Create -d ${shared_data}domain=$domainname
       exit
}

createRecord() {
             for sub_domain in $sub_domains;do

             local extra_data="&domain_id=$domain_id&sub_domain=$sub_domain&record_type=$record_type&record_line=默认&value=${pub_ip_addr}"
             curl -k -A 'xddns' $API_url/Record.Create -d $shared_data"$extra_data"			 
            done
}

updateRecord() {

         local record_id_list=`getRecordId`
         tmp_sub_domains=${sub_domains}

         for record_id in $record_id_list;do

                 sub_domain=$(echo $tmp_sub_domains | awk '{ print $1 }')
                 tmp_sub_domains=${tmp_sub_domains#* }

      local extra_data="&domain_id=$domain_id&sub_domain=${sub_domain}\
          &record_type=${record_type}&record_line=默认&value=${pub_ip_addr}&record_id=$record_id"

      curl -k -A 'xddns' $API_url/Record.Modify -d $shared_data"$extra_data"

          done
}

		
removeRecord(){

             read -p "list the subdomains to be deleted " subdomain

                 sub_domains="$subdomain"
				 
				 for sub_domain in $sub_domains;do

                   record_id=`getRecordId`
				   
                  local extra_data="&domain_id=$domain_id&record_id=$record_id"
                  curl -k -A 'xddns' $API_url/Record.Remove -d $shared_data"$extra_data"
			    done

                 exit
				
 }



checkip() {
          oldip=$pub_ip_addr
          newip=$(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')

                  if [ "$newip" != "$oldip" ];then
                        oldip="$newip"
                        return 8
                  else
                        return 3
                  fi
}

 if [ $# -ne 2 ];then
      echo "usage:$PROGRAM |-c create record|-r remove records|-C create new domain|-m modify record | -bc create a batch of records DOMAIN [eg.example.com]"
          exit 3
 fi

 [ -n "$2" ] && domainname=$2

domain_id=`getDomainId $domainname`


  case $1 in
          -c)createRecord $domainname;;
          -r)removeRecord $domainname;;
          -u)updateRecord $domainname;;
          -C)createDomain $domainname;;
          -i)getRecordId $domainname;;
          *) echo "no such option";
             exit 3;;
  esac


 while [ 1 ];do

          checkip
		  
       if [ $? -eq 8 ];then
               pub_ip_addr="$newip"
               updateRecord
       fi
	   
       sleep $wait


done
分享到:
评论

相关推荐

    DNSpod客户端版 v1.0.zip

    DNSpod客户端版是一款将DNSpod网站主要功能集在了更轻的200k不到的小工具中,更方便管理/查看! DNSpod是很多站长都爱用且信赖的DNS解析服务提供商,本软件专门为那些站点多,域名管理麻烦的站长使用!软件干净、...

    易语言源码易语言DNSPod客户端源码.rar

    易语言源码易语言DNSPod客户端源码.rar 易语言源码易语言DNSPod客户端源码.rar 易语言源码易语言DNSPod客户端源码.rar 易语言源码易语言DNSPod客户端源码.rar 易语言源码易语言DNSPod客户端源码.rar 易语言源码...

    DNSPod客户端

    DNSPod 客户端 PC版 包括域名管理、批量操作记录等功能。

    DNSPod客户端.rar

    DNSPod客户端.rar

    DNSpod客户端 V2.1 绿色版.rar

    2.1版增加了动态域名服务. ... ============================================ 是一款域名解析管理软件 ...在 www.dnspod.com 注册账号添加域名 就可以试用本程序管理解析了 本软件没有捆绑任何插件.

    易语言DNSpod客户端源码.rar

    易语言DNSpod客户端源码.rar

    易语言DNSpod客户端源码.zip易语言项目例子源码下载

    易语言DNSpod客户端源码.zip易语言项目例子源码下载易语言DNSpod客户端源码.zip易语言项目例子源码下载 1.合个人学习技术做项目参考 2.适合学生做毕业设计参考 3.适合小团队开发项目参考

    DNSpod DNSpod客户端版 v1.1

    DNSpod客户端版是一款将DNSpod网站主要功能集在了更轻的200k不到的小工具中,更方便管理/查看!DNSpod是很多都爱用且信赖的DNS解析服务提供商,本软件专门为那些站点多,域名管理

    易语言DNSPod客户端源码2012-11-05.rar

    易语言DNSPod客户端源码2012-11-05.rar

    易语言DNSPod客户端

    易语言DNSPod客户端源码,DNSPod客户端,Xmlhttp,子程序_取域名列表,子程序_取记录列表,子程序_添加域名,子程序_删除域名,子程序_添加记录,子程序_删除记录,子程序_设置记录状态,子程序_修改记录,到GBK编码,管理域名_...

    DNSPod官方客户端 v0.0.1.0

    本版本使用了.NET Framework 3.5重新开发,支持记录和域名的批量导入导出、批量修改、定时任务、动态域名解析(DDNS)等强大功能!

    openwrt-dnspod-ddns:openwrt-dnspod-ddns

    openwrt-dnspod-ddns 从复制

    DNSPod 客户端 v2.0

    必须安装 Microsoft .NET Framework 2.0 或更高版本 才可以正常运行. 更新地址 http://www.lgblog.org/2009/12/10/96.html

    e语言-DNSPod客户端管理

    源码实现了DNS修改,管理,添加等功能。

    易语言DNSPod客户端源码-易语言

    易语言DNSPod客户端源码

    LEDE openwrt dnspod 域名解析 动态域名更新

    LEDE及openwrt的路由中没有集成dnspod的动态域名客户端,如果需要使用该功能则要通过脚本来实现,找遍全网没找到好用的脚本,自己写了个,已稳定运行数月,该脚本支持使用Token认证和邮箱密码认证,建议使用Token...

    node.js版dnspod动态域名客户端dnspod-ddns.zip

    node.js版的dnspod动态域名客户端。dnspod-ddns完全按照dnspod的文档要求的来写的。dnspod严禁短时间内多次修改同一个记录,而dnspod-ddns内建ip变化判断机制,只在ip变化时修改记录。另外dnspod-ddns会通过dnspod...

Global site tag (gtag.js) - Google Analytics