addhost.sh 609 B

123456789101112131415161718192021
  1. #!/bin/sh
  2. [ "$1" == "del" ] && sed -i '/programaddstart/,/programaddend/d' /etc/hosts && exit 0
  3. /usr/bin/awk 'BEGIN{
  4. while ((getline < "/tmp/dhcp.leases") > 0)
  5. {
  6. a[$2]=$4;
  7. }
  8. while (("ip -6 neighbor show | grep -v fe80" | getline) > 0)
  9. {
  10. if (a[$5]) {print $1" "a[$5] >"/tmp/tmphost"; }
  11. }}'
  12. echo "#programaddend" >>/tmp/tmphost
  13. grep programaddstart /etc/hosts
  14. if [ "$?" == "0" ]; then
  15. sed -i '/programaddstart/,/programaddend/c\#programaddstart' /etc/hosts
  16. sed -i '/programaddstart/'r/tmp/tmphost /etc/hosts
  17. else
  18. echo "#programaddstart" >>/etc/hosts
  19. cat /tmp/tmphost >> /etc/hosts
  20. fi
  21. rm /tmp/tmphost