瀏覽代碼

feature:crontab: Auto update ipv6 hosts and restart adh

rufengsuixing 5 年之前
父節點
當前提交
1fc36a94ff

+ 1 - 0
luasrc/model/cbi/AdGuardHome/base.lua

@@ -250,6 +250,7 @@ o = s:option(MultiValue, "crontab", translate("Crontab task"),translate("Please
 o:value("autoupdate",translate("Auto update core"))
 o:value("cutquerylog",translate("Auto tail querylog"))
 o:value("cutruntimelog",translate("Auto tail runtime log"))
+o:value("autohost",translate("Auto update ipv6 hosts and restart adh"))
 o.widget = "checkbox"
 o.default = nil
 o.optional=true

+ 2 - 0
po/zh-cn/AdGuardHome.po

@@ -238,3 +238,5 @@ msgstr "计划任务"
 msgid "Please change time and args in crontab"
 msgstr "请在计划任务中修改时间和参数"
 
+msgid "Auto update ipv6 hosts and restart adh"
+msgstr "自动更新ipv6主机并重启adh"

+ 6 - 0
root/etc/init.d/AdGuardHome

@@ -549,6 +549,12 @@ do_crontab(){
 		[ "${crontab//cutruntimelog/}" == "$crontab" ] && enable=0 || enable=1
 		crontab_editor
 	fi
+	
+	findstr="/usr/bin/AdGuardHome/addhost.sh"
+	default="#0 * * * * /usr/bin/AdGuardHome/addhost.sh"
+	[ "${crontab//autohost/}" == "$crontab" ] && enable=0 || enable=1
+	crontab_editor
+	[ "$enable" == "0" ] && /usr/bin/AdGuardHome/addhost.sh del
 }
 crontab_editor(){
 	local line="$(grep "$findstr" $CRON_FILE)"

+ 21 - 0
root/usr/share/AdGuardHome/addhost.sh

@@ -0,0 +1,21 @@
+#!/bin/sh
+[ "$1" == "del" ] && sed -i '/programaddstart/,/programaddend/d' /etc/hosts && exit 0
+/usr/bin/awk 'BEGIN{
+while ((getline < "/tmp/dhcp.leases") > 0)
+{
+    a[$2]=$4;
+}
+while (("ip -6 neighbor show | grep -v fe80" | getline) > 0)
+{
+    if (a[$5]) {print $1" "a[$5] >"/tmp/tmphost"; }
+}}'
+echo "#programaddend" >>/tmp/tmphost
+grep programaddstart /etc/hosts
+if [ "$?" == "0" ]; then
+	sed -i '/programaddstart/,/programaddend/c\#programaddstart' /etc/hosts
+	sed -i '/programaddstart/'r/tmp/tmphost /etc/hosts
+else
+	echo "#programaddstart" >>/etc/hosts
+	cat /tmp/tmphost >> /etc/hosts
+fi
+rm /tmp/tmphost