海运的博客

openwrt使用dnspod api自动更新ddns

发布时间:February 12, 2019 // 分类:OpenWrt // No Comments

通过dnspod api实现动态ddns更新ip,ipv4和ipv6支持,shell脚本如下:

#!/bin/bash
token="www.haiyun.me"
domain="haiyun.me"
if which jq > /dev/null; then
  json="jq"
elif which jsonfilter > /dev/null; then
  json="jsonfilter"
else
  echo 'please install jq or jsonfilter'
  exit
fi
if ! which curl > /dev/null || ! which curl > /dev/null; then
  echo 'please install curl and grep'
  exit
fi
if [[ $1 == "list" ]]; then
  curl -s -d "login_token=$token&format=json&domain=$domain" "https://dnsapi.cn/Record.List" | jq -r -M '.records[]|.name + "\t\t " + .type + "\t\t " + .value'
  exit
fi
if [[ $1 == "delete" ]]; then
  if [[ ! $3 || ! $2 ]]; then
    echo 'use ddns.sh delete name type'
    exit
  fi
  id=$(curl -s -d "login_token=$token&format=json&domain=$domain" "https://dnsapi.cn/Record.List" | jq -r -e ".records | .[] | select(.name == \"$2\" and .type == \"${3^^}\")|.id")
  if [[ $id ]]; then
    if curl -s -d "login_token=$token&format=json&domain=$domain&record_id=$id" https://dnsapi.cn/Record.Remove | grep -q '"code":"1"'; then
      echo "sus"
    fi
  else
    echo 'no record'
  fi
  exit
fi
if [[ ! $1 || ! $2 ]]; then
  echo 'use ddns.sh name ip'
  echo 'use ddns.sh list'
  echo 'use ddns.sh delete name type'
  exit
