eibgrad

ddwrt-dhcp-lease-persist.sh

May 25th, 2019 (edited)
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.41 KB | None | 0 0
  1. #!/bin/sh
  2. #DEBUG=; set -x # comment/uncomment to disable/enable debug mode
  3.  
  4. #          name: ddwrt-dhcp-lease-persist.sh
  5. #       version: 1.0.4, 29-jun-2019, by eibgrad
  6. #       purpose: make dhcp leases persistent w/ external storage (e.g., usb)
  7. #   script type: startup (autostart)
  8. #  installation:
  9. #    1. enable jffs2 (administration->jffs2)
  10. #    2. enable syslogd (services->services->system log)
  11. #    3. use shell (telnet/ssh) to execute one of the following commands:
  12. #         curl -kLs bit.ly/ddwrt-installer|tr -d '\r'|sh -s MxA19W1M startup
  13. #       or
  14. #         wget -qO - bit.ly/ddwrt-installer|tr -d '\r'|sh -s MxA19W1M startup
  15. #    4. modify options using vi editor (optional):
  16. #         vi /jffs/etc/config/ddwrt-dhcp-lease-persist.startup
  17. #    5. reboot
  18. {
  19. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  20.  
  21. # external storage mounting point (default=/opt)
  22. MOUNT="/opt"
  23. #MOUNT="/tmp/mnt/sda1" # (hd0,0), (drive1,partition1)
  24. #MOUNT="/tmp/mnt/sda2" # (hd0,1), (drive1,partition2)
  25.  
  26. # external storage directory (default=/ (root))
  27. DIR="$MOUNT"
  28. #DIR="$MOUNT/ddwrt/system_files" # example of non-root storage
  29.  
  30. # external storage file
  31. FILE="$DIR/dnsmasq.leases"
  32.  
  33. # max time (in secs) to wait for external storage to be mounted (0=infinite)
  34. MAX_WAIT=120
  35.  
  36. # ------------------------------- END OPTIONS -------------------------------- #
  37.  
  38. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  39.  
  40. # start the timer
  41. start_time=$(date +%s)
  42.  
  43. # wait for the external storage to be mounted
  44. while :; do
  45.     # determine if external storage is mounted
  46.     df | grep -Eq "[[:space:]]+$MOUNT\$" && break
  47.  
  48.     if [ $MAX_WAIT -gt 0 ]; then
  49.         # exit upon reaching execution limit
  50.         [ $(($(date +%s) - start_time)) -ge $MAX_WAIT ] && exit 1
  51.     fi
  52.  
  53.     sleep 3
  54. done
  55.  
  56. # wait for dhcp server to appear in process table
  57. while ! pidof dnsmasq >/dev/null 2>&1; do sleep 3; done
  58.  
  59. # stop dhcp server
  60. stopservice dnsmasq && sleep 3
  61.  
  62. # create persistent directory as necessary
  63. [ -d $DIR ] || mkdir -p $DIR
  64.  
  65. # copy dhcp leases (if any) to persistent file
  66. [ -f $FILE ] || cp /tmp/dnsmasq.leases $FILE
  67.  
  68. # force dhcp server to use persistent file
  69. ln -sf $FILE /tmp/dnsmasq.leases
  70.  
  71. # restart dhcp server
  72. restart dnsmasq
  73.  
  74. exit 0
  75.  
  76. } 2>&1 | logger $([ ${DEBUG+x} ] && echo "-p user.debug") \
  77.     -t $(echo $(basename $0) | grep -Eo '^.{0,23}')[$$] &
Add Comment
Please, Sign In to add comment