免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 8715 | 回复: 22
打印 上一主题 下一主题

[学习共享] shell写的LINUX dnspod客户端 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-09-09 22:08 |只看该作者 |倒序浏览
本帖最后由 freefall12 于 2013-09-10 09:19 编辑

这个脚本干什么用的?主要用来解析顶级域名到动态IP的,同时也可以对域名记录进行批量操作,比如一次性创建或删除多个域名记录。脚本不受限于shell版本,支持bash,也支持busybox中的ash.其它的没有测试。有兴趣的可以试下。
  1. #!/bin/sh
  2. #written by benson huang
  3. #admin@zhetenger.com

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

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

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

  34. getRecordId() {

  35.   for sub_domain in $sub_domains;do

  36.       local json_data
  37.       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}")
  38.          
  39.            #check if record type is NS
  40.            IS_NS=$(getJsonValue type | grep -o 'NS' | head -n1)
  41.            
  42.       #if there are 3 @ subdomains, get the non-NS record id only
  43.      if [ "$IS_NS" = "NS" ];then
  44.             
  45.                  numofline=$(getJsonValue id | sed '/^[0-9]\{7\}$/d' | wc -l)
  46.                  
  47.                 [ $numofline -eq 3 ] && tmp_result_id="$(getJsonValue id | sed '/^[0-9]\{7\}$/d' | head -n1 )" || continue

  48.      else  
  49.                tmp_result_id="$(getJsonValue id | sed '/^[0-9]\{7\}$/d')"
  50.          fi        
  51.                 #if result_id is not empty or unset, append a space to split record id
  52.                    result_id="${result_id:+${result_id} }${tmp_result_id}"
  53.                
  54.   done
  55.          
  56.       echo $result_id
  57.       exit
  58. }

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

  63. createRecord() {
  64.              for sub_domain in $sub_domains;do

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

  69. updateRecord() {

  70.          local record_id_list=`getRecordId`
  71.          tmp_sub_domains=${sub_domains}

  72.          for record_id in $record_id_list;do

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

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

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

  78.           done
  79. }

  80.                
  81. removeRecord(){

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

  83.                  sub_domains="$subdomain"
  84.                                  
  85.                                  for sub_domain in $sub_domains;do

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

  91.                  exit
  92.                                 
  93. }



  94. checkip() {
  95.           oldip=$pub_ip_addr
  96.           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')

  97.                   if [ "$newip" != "$oldip" ];then
  98.                         oldip="$newip"
  99.                         return 8
  100.                   else
  101.                         return 3
  102.                   fi
  103. }

  104. if [ $# -ne 2 ];then
  105.       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]"
  106.           exit 3
  107. fi

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

  109. domain_id=`getDomainId $domainname`


  110.   case $1 in
  111.           -c)createRecord $domainname;;
  112.           -r)removeRecord $domainname;;
  113.           -u)updateRecord $domainname;;
  114.           -C)createDomain $domainname;;
  115.           -i)getRecordId $domainname;;
  116.           *) echo "no such option";
  117.              exit 3;;
  118.   esac


  119. while [ 1 ];do

  120.           checkip
  121.                   
  122.        if [ $? -eq 8 ];then
  123.                pub_ip_addr="$newip"
  124.                updateRecord
  125.        fi
  126.            
  127.        sleep $wait


  128. done
复制代码
使用方法可以参考这里

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
2 [报告]
发表于 2013-09-10 08:26 |只看该作者
本帖最后由 rdcwayx 于 2013-09-10 10:26 编辑

谢谢分享

那个最后的参考连接好像不工作。

论坛徽章:
0
3 [报告]
发表于 2013-09-10 09:16 |只看该作者
回复 2# rdcwayx


    连接修正了

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
4 [报告]
发表于 2013-09-10 09:50 |只看该作者
应该是在linux 下调试的吧。 简单的看了一下,很多的sed/awk,感觉是可以优化的。还有, 有几个条件是必须的:

1. 需要 curl
2.  grep 要支持 -o 选项。
3.  也是最重要的,需要dnspod的帐号。 这个不是免费的。

所以也就没法往下继续测试了。

论坛徽章:
0
5 [报告]
发表于 2013-09-10 10:40 |只看该作者
本帖最后由 freefall12 于 2013-09-10 10:42 编辑

回复 4# rdcwayx


    欢迎优化。如果你不使用某些高级功能,DNSPOD是完全免费的,随便注册个帐号然后把域名解析转过来就可以用了,一分钟不要。

论坛徽章:
0
6 [报告]
发表于 2013-09-10 10:44 |只看该作者
curl是必需的,但grep那行代码可以去掉,目前没有起到作用

论坛徽章:
14
15-16赛季CBA联赛之辽宁
日期:2019-06-16 15:47:3515-16赛季CBA联赛之广夏
日期:2016-08-13 21:24:352015亚冠之武里南联
日期:2015-07-07 17:37:372015亚冠之萨济拖拉机
日期:2015-07-06 17:07:482015亚冠之全北现代
日期:2015-06-04 13:54:272015亚冠之城南
日期:2015-05-21 15:43:212015年亚洲杯之伊朗
日期:2015-04-25 18:20:362015年亚洲杯之伊朗
日期:2015-04-20 16:06:052015年亚洲杯之科威特
日期:2015-03-07 12:51:26丑牛
日期:2014-12-30 10:26:38申猴
日期:2014-09-28 22:40:18金牛座
日期:2014-09-13 21:12:22
7 [报告]
发表于 2013-09-10 10:57 |只看该作者
回复 1# freefall12


    亲测脚本,解析正常。。

建议把解析要解析到的IP当作变量输入,第2个是,打印出的界面,可以美化下。

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
8 [报告]
发表于 2013-09-10 11:37 |只看该作者
回复 5# freefall12


    你说的是http://dyndns.com 吗? 没看到可以免费啊。 给个具体的免费注册链接好吗?

论坛徽章:
14
15-16赛季CBA联赛之辽宁
日期:2019-06-16 15:47:3515-16赛季CBA联赛之广夏
日期:2016-08-13 21:24:352015亚冠之武里南联
日期:2015-07-07 17:37:372015亚冠之萨济拖拉机
日期:2015-07-06 17:07:482015亚冠之全北现代
日期:2015-06-04 13:54:272015亚冠之城南
日期:2015-05-21 15:43:212015年亚洲杯之伊朗
日期:2015-04-25 18:20:362015年亚洲杯之伊朗
日期:2015-04-20 16:06:052015年亚洲杯之科威特
日期:2015-03-07 12:51:26丑牛
日期:2014-12-30 10:26:38申猴
日期:2014-09-28 22:40:18金牛座
日期:2014-09-13 21:12:22
9 [报告]
发表于 2013-09-10 12:04 |只看该作者
回复 8# rdcwayx


    https://www.dnspod.cn/  免费注册,免费解析个人域名。

论坛徽章:
0
10 [报告]
发表于 2013-09-10 12:14 |只看该作者
回复 7# reb00t


    嗯,我自己用的版本是提取ipconfig输出的公网IP作为变量,可以加这个选项。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP