免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1960 | 回复: 0

[网络相关] bash写的动态dns客户端 [复制链接]

论坛徽章:
0
发表于 2013-02-01 12:00 |显示全部楼层
本帖最后由 freefall12 于 2013-02-01 12:25 编辑

本人是bash老菜鸟,上周折腾了好久写了一个dnspod 动态ip更新脚本,脚本在ubuntu,centos和openwrt测试正常。但感觉脚本还有很多地方可以改进,特发到这里与大家一起交流


  1. #!/bin/sh
  2. #Authored by Benson Huang
  3. #More details at www.zhetenger.com

  4. #for openwrt only. source the path variable set in /etc/profile
  5. is_openwrt=`uname -a | grep -o OpenWrt`

  6. if [ $is_openwrt ] && [ -f /etc/profile ]

  7. then

  8. . /etc/profile

  9. fi

  10. #import dnspod configuration file(DNSPOD login credentials, domain,subdomain,domain_id and record_id).To prevent the passord visible to anyone who stumble across this file, the password is obscured using a string of meaningless letters and numbers.

  11. if [ -f ~/.dnspodrc ]

  12. then
  13. . ~/.dnspodrc

  14. else

  15. read -p "email:(e.g test@gmail.com)" email
  16. read -p "password:" password
  17. read -p "sub_domain name:(e.g @, www, ftp, etc)" subdomain
  18. read -p "main domain: (e.g zhetenger.com)" domain

  19. echo "$email" | sed 's/.*@.*\.com/email=&/' > ~/.dnspodrc
  20. echo "$password" | sed 's/.*/password=secure&protection/' >> ~/.dnspodrc
  21. echo "$subdomain" | sed 's/.*/subdomain=&/' >> ~/.dnspodrc
  22. echo "$domain" | sed 's/.*/domain=&/' >> ~/.dnspodrc

  23. fi

  24. #parse password from ~/.dnspodrc.

  25. password=$(sed -n '/password/{
  26. s/secure//
  27. s/protection//
  28. s/password=//
  29. p
  30. }' ~/.dnspodrc)

  31. pub_ip_addr=$(curl -s http://members.3322.org/dyndns/getip) #get the current public IP address
  32. cert_file="/etc/ssl/cacert.pem" #specify the certificate file path
  33. dns_url="https://dnsapi.cn" #DNSPOD API URL
  34. data="login_email=$email&login_password=$password&format=json&lang=en&" #assign login credentials and other parameters to the variable data

  35. upload_data () {
  36. curl -s -k -A "dnspod" -d $data$2 $dns_url/$1
  37. }

  38. # if domain_id and record_id have already been set in ~/.dnspodrc, the following codes will be skipped

  39. exist_id=`grep -o 'domain_id=[0-9]*' ~/.dnspodrc`

  40. if [ -z "$exist_id" ]

  41. then
  42. domain_id=`upload_data Domain.Info domain=$domain | sed -rn 's/.*("domain":\{.[^}]+\}).*/\1/g;s/.*"id":"([0-9]+)".*/\1/p'`

  43. record_id=$(upload_data Record.List "domain_id=$domain_id" | sed -rn "s/.*(\"records\":\[.[^]]+\]).*/\1/g;s/.*\{\"id\":\"([0-9]+)\",\"name\":\"@\".*\"type\":\"A\".*/\1/p")

  44. cat >> ~/.dnspodrc domain_id=$domain_id
  45. record_id=$record_id
  46. EOF
  47. fi

  48. #update dns record
  49. upload_data Record.Modify "domain_id=$domain_id&record_id=$record_id&record_type=A&record_line=默认&ttl=600&value=$pub_ip_addr"
复制代码
dnspod动态ip解析脚本
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP