最新消息:建议使用非IE内核浏览本站博客,以便得到更好体验。比如360极速浏览器火狐等浏览器。

OPENWRT如何使用DDNS

命令代码 李展滢 9461浏览 0评论

        我在使用/etc/init.d/ddns  restart重启命令是,总是遇到错误,网上的解决方法很多,但是不一定都适用。我教大家一个方法,可借鉴修复(我的是华为HG255D设备)。

       首先进/etc/config/ddns进行编辑,贴上代码:


config 'goble'
	option 'enabled' '1'
	//1为打开DDNS,0为关闭
	option 'check_interval' '999999'
	//999999分钟更新一次,当然可以自己设置
 
config 'service'
	option 'service_name' 'oray.com'
	//使用花生壳解析,如果使用3322就输入3322.org
	option 'domain' 'www.lizhanying.net'
	//解析域名为www.lizhanying.net
	option 'username' 'lizhanying'
	//用户名为lizhanying
	option 'password' '123456'
	//密码为123456
	option 'enabled' '1'
	//1为打开,0为关闭
	option 'ip_network' 'wan'
	//使用wan口ip
	option 'ipaddr' '117.141.25.20'
	//ip地址,会自动更新,无需理会
	option 'uptime' 'Sat Apr 12 13:50:15 2014'
	//上一次更新时间

       打开/usr/lib/ddns/dynamic_dns_updater.sh进行编辑,当然,建议先备份。打开后将里面的文字全部删除,并粘贴我下面的代码即可,保存并退出。使用/etc/init.d/ddns  restart命令即可更新,更新有延迟,请等几分钟再用nslookup命令查询。

. /etc/functions.sh

INTERFACE=${INTERFACE}
update_ipaddress(){
  [ -n "$service_name" ]&&update_url=$(cat /usr/lib/ddns/services |grep $service_name|awk -F " " '{print $2}')
# neiwang
    [ "$neiwang" == "1" ]&& {
        if [ -n "$ip_network" ]; then
        num=`echo $ip_network | tr -d "wan"`
        if [ -z $num ] ; then num=0; fi  
        wanrule=$((($num+1)*10)) 
        fi
   local ddnsipd=$(cat /tmp/ddnsipd)
   [ -n "$ddnsipd" ]&& eval $ddnsipd
        iptables -t mangle -A ASSIGNOUT -d www.3322.org -j MARK --set-mark $wanrule
        iptables -t mangle -A ASSIGNOUT -d  checkip.dyndns.org -j MARK --set-mark $wanrule
#http://www.3322.org/dyndns/getip
   echo  "iptables -t mangle -D ASSIGNOUT -d www.3322.org -j MARK --set-mark $wanrule" >/tmp/ddnsipd
  echo  "iptables -t mangle -D ASSIGNOUT -d checkip.dyndns.org -j MARK --set-mark $wanrule" >/tmp/ddnsipd

[ $(echo $service_name |grep -v 3322 ) ]||ipaddr=$( echo `wget -q -O- http://www.3322.org/dyndns/getip`|grep -o "$ip_regex")
[ $(echo $service_name |grep -v dyndns ) ]|| ipaddr=$(echo `wget -q -O- http://checkip.dyndns.org/`|grep -o "$ip_regex")
  }

#change username

update_url=$(echo $update_url | sed s/"\[USERNAME\]"/"$username"/g)
  #change password
update_url=$(echo $update_url | sed s/"\[PASSWORD\]"/"$password"/g)
  #change domain
update_url=$(echo $update_url | sed s/"\[DOMAIN\]"/"$domain"/g)
  #change ipaddr
  update_url=$(echo $update_url | sed s/"\[IP\]"/"$ipaddr"/g)  
  #delete ""
  update_url=$(echo $update_url | sed s/"\""/""/g)  
  #update  ipaddr 
  echo "wget -q -O- $update_url" 

  wget -t 2 -T 10 -q -O- $update_url 
  nowtime=`date +%c`
#echo `wget -q -O- http://checkip.dyndns.org/`|grep -o "$ip_regex"
uci set ddns.$section.uptime="$nowtime"
uci set ddns.$section.ipaddr="$ipaddr"
uci commit ddns

}

ddns_goble_get(){
  config_get enabled $1 enabled
  config_get check_interval $1 check_interval
# echo "1=$enabled 2=$check_interval" 

if  [ "$enabled" == "1" -a "$check_interval" != "0" ]; then 
[ -z "$(cat /etc/crontabs/root| grep ddns_scheduler)" ]&&echo "*/${check_interval} * * * * /usr/lib/ddns/dynamic_dns_updater.sh scheduler  #ddns_scheduler#" >> /etc/crontabs/root
[ -z "$(cat /tmp/tmp_crontab  | grep ddns_scheduler)" ]&&echo "*/${check_interval} * * * * /usr/lib/ddns/dynamic_dns_updater.sh scheduler  #ddns_scheduler#" >> /tmp/tmp_crontab
   [ $(ps | grep crond | grep -v grep | wc -l ) == 0 ] && /etc/init.d/cron restart
  else 
  [ -n "$(cat /tmp/tmp_crontab| grep  ddns_scheduler)" ]&& sed -i -e '/ddns_scheduler/d' /tmp/tmp_crontab
    [ -n "$(cat /etc/crontabs/root| grep  ddns_scheduler)" ]&& sed -i -e '/ddns_scheduler/d' /etc/crontabs/root
  fi
}

ddns_service_get(){

unset ipaddr  
unset update_url
config_get enabled $1 enabled
config_get neiwang $1 neiwang
config_get service_name $1 service_name
config_get domain $1 domain
config_get username $1 username
config_get password $1 password
config_get ip_network $1 ip_network
config_get update_url $1 update_url
config_get uptime $1 uptime
section=$1
ip_regex="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" 
echo line 97
[ "$enabled" == "1" ]&&{
ipaddr=$(uci -P /var/state get network.${ip_network}.ipaddr)
old_ipaddr=$(ping -c 1 $domain|head -1 | grep -o "$ip_regex")
[ -z "$ipaddr" ]&&exit 0
#  echo "enabled=$enabled old_ipaddr=$old_ipaddr ipaddr=$ipaddr"
#ifup update ip
if [ "$isifup" == "1" -a "$ip_network" = "$INTERFACE" ];then
update_ipaddress
fi 
# scheduler update ip
if [ "$ipaddr" != "$old_ipaddr" ];then
update_ipaddress
fi
}

}
config_load ddns
config_foreach oscam_conf conf
case "$1" in
start )
   config_foreach ddns_goble_get goble
   config_foreach ddns_service_get service
  ;;
stop )
  echo "stop"
  ;;
scheduler )
  config_foreach ddns_service_get service
  ;;
ifup )
  local isifup
  isifup=1
  config_foreach ddns_service_get service
  ;;
esac

转载请注明:宿居博客 » OPENWRT如何使用DDNS

您必须 登录 才能发表评论!