home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / network / serial < prev    next >
Text File  |  2016-03-07  |  2KB  |  61 lines

  1. #!/bin/sh
  2. ## This is a repaired version of a script offered in 2009 by a poster named 
  3. ## 'clinty' to forums.freebsd.org .  Accordingly, copyright is his/hers, 
  4. ## but the author never stated any licence terms.
  5.  
  6. # vars
  7. #ZONEPATH=/etc/bind
  8.  
  9. # check args
  10. if [ ! $# -eq 1 ]; then
  11.         echo "Usage $0 domain.tld"
  12.         exit 1
  13. fi
  14.  
  15. # read zonefile from cmd
  16. ZONE=${ZONEPATH}/${1}
  17.  
  18. # check the file
  19. if [ ! -f ${ZONE} ]; then
  20.         echo "Error: zone does not exist"
  21.         exit 1
  22. fi
  23.  
  24. # get serial number
  25. SERIAL=$(grep -e "2[0-9]\{3\}[0-1]\{1\}[0-9]\{1\}[0-3]\{1\}[0-9]\{1\}[0-9]\{2\}" ${ZONE} | awk '{print $1}')
  26.  
  27. # get serial number's date and number
  28. SERIAL_DATE=$(echo ${SERIAL} | cut -b 1-8)
  29. SERIAL_NUMBER=$(echo ${SERIAL} | cut -b 9-10)
  30.  
  31. # get today's date in same style
  32. DATE_TODAY=$(date +%Y%m%d)
  33.  
  34. # compare date and serial date
  35. if [ "${SERIAL_DATE}" = "${DATE_TODAY}" ]
  36. then
  37.         # if equal, just add 1
  38.         # if equal and number equal 99, do not change
  39.         if [ ${SERIAL_NUMBER} -ge 99 ]; then
  40.                 NEWSERIAL=${SERIAL}
  41.         else
  42.                 # increment serial number
  43.                 NEWSERIAL_NUMBER=$(expr $SERIAL_NUMBER + 1)
  44.                 # if < 10, add a 0 to have 2 digits
  45.                 if [ ${NEWSERIAL_NUMBER} -le 9 ]; then
  46.                         NEWSERIAL_NUMBER="0"${NEWSERIAL_NUMBER}
  47.                 fi
  48.                 # construct new serial
  49.                 NEWSERIAL=${SERIAL_DATE}${NEWSERIAL_NUMBER}
  50.         fi
  51. else
  52.         # if not equal, make a new one and add 00
  53.         NEWSERIAL=$(echo ${DATE_TODAY}"00")
  54. fi
  55.  
  56. # write the new serial
  57. sed -i -e "s/${SERIAL}/${NEWSERIAL}/g" ${ZONE}
  58.  
  59. #ok
  60. exit 0
  61.