几篇参考文章:
1、http://www.anrip.com/post/872
2、http://fengwan.blog.51cto.com/508652/1404534/
源码地址:https://github.com/anrip/ArDNSPod

只是源码只能实现获取本地IP,若是有路由器,仅能获得内网地址。,要向获得外网地址:需要结合两篇文章。
两个文件,放到/usr/bin下面。
第一个文件,ddnspod.sh:

[php]
#!/bin/sh

#################################################
# AnripDdns v5.08
# Dynamic DNS using DNSPod API
# Original by anrip<mail@anrip.com>, http://www.anrip.com/ddnspod
# Edited by ProfFan
#################################################

# OS Detection
case $(uname) in
'Linux')
echo "Linux"
arIpAddress() {
local extip
extip='http://members.3322.org/dyndns/getip'
echo $extip
}
;;
'FreeBSD')
echo 'FreeBSD'
exit 100
;;
'WindowsNT')
echo "Windows"
exit 100
;;
'Darwin')
echo "Mac"
arIpAddress() {
ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'
}
;;
'SunOS')
echo 'Solaris'
exit 100
;;
'AIX')
echo 'AIX'
exit 100
;;
*) ;;
esac

# Get script dir
# See: http://stackoverflow.com/a/29835459/4449544
rreadlink() ( # Execute the function in a *subshell* to localize variables and the effect of `cd`.

target=$1 fname= targetDir= CDPATH=

# Try to make the execution environment as predictable as possible:
# All commands below are invoked via `command`, so we must make sure that `command`
# itself is not redefined as an alias or shell function.
# (Note that command is too inconsistent across shells, so we don't use it.)
# `command` is a *builtin* in bash, dash, ksh, zsh, and some platforms do not even have
# an external utility version of it (e.g, Ubuntu).
# `command` bypasses aliases and shell functions and also finds builtins
# in bash, dash, and ksh. In zsh, option POSIX_BUILTINS must be turned on for that
# to happen.
{ \unalias command; \unset -f command; } >/dev/null 2>&1
[ -n "$ZSH_VERSION" ] && options[POSIX_BUILTINS]=on # make zsh find *builtins* with `command` too.

while :; do # Resolve potential symlinks until the ultimate target is found.
[ -L "$target" ] || [ -e "$target" ] || { command printf '%s\n' "ERROR: '$target' does not exist." >&2; return 1; }
command cd "$(command dirname -- "$target")" # Change to target dir; necessary for correct resolution of target path.
fname=$(command basename -- "$target") # Extract filename.
[ "$fname" = '/' ] && fname='' # !! curiously, `basename /` returns '/'
if [ -L "$fname" ]; then
# Extract [next] target path, which may be defined
# *relative* to the symlink's own directory.
# Note: We parse `ls -l` output to find the symlink target
# which is the only POSIX-compliant, albeit somewhat fragile, way.
target=$(command ls -l "$fname")
target=${target#* -> }
continue # Resolve [next] symlink target.
fi
break # Ultimate target reached.
done
targetDir=$(command pwd -P) # Get canonical dir. path
# Output the ultimate target's canonical path.
# Note that we manually resolve paths ending in /. and /.. to make sure we have a normalized path.
if [ "$fname" = '.' ]; then
command printf '%s\n' "${targetDir%/}"
elif [ "$fname" = '..' ]; then
# Caveat: something like /var/.. will resolve to /private (assuming /var@ -> /private/var), i.e. the '..' is applied
# AFTER canonicalization.
command printf '%s\n' "$(command dirname -- "${targetDir}")"
else
command printf '%s\n' "${targetDir%/}/$fname"
fi
)

DIR=$(dirname -- "$(rreadlink "$0")")

# Global Variables:

# Token-based Authentication
arToken=""
# Account-based Authentication
arMail=""
arPass=""

# Load config

#. $DIR/dns.conf

# Get Domain IP
# arg: domain
arNslookup() {
#wget --quiet --output-document=- $inter$1
echo ""
}

# Get data
# arg: type data
arApiPost() {
local agent="AnripDdns/5.07(ligel@163.com)"
local inter="https://dnsapi.cn/${1:?'Info.Version'}"
if [ "x${arToken}" = "x" ]; then # undefine token
local param="login_email=${arMail}&login_password=${arPass}&format=json&${2}"
else
local param="login_token=${arToken}&format=json&${2}"
fi
wget --quiet --no-check-certificate --output-document=- --user-agent=$agent --post-data $param $inter
}

# Update
# arg: main domain sub domain
arDdnsUpdate() {
local domainID recordID recordRS recordCD myIP
# Get domain ID
domainID=$(arApiPost "Domain.Info" "domain=${1}")
domainID=$(echo $domainID | sed 's/.*{"id":"\([0-9]*\)".*/\1/')

# Get Record ID
recordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}")
recordID=$(echo $recordID | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')

# Update IP
myIP=$(arIpAddress)
recordRS=$(arApiPost "Record.Ddns" "domain_id=${domainID}&record_id=${recordID}&sub_domain=${2}&record_type=A&value=${myIP}&record_line=默认")
recordCD=$(echo $recordRS | sed 's/.*{"code":"\([0-9]*\)".*/\1/')

# Output IP
if [ "$recordCD" = "1" ]; then
echo $recordRS | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/'
return 1
fi
# Echo error message
echo $recordRS | sed 's/.*,"message":"\([^"]*\)".*/\1/'
}

# DDNS Check
# Arg: Main Sub
arDdnsCheck() {
local postRS
local hostIP=$(arIpAddress)
local lastIP=$(arNslookup "${2}.${1}")
echo "hostIP: ${hostIP}"
echo "lastIP: ${lastIP}"
if [ "$lastIP" != "$hostIP" ]; then
postRS=$(arDdnsUpdate $1 $2)
echo "postRS: ${postRS}"
if [ $? -ne 1 ]; then
return 0
fi
fi
return 1
}

# DDNS
#echo ${#domains[@]}
#for index in ${!domains[@]}; do
# echo "${domains[index]} ${subdomains[index]}"
# arDdnsCheck "${domains[index]}" "${subdomains[index]}"
#done

. $DIR/dns.conf

[/php]

第二个文件,dns.conf:

[php]
# For security reasons, it is recommended that you use token-based auth instead
# arMail="test@gmail.com"
# arPass="123"

# Combine your token ID and token together as follows
arToken="12345,7676f344eaeaea9074c123451234512d"

# Place each domain you want to check as follows
# you can have multiple arDdnsCheck blocks
arDdnsCheck "test.org" "subdomain"
[/php]

增加执行权限

[php]chmod +x /root/ddnspod.sh[/php]

加入任务计划(5分钟执行一次)

[php]echo "*/5 * * * * root /root/ddnspod.sh" >> /etc/crontab[/php]

计划任务执行情况可以查看/var/log/cron
说明:
如果在执行后发现

[php]
[root@localhost ~]# ./ddnspod.sh
./ddns.sh: line 24: nslookup: command not found
hostIP: 114.234.77.222
lastIP:
postRS: 114.234.77.222[/php]

如果你使用的是CentOS则执行

[php]yum -y install bind-utils[/php]

或者是ubuntu的话

[php]sudo apt-get install dnsutils[/php]

CentOS通过DNSpod实现动态域名

rong341233关注0人评论6012人阅读2014-04-29 14:01:41

因为家里使用ADSL动态IP上网,有时在外面需要使用家里资源,原本使用的是TP-LINK自带的花生壳解析,用过之后发现很不稳定。因为自己注册了域名也已使用了DNSPOD解析。

1.新建脚本文件

<span style="color:#333333"><span style="color:#567482">vim ddns.sh</span></span>

文件内容如下:

<span style="color:#333333"><span style="color:#567482">#!/bin/sh# 全局变量表
arPass=arMail=""# 获得外网地址
arIpAdress() {local inter="http://members.3322.org/dyndns/getip"wget --quiet --no-check-certificate --output-document=- $inter
}# 查询域名地址
# 参数: 待查询域名
arNslookup() {local dnsvr="114.114.114.114"nslookup ${1} $dnsvr | tr -d '\n[:blank:]' | awk -F':' '{print $6}'
}# 读取接口数据
# 参数: 接口类型 待提交数据
arApiPost() {local agent="Ddns/3.08"local inter="https://dnsapi.cn/${1:?'Info.Version'}"local param="login_token=${arID},${arToken}&format=json&${2}"wget --quiet --no-check-certificate --output-document=- --user-agent=$agent --post-data $param $inter
}# 更新记录信息
# 参数: 主域名 子域名
arDdnsUpdate() {local domainID recordID recordRS recordCD# 获得域名IDdomainID=$(arApiPost "Domain.Info" "domain=${1}")domainID=$(echo $domainID | sed 's/.\+{"id":"\([0-9]\+\)".\+/\1/')# 获得记录IDrecordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}")recordID=$(echo $recordID | sed 's/.\+\[{"id":"\([0-9]\+\)".\+/\1/')# 更新记录IPrecordRS=$(arApiPost "Record.Ddns" "domain_id=${domainID}&record_id=${recordID}&sub_domain=${2}&record_line=默认")recordCD=$(echo $recordRS | sed 's/.\+{"code":"\([0-9]\+\)".\+/\1/')# 输出记录IPif [ "$recordCD" == "1" ]; thenecho $recordRS | sed 's/.\+,"value":"\([0-9\.]\+\)".\+/\1/'return 1fi# 输出错误信息echo $recordRS | sed 's/.\+,"message":"\([^"]\+\)".\+/\1/'
}
# 动态检查更新
# 参数: 主域名 子域名
arDdnsCheck() {local postRSlocal hostIP=$(arIpAdress)local lastIP=$(arNslookup "${2}.${1}")echo "hostIP: ${hostIP}"echo "lastIP: ${lastIP}"if [ "$lastIP" != "$hostIP" ]; thenpostRS=$(arDdnsUpdate $1 $2)echo "postRS: ${postRS}"if [ $? -ne 1 ]; thenreturn 0fifireturn 1
}#################################################### 设置用户参数
arID="89322"
arToken="3222w2e3213e123421421424234"
#填写账户的API token# 检查更新域名
arDdnsCheck "anrip.com" "lab"</span></span>

ps:以上代码来自http://www.anrip.com/post/872

我们只需要将上面的

# 设置用户参数

arID="user@anrip.com"     //DNSPOD的API ID

arToken="anrip.net"                //DNSPOD的API Token

# 检查更新域名

arDdnsCheck"anrip.com"  "lab"     //需要更新的域名为anrip.com,主机为lab,就是lab.anrip.com

arDdnsCheck"anrip.net"  "lab"      //更新lab.anrip.net

修改成自己的信息即可

3.增加执行权限

<span style="color:#333333"><span style="color:#567482">chmod +x /root/ddns.sh</span></span>

4.加入任务计划(5分钟执行一次)

<span style="color:#333333"><span style="color:#567482">echo "*/5  * * * * root /root/ddns.sh" >> /etc/crontab</span></span>

计划任务执行情况可以查看/var/log/cron

说明:

如果在执行后发现

<span style="color:#333333"><span style="color:#567482">[root@localhost ~]# ./ddns.sh
./ddns.sh: line 24: nslookup: command not found
hostIP: 114.234.77.222
lastIP:
postRS: 114.234.77.222</span></span>

如果你使用的是CentOS则执行

<span style="color:#333333"><span style="color:#567482">yum -y install bind-utils</span></span>

或者是ubuntu的话

<span style="color:#333333"><span style="color:#567482">sudo apt-get install dnsutils</span></span>

CentOS通过DNSpod实现DDNS动态域名,在家也可以搭建主机服务器了相关推荐

  1. 让我们用 Node.js 写自己的 DDNS 动态域名程序

    原文链接:http://xcoder.in/2014/09/20/lets-build-ourselves-ddns/ 起因 一开始我在移动的宽带中.那个时候虽然还不是完全的局域网,但是电信网络访问不 ...

  2. RouterOS(ros)软路由实现DDNS动态域名功能(DNSPOD)

    DDNS(Dynamic Domain Name Server,动态域名服务)是将用户的动态IP地址映射到一个固定的域名解析服务上,用户每次连接网络的时候客户端程序就会通过信息传递把该主机的动态IP地 ...

  3. 如何设置ddns动态域名实现内网发布外网

    在本地搭建好服务器,部署好web网站或其他应用后,需要设置动态域名服务ddns,将主机的内网IP端口映射到外网访问,才能实现在外网访问内网.今天小编就和大家分享一下内网发布外网方案,即如何设置ddns ...

  4. windows下ipv6 ddns 动态域名自动注册批处理 dynv6(服务方式,开机自动运行无需登录)

    现在IPV6加速推进,几乎限量的地址资源,但是ipv6地址比V4难记忆多了nnds(动态域名)就显得很有必要. 网文 免费好用的IPv6之DDNS服务-Openwrt上dynv6的使用介绍 该网文详细 ...

  5. 华为防火墙USG2220 DDNS动态域名配置

    关于DDNS DDNS(DynamicDomain Name Server)是动态域名服务的缩写.DDNS是将用户的动态IP地址映射到一个固定的域名解析服务上,用户每次连接网络的时候客户端程序就会通过 ...

  6. php实现ddns,动态域名DDNS实现

    利用php重定向功能实现DDNS. 在openwrt上配置自定义的ddns,定时上报路由器的动态IP信息 1 将IP信息定时发送到http://xxx.xxx.net/updateip.php 在服务 ...

  7. 记录一次使用DDNS动态域名解析服务外网通过域名访问内网服务器服务的过程

    DDNS概念 DDNS(Dynamic Domain Name Server)是动态域名服务的缩写.  DDNS是将用户的动态IP地址映射到一个固定的域名解析服务上,用户每次连接网络的时候客户端程序就 ...

  8. 企业实战(17)记录一次使用DDNS动态域名解析服务外网通过域名访问内网服务器服务的过程

    DDNS概念  DDNS(Dynamic Domain Name Server)是动态域名服务的缩写.  DDNS是将用户的动态IP地址映射到一个固定的域名解析服务上,用户每次连接网络的时候客户端程序 ...

  9. 公云 PubYun – 一个可免费DDNS(动态域名)服务商

    本页目录 介绍:什么是DDNS(动态域名) 配置自动切换IP 具体操作: 公司在开发项目的时候,需要公网IP,因为公司使用的是电信家庭千兆宽带,电信不给固定的IP.所以我们就需要找个办法,间接性的获得 ...

  10. 动态域名搭建exchang邮箱服务器

    利用花生壳域名和宽带架设自己的Exchange 2010 Web邮件服务器 实验AD域控:test.com 实验内网固定ip地址:192.168.0.169 实验计算机主机名:PDC 实验动态域名:h ...

最新文章

  1. 天翼云从业认证课后习题(3.2天翼云存储产品和3.4云数据库)
  2. pcu tps_Mac版Microsoft Office 2011重新定义您的TPS报告体验
  3. 时钟在微型计算机中作用,微型计算机系统中的实时时钟
  4. HDU 3573(详解+思路+代码)
  5. CORS跨域时axios无法获取服务器自定义的header信息 - 番外篇
  6. 豪横C4D电商实用素材,从此横着走
  7. 后端CORS解决跨域问题
  8. Markdown-Latex全称量词和存在量词(对于全体、存在)
  9. VMware vCenter Server安装与配置
  10. Foobar2000自用插件
  11. t450加固态硬盘教程_Thinkpad T450 超级笔记本 开箱更换SSD和内存
  12. pytorch搭建卷积神经网络【第三课_自己搭建对抗神经网络】
  13. RISC-V MCU 智能语音物联网家居控制系统
  14. matlab 窄带通,MATLAB 窄带随机过程
  15. 查看users表空间使用率高的原因
  16. 百度AI 开放平台 - 各大AI 开放平台开放能力解读和分析,技术的应用场景,技术前景
  17. uniapp 动态设置导航栏标题 副标题 背景图片 web-view
  18. 线性代数学习笔记7-4:马尔可夫矩阵、矩阵幂的稳态问题
  19. 8583报文MAC验证实现过程
  20. 谈谈如何设计秒杀服务

热门文章

  1. YDOOK: ANSYS Maxwell 19 教程21:Maxwell 2D 直流传导电场 求解设置 分析设置
  2. 接口文档系统 - Yapi
  3. 音乐播放器代码和网页播放器代码
  4. windows bat脚本 启动和停止程序
  5. GeoTools-WKT\GeoJson相互转换
  6. Docker 极简入门指南
  7. 中国室内定位系统LBS市场现状研究分析与发展前景预测报告(2022)
  8. 计算机软件存储位置,微信电脑版存储位置在什么地方?查看微信电脑版存储路径的方法...
  9. 大数据学习之hadoop——09一次完整的edits、fsimage、edits_inprogress、chkpoint、NameNode运行原理分析
  10. hive sql中常用技巧