fi
name=$1
new_ip=$2
if [[ $new_ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  #sleep 10
  #curl http://192.168.168.6/announce.php --silent --output /dev/null
  record_type='A'
  echo 'ipv4'
elif [[ $new_ip =~ ^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$ ]]; then
  echo 'ipv6'
  record_type='AAAA'
else
  echo "invalid IP address $new_ip"
  #logger -t ddns "invalid IP address $new_ip"
  exit
fi
curl -s -d "login_token=$token&format=json&domain=$domain" "https://dnsapi.cn/Record.List" -o /tmp/dns.txt
if ! grep -q '"code":"1"' /tmp/dns.txt; then
  echo 'get record list error'
  exit
fi

if [[ $record_type == "AAAA" ]]; then
  if [[ $json == "jq" ]]; then
    id=$(jq -r -e ".records | .[] | select(.name == \"$name\" and .type == \"AAAA\")|.id" /tmp/dns.txt)
    ip=$(jq -r -e ".records | .[] | select(.name == \"$name\" and .type == \"AAAA\")|.value" /tmp/dns.txt)
  else
    ip=$(jsonfilter -i /tmp/dns.txt -e "@.records[@.name='$name'&&@.type='AAAA'].value")
    id=$(jsonfilter -i /tmp/dns.txt -e "@.records[@.name='$name'&&@.type='AAAA'].id")
  fi
elif [[ $record_type == "A" ]]; then
  if [[ $json == "jq" ]]; then
    id=$(jq -r -e ".records | .[] | select(.name == \"$name\" and .type == \"A\")|.id" /tmp/dns.txt)
    ip=$(jq -r -e ".records | .[] | select(.name == \"$name\" and .type == \"A\")|.value" /tmp/dns.txt)
  else
    ip=$(jsonfilter -i /tmp/dns.txt -e "@.records[@.name='$name'&&@.type='A'].value")
    id=$(jsonfilter -i /tmp/dns.txt -e "@.records[@.name='$name'&&@.type='A'].id")
  fi
fi
#echo $name;
#echo $id;
#echo $ip;
#echo $new_ip;
if [[ $ip == $new_ip ]]; then
  echo 'no update needed'
  exit
fi
if [[ $id ]]; then
  echo "mod ip"
  if curl -s -d "login_token=$token&format=json&domain=$domain&record_id=$id&value=$new_ip&record_type=$record_type&record_line_id=0&sub_domain=$name" https://dnsapi.cn/Record.Modify | grep -q '"code":"1"'; then
    echo "sus"
  fi
else
  echo "add ip"
  if curl -s -d "login_token=$token&format=json&domain=$domain&sub_domain=$name&record_type=$record_type&record_line_id=0&value=$new_ip" https://dnsapi.cn/Record.Create | grep -q '"code":"1"'; then
    echo "sus"
  fi
fi

在/lib/netifd/ppp-up文件内调用上面的脚本,当pppoe网络连接成功时会执行此文件,$4变量为pppoe连接的本地IP。

/usr/bin/update-ip.sh name $4 > /dev/null 2>&1 &

pppoe只能传递公网ipv4,使用ifstatus可获取pppoe接口ipv6地址和分配内网的ipv6前缀,根据mac生成的ipv6后缀可为内网其它机器做ddns。

ifstatus wan_6
ifstatus wan
ubus call network.interface dump
jsonfilter -i /tmp/wan6.txt -e '@["ipv6-prefix"][0].address'
jsonfilter -i /tmp/wan6.txt -e '@["ipv6-address"][0].address' 

PHP版本:
https://www.haiyun.me/archives/1186.html

Openwrt下转守护进程runit使用

发布时间:February 11, 2015 // 分类:OpenWrt // No Comments

类似于s6,不过runit在openwrt busybox中自带,需编译openwrt固件时选择。
runsvdir用以扫描指定目录子目录下run脚本并使用runsv启动为守护进程:

runsvdir -P /etc/config/service/

一个openconnect启动脚本示例,注意启动的程序一定要以非守护进程启动且以exec命令执行:

cat /etc/config/service/openconnect/run 
exec 2>&1
exec openconnect -c user-cert.pem -k user-key.pem -s /etc/config/vpnc --no-cert-check www.haiyun.me

如果run目录存在finish脚本,run执行的程序退出后runsv会执行finish并传递程序退出code给finish参数1,然后再重新执行run启动程序,我们可以使用finish判断当网络不通时程序退出状态,避免程序持续启动退出导致死循环:

#!/bin/sh
if [ $1 -eq 255 ]
then
  sleep 5
else
  exit
fi

控制启动的程序,更多见http://smarden.org/runit/sv.8.html

sv status /etc/config/service/openconnect/
sv stop /etc/config/service/openconnect/
sv start /etc/config/service/openconnect/
sv restart /etc/config/service/openconnect/

也可以用runit管理ssh动态隧道转发实现类似于autossh的功能,ssh连接时使用超时控制,超时后ssh关闭runsv会自动重启ssh。
更多示例:
http://smarden.org/runit/runscripts.html

此内容被密码保护

发布时间:February 11, 2015 // 分类:OpenWrt // 1 Comment

请输入密码访问

Openwrt无线管理工具/命令

发布时间:February 9, 2015 // 分类:OpenWrt // No Comments

查看连接Openwrt路由的无线客户端:

iwinfo wlan0 assoclist
iw dev wlan0 station dump

查看分配的DHCP客户端IP:

 cat /tmp/dhcp.leases 

扫描无线路由器/AP:

iwinfo wlan0 scan

查看Openwrt路由无线信息:

iwinfo wlan0 info
wlan0     ESSID: "OpenWrt"
          Access Point: 00:1F:A3:65:55:8E
          Mode: Master  Channel: 5 (2.432 GHz)
          Tx-Power: 20 dBm  Link Quality: 30/70
          Signal: -80 dBm  Noise: unknown
          Bit Rate: 32.5 MBit/s
          Encryption: WPA2 PSK (CCMP)
          Type: nl80211  HW Mode(s): 802.11bg
          Hardware: unknown [Generic MAC80211]
          TX power offset: unknown
          Frequency offset: unknown
          Supports VAPs: no  PHY name: phy0

重启无线:

wifi down && sleep 5 && wifi

一个bash脚本,输出当前无线客户端IP/MAC/名称/速率:

#!/bin/bash
echo -e "# IP address\tname\tMAC address\ttx bit\trx bit"
for interface in `iw dev | grep Interface | cut -f 2 -s -d" "`
do
  maclist=(`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "`)
  txlist=(`iw dev wlan0 station dump|grep 'tx bitrate'|awk '{print $3$4}'`)
  rxlist=(`iw dev wlan0 station dump|grep 'rx bitrate'|awk '{print $3$4}'`)
  len=${#maclist[@]}
  for ((i=0;i<$len;i++))
  do
    mac=${maclist[$i]}
    tx=${txlist[$i]}
    rx=${rxlist[$i]}
    ip="UNKN"
    host=""
    ip=`cat /tmp/dhcp.leases | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 2 -s -d" "`
    host=`cat /tmp/dhcp.leases | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 3 -s -d" "`
    echo -e "$ip\t$host\t$mac\t$tx\t$rx"|awk '{printf "%-15s %-25s %-15s %-10s %-10s\n",$1,$2,$3,$4,$5}'
  done
done

http://wiki.openwrt.org/doc/faq/faq.wireless
http://wiki.openwrt.org/doc/howto/wireless.utilities

编译Openwrt固件安装软件内核版本不一致问题解决

发布时间:February 5, 2015 // 分类:OpenWrt // No Comments

最近将使用近3年Openwrt Backfire 10.03固件升级到了最新版本Barrier Breaker 14.07,编译后使用opkg安装官方源提供的软件出现了内核版本不一致的问题:

Collected errors:
 * satisfy_dependencies_for: Cannot satisfy the following dependencies for openconnect:
 *     kernel (= 3.10.49-1-3051dee8f07064b727e9d57fbfeb05ec) * 
 * opkg_install_cmd: Cannot install package openconnect.

安装的openwrt kernel版本,内核大版本一样,就后面md5不一样:

opkg info kernel
Package: kernel
Version: 3.10.49-1-eac88df3cb49b94d68ac3bc78be57f95
Depends: libc
Status: install hold installed
Architecture: x86
Installed-Time: 1423195057

看看md5版本号是怎么生成的:

grep vermagic include/kernel-defaults.mk
$(SH_FUNC) grep '=[ym]' $(LINUX_DIR)/.config | LC_ALL=C sort | md5s > $(LINUX_DIR)/.vermagic

在编译前指定内核版本号:

#查看Openwrt官方源内核版本:
wget http://downloads.openwrt.org/barrier_breaker/14.07/x86/generic/packages/base/Packages.gz
zgrep -m 1 "Depends: kernel (=.*)$" Packages.gz | sed -e 's/.*-\(.*\))/\1/' > .vermagic
#取而代之
sed -i -e 's/^\(.\).*vermagic$/\1cp $(TOPDIR)\/.vermagic $(LINUX_DIR)\/.vermagic/' include/kernel-defaults.mk

编译后指定内核版本:

sed -i 's/eac88df3cb49b94d68ac3bc78be57f95/3051dee8f07064b727e9d57fbfeb05ec/' /usr/lib/opkg/status

以上解决方法和opkg --force-depends无区别,会造成不稳定因素,最好的解决方法是使用官方提供的固件,如果非要自己编译固件的话使用官方软件相同版本的源码编译,如Barrier Breaker 14.07 r42625:

svn co svn://svn.openwrt.org/openwrt/branches/barrier_breaker -r 42625 

就算使用同一版本源码编译内核md5版本号也不一致,除非使用官方提供的.config,可以使用前面的两种方法修改内核版本号。
Openwrt版本历史及版本号:
http://en.wikipedia.org/wiki/OpenWrt
http://wiki.openwrt.org/about/history#barrier_breaker

分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...