- 论坛徽章:
- 0
|
本帖最后由 freefall12 于 2013-02-01 12:25 编辑
本人是bash老菜鸟,上周折腾了好久写了一个dnspod 动态ip更新脚本,脚本在ubuntu,centos和openwrt测试正常。但感觉脚本还有很多地方可以改进,特发到这里与大家一起交流
- #!/bin/sh
- #Authored by Benson Huang
- #More details at www.zhetenger.com
- #for openwrt only. source the path variable set in /etc/profile
- is_openwrt=`uname -a | grep -o OpenWrt`
- if [ $is_openwrt ] && [ -f /etc/profile ]
- then
- . /etc/profile
- fi
- #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.
- if [ -f ~/.dnspodrc ]
- then
- . ~/.dnspodrc
- else
- read -p "email:(e.g test@gmail.com)" email
- read -p "password:" password
- read -p "sub_domain name:(e.g @, www, ftp, etc)" subdomain
- read -p "main domain: (e.g zhetenger.com)" domain
- echo "$email" | sed 's/.*@.*\.com/email=&/' > ~/.dnspodrc
- echo "$password" | sed 's/.*/password=secure&protection/' >> ~/.dnspodrc
- echo "$subdomain" | sed 's/.*/subdomain=&/' >> ~/.dnspodrc
- echo "$domain" | sed 's/.*/domain=&/' >> ~/.dnspodrc
- fi
- #parse password from ~/.dnspodrc.
- password=$(sed -n '/password/{
- s/secure//
- s/protection//
- s/password=//
- p
- }' ~/.dnspodrc)
- pub_ip_addr=$(curl -s http://members.3322.org/dyndns/getip) #get the current public IP address
- cert_file="/etc/ssl/cacert.pem" #specify the certificate file path
- dns_url="https://dnsapi.cn" #DNSPOD API URL
- data="login_email=$email&login_password=$password&format=json&lang=en&" #assign login credentials and other parameters to the variable data
- upload_data () {
- curl -s -k -A "dnspod" -d $data$2 $dns_url/$1
- }
- # if domain_id and record_id have already been set in ~/.dnspodrc, the following codes will be skipped
- exist_id=`grep -o 'domain_id=[0-9]*' ~/.dnspodrc`
- if [ -z "$exist_id" ]
- then
- domain_id=`upload_data Domain.Info domain=$domain | sed -rn 's/.*("domain":\{.[^}]+\}).*/\1/g;s/.*"id":"([0-9]+)".*/\1/p'`
- 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")
- cat >> ~/.dnspodrc domain_id=$domain_id
- record_id=$record_id
- EOF
- fi
- #update dns record
- 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解析脚本 |
|