addhost.sh 973 B

123456789101112131415161718192021222324252627282930313233
  1. #!/bin/sh
  2. checkmd5(){
  3. local nowmd5=$(md5sum /etc/hosts)
  4. nowmd5=${nowmd5%% *}
  5. local lastmd5=$(uci get AdGuardHome.AdGuardHome.hostsmd5 2>/dev/null)
  6. if [ "$nowmd5" != "$lastmd5" ]; then
  7. uci get AdGuardHome.AdGuardHome.hostsmd5="$nowmd5"
  8. uci commit AdGuardHome
  9. [ "$1" == "noreload" ] || /etc/init.d/AdGuardHome reload
  10. fi
  11. }
  12. [ "$1" == "del" ] && sed -i '/programaddstart/,/programaddend/d' /etc/hosts && checkmd5 "$2" && exit 0
  13. /usr/bin/awk 'BEGIN{
  14. while ((getline < "/tmp/dhcp.leases") > 0)
  15. {
  16. a[$2]=$4;
  17. }
  18. while (("ip -6 neighbor show | grep -v fe80" | getline) > 0)
  19. {
  20. if (a[$5]) {print $1" "a[$5] >"/tmp/tmphost"; }
  21. }
  22. print "#programaddend" >"/tmp/tmphost";
  23. }'
  24. grep programaddstart /etc/hosts >/dev/null 2>&1
  25. if [ "$?" == "0" ]; then
  26. sed -i '/programaddstart/,/programaddend/c\#programaddstart' /etc/hosts
  27. sed -i '/programaddstart/'r/tmp/tmphost /etc/hosts
  28. else
  29. echo "#programaddstart" >>/etc/hosts
  30. cat /tmp/tmphost >> /etc/hosts
  31. fi
  32. rm /tmp/tmphost
  33. checkmd5 "$2"