addhost.sh 943 B

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