Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 13.91 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2. declare -x uuidgen="/usr/bin/uuidgen"
  3. declare -x plistbuddy="/usr/libexec/PlistBuddy"
  4.  
  5. declare -x awk="/usr/bin/awk"
  6. declare -x cat="/bin/cat"
  7. declare -x cp="/bin/cp"
  8. declare -x dscl="/usr/bin/dscl"
  9. declare -x defaults="/usr/bin/defaults"
  10. declare -x du="/usr/bin/du"
  11. declare -x id="/usr/bin/id"
  12. declare -x ntpdate="ntpdate"
  13. declare -x scutil="/usr/sbin/scutil"
  14. declare -x perl="/usr/bin/perl"
  15. declare -x basename="/usr/bin/basename"
  16. declare -x date="/bin/date"
  17. declare -x defaults="/usr/bin/defaults"
  18. declare -x dscl="/usr/bin/dscl"
  19. declare -x find="/usr/bin/find"
  20. declare -x groups="/usr/bin/groups"
  21. declare -x ls="/bin/ls"
  22. declare -x mv="/bin/mv"
  23. declare -x rm="/bin/rm "
  24. declare -x rmdir="/bin/rmdir"
  25. declare -x sudo="/usr/bin/sudo"
  26. declare -x serversetup="/System/Library/ServerSetup/serversetup"
  27. declare -x plistbuddy="/usr/libexec/PlistBuddy"
  28.  
  29. # -- Runtime varibles
  30. declare -x REQCMDS="$awk $dscl $deaults $ntpdate $perl $scutil"
  31. declare -x SCRIPT="${0##*/}" ; SCRIPTNAME="${SCRIPT%%\.*}"
  32. declare -x SCRIPTPATH="$0" RUNDIRECTORY="${0%/*}"
  33. declare -x SYSTEMVERSION="/System/Library/CoreServices/SystemVersion.plist"
  34. declare -x OSVER="$("$defaults" read "${SYSTEMVERSION%.plist}" ProductVersion )"
  35. #declare -x CONFIGFILE="${RUNDIRECTORY:?}/${SCRIPTNAME}.conf"
  36. declare -x BUILDVERSION="2009051"
  37.  
  38. [ "$EUID" != 0 ] &&     printf "%s\n" "This script requires root access ($EUID)!" && exit 1
  39.  
  40. # -- Start the script log
  41. # Set to "VERBOSE" for more logging prior to using -v
  42. declare -x LOGLEVEL="NORMAL" SCRIPTLOG="/Library/Logs/${SCRIPT%%\.*}.log"
  43.  
  44. declare -i CURRENT_LOG_SIZE="$("$du" -hm "${SCRIPTLOG:?}" |
  45.                                 "$awk" '/^[0-9]/{print $1;exit}')"
  46.  
  47. if [ ${CURRENT_LOG_SIZE:=0} -gt 50 ] ; then
  48.         "$rm" "$SCRIPTLOG"
  49.         statusMessage "LOGSIZE:$CURRENT_LOG_SIZE, too large removing"
  50. fi
  51.  
  52. exec 2>>"${SCRIPTLOG:?}" # Redirect standard error to log file
  53. # Strip any extention from scriptname and log stderr to script log
  54. if [ -n ${SCRIPTLOG:?"The script log has not been specified"} ] ; then
  55.         printf "%s\n" \
  56. "STARTED:$SCRIPTNAME:EUID:$EUID:$("$date" +%H:%M:%S): Mac OS X $OSVER:BUILD:$BUILDVERSION" >>"${SCRIPTLOG:?}"
  57.         printf "%s\n" "Log file is: ${SCRIPTLOG:?}"
  58. fi
  59.  
  60. statusMessage() { # Status message function with type and now color!
  61. # Requires SCRIPTLOG STATUS_TYPE=1 STATUS_MESSAGE=2
  62.  
  63. declare date="${date:="/bin/date"}"
  64. declare DATE="$("$date" -u "+%Y-%m-%d")"
  65. declare STATUS_TYPE="$1" STATUS_MESSAGE="$2"
  66. if [ "$ENABLECOLOR" = "YES"  ] ; then
  67.         # Background Color
  68.         declare REDBG="41" WHITEBG="47" BLACKBG="40"
  69.         declare YELLOWBG="43" BLUEBG="44" GREENBG="42"
  70.         # Foreground Color
  71.         declare BLACKFG="30" WHITEFG="37" YELLOWFG="33"
  72.         declare BLUEFG="36" REDFG="31"
  73.         declare BOLD="1" NOTBOLD="0"
  74.         declare format='\033[%s;%s;%sm%s\033[0m\n'
  75.         # "Bold" "Background" "Forground" "Status message"
  76.         printf '\033[0m' # Clean up any previous color in the prompt
  77. else
  78.         declare format='%s\n'
  79. fi
  80. # Function only seems to work on intel and higher.
  81. showUIDialog(){
  82. statusMessage header "FUNCTION: #       $FUNCNAME" ; unset EXITVALUE TRY
  83. "$killall" -HUP "System Events" 2>/dev/null
  84. declare -x UIMESSAGE="$1"
  85. "$osascript" <<EOF
  86. try
  87. with timeout of 0.1 seconds
  88.         tell application "System Events"
  89.                 set UIMESSAGE to (system attribute "UIMESSAGE") as string
  90.                 activate
  91.                         display dialog UIMESSAGE with icon 2 giving up after "3600" buttons "Dismiss" default button "Dismiss"
  92.                 end tell
  93.         end timeout
  94. end try
  95. EOF
  96. return 0
  97. } # END showUIDialog()
  98. case "${STATUS_TYPE:?"Error status message with null type"}" in
  99.         progress) \
  100.         [ -n "$LOGLEVEL" ] &&
  101.         printf $format $NOTBOLD $WHITEBG $BLACKFG "PROGRESS:$STATUS_MESSAGE"  ;
  102.         printf "%s\n" "$DATE:PROGRESS: $STATUS_MESSAGE" >> "${SCRIPTLOG:?}" ;;
  103.         # Used for general progress messages, always viewable
  104.        
  105.         notice) \
  106.         printf "%s\n" "$DATE:NOTICE:$STATUS_MESSAGE" >> "${SCRIPTLOG:?}" ;
  107.         [ -n "$LOGLEVEL" ] &&
  108.         printf $format $NOTBOLD $YELLOWBG $BLACKFG "NOTICE  :$STATUS_MESSAGE"  ;;
  109.         # Notifications of non-fatal errors , always viewable
  110.        
  111.         error) \
  112.         printf "%s\n\a" "$DATE:ERROR:$STATUS_MESSAGE" >> "${SCRIPTLOG:?}" ;
  113.         [ -n "$LOGLEVEL" ] &&
  114.         printf $format $NOTBOLD $REDBG $YELLOWFG "ERROR   :$STATUS_MESSAGE"  ;;
  115.         # Errors , always viewable
  116.  
  117.         verbose) \
  118.         printf "%s\n" "$DATE:VERBOSE: $STATUS_MESSAGE" >> "${SCRIPTLOG:?}" ;
  119.         [ "$LOGLEVEL" = "VERBOSE" ] &&
  120.         printf $format $NOTBOLD $WHITEBG $BLACKFG "VERBOSE :$STATUS_MESSAGE" ;;
  121.         # All verbose output
  122.        
  123.         header) \
  124.         [ "$LOGLEVEL" = "VERBOSE" ] &&
  125.         printf $format $NOTBOLD $BLUEBG $BLUEFG "VERBOSE :$STATUS_MESSAGE" ;
  126.         printf "%s\n" "$DATE:PROGRESS: $STATUS_MESSAGE" >> "${SCRIPTLOG:?}" ;;
  127.         # Function and section headers for the script
  128.        
  129.         passed) \
  130.         [ "$LOGLEVEL" = "VERBOSE" ] &&
  131.         printf $format $NOTBOLD $GREENBG $BLACKFG "SANITY  :$STATUS_MESSAGE" ;
  132.         printf "%s\n" "$DATE:SANITY: $STATUS_MESSAGE" >> "${SCRIPTLOG:?}" ;;
  133.         # Sanity checks and "good" information
  134.         graphical) \
  135.         [ "$GUI" = "ENABLED" ] &&
  136.         showUIDialog "$STATUS_MESSAGE" ;;
  137.        
  138. esac
  139. return 0
  140. } # END statusMessage()
  141.  
  142. die() { # die Function
  143. statusMessage header "FUNCTION: #       $FUNCNAME" ; unset EXITVALUE
  144. declare LASTDIETYPE="$1" LAST_MESSAGE="$2" LASTEXIT="$3"
  145. declare LASTDIETYPE="${LASTDIETYPE:="UNTYPED"}"
  146. if [ ${LASTEXIT:="192"} -gt 0 ] ; then
  147.         statusMessage error "$LASTDIETYPE :$LAST_MESSAGE:EXIT:$LASTEXIT"
  148.         # Print specific error message in red
  149. else
  150.         statusMessage verbose "$LASTDIETYPE :$LAST_MESSAGE:EXIT:$LASTEXIT"
  151.         # Print specific error message in white
  152. fi
  153.         statusMessage verbose "COMPLETED:$SCRIPT IN $SECONDS SECONDS"
  154.         "$killall" "System Events"
  155. exit "${LASTEXIT}"      # Exit with last status or 192 if none.
  156. return 1                # Should never get here
  157. } # END die()
  158.  
  159.  
  160. cleanUp() { # -- Clean up of our inportant sessions variables and functions.
  161. statusMessage header "FUNCTION: #       $FUNCNAME" ; unset EXITVALUE
  162. statusMessage verbose "TIME: $SCRIPT ran in $SECONDS seconds"
  163. unset -f ${!check*}
  164. [ "${ENABLECOLOR:-"ENABLECOLOR"}" = "YES"  ] && printf '\033[0m' # Clear Color
  165.  
  166. if [ "$PPID" == 1 ] ; then # LaunchD is always PID 1 in 10.4+
  167.         : # Future LaunchD code
  168. fi
  169. exec 2>&- # Reset the error redirects
  170. return 0
  171. } # END cleanUp()
  172.  
  173. # Check script options
  174. statusMessage header "GETOPTS: Processing script $# options:$@"
  175. # ABOVE: Check to see if we are running as a postflight script,the installer  creates $SCRIPT_NAME
  176. [ $# = 0 ] && statusMessage verbose "No options given"
  177. # If we are not running postflight and no parameters given, print usage to stderr and exit status 1
  178. while getopts vCud:f: SWITCH ; do
  179.         case $SWITCH in
  180.                 v ) export LOGLEVEL="VERBOSE" ;;
  181.                 C ) export ENABLECOLOR="YES" ;;
  182.                 u ) export GUI="ENABLED" ;;
  183.                 d ) export SAVE_DIRECTORY="$OPTARG" ;;
  184.                 f ) export CSV_FILE="$OPTARG" ;;
  185.         esac
  186. done # END getopts
  187.  
  188. checkLineEndings(){
  189.         declare -i FUNCSECONDS="$SECONDS" # Capture start time
  190.         declare FILE_TO_CHECK="$1"
  191.         statusMessage header  "FUNCTION: #      ${FUNCNAME}" ; unset EXITVALUE
  192.         if [ -f "$FILE_TO_CHECK" ] ; then
  193.                 if ! $perl -ne "exit 1 if m/\r\n/;" "$FILE_TO_CHECK" ; then
  194.                         statusMessage notice \
  195.                         "Incorrect line endings detected (probobly due to Mircosoft edit)"
  196.                         statusMessage notice \
  197.                         "Backup: $CSV_FILE.bak"
  198.                         $cp -f "$FILE_TO_CHECK" "$FILE_TO_CHECK".bak
  199.                         statusMessage verbose 'Resetting line endings \r/\n/ to \n'
  200.                         $perl -i -pe 's/\r/\n/g' "$FILE_TO_CHECK"
  201.                 elif ! $perl -ne "exit 1 if m/\r/;" "$FILE_TO_CHECK" ; then
  202.                         statusMessage notice \
  203.                         "Incorrect line endings detected (DOS?) fixing backup: $FILE_TO_CHECK.bak"
  204.                         $cp -f "$FILE_TO_CHECK" "$FILE_TO_CHECK".bak
  205.                         statusMessage verbose 'Resetting line endings \r/\n/'
  206.                         $perl -i -pe 's/\r/\n/g' "$FILE_TO_CHECK"
  207.  
  208.                 fi
  209.         else
  210.                 statusMessage error "File: $FILE_TO_CHECK does not exist"
  211.                 die ERROR "Invalid file specified: $FILE_TO_CHECK"
  212.         fi
  213.         statusMessage verbose "TIME:$FUNCNAME:Took $FUNCTIME seconds to EXIT:$EXITVALUE"
  214. }
  215. checkCommands() { # CHECK_CMDS Required Commands installed check using the REQCMDS varible.
  216. declare -i FUNCSECONDS="$SECONDS" # Capture start time
  217. statusMessage header  "FUNCTION: #      ${FUNCNAME}" ; unset EXITVALUE
  218. declare REQCMDS="$1"
  219. for RQCMD in ${REQCMDS:?} ; do
  220.         if [  -x "$RQCMD" ] ; then
  221.                 statusMessage passed "PASSED: $RQCMD is executable"
  222.         else
  223.         # Export the command Name to the die status message can refernce it"
  224.                 export RQCMD ; return 1
  225.         fi
  226.         done
  227. return 0
  228. declare -i FUNCTIME=$(( ${SECONDS:?} - ${FUNCSECONDS:?} ))
  229. [ "${FUNCTIME:?}" != 0 ] &&
  230. statusMessage verbose "TIME:$FUNCNAME:Took $FUNCTIME seconds to EXIT:$EXITVALUE"
  231. } # END checkCommands()
  232.  
  233.  
  234. checkSystemVersion() {
  235. # CHECK_OS Read the /Sys*/Lib*/CoreSer*/S*Version.plist value for OS version
  236. statusMessage header "FUNCTION: #       ${FUNCNAME}" ; unset EXITVALUE
  237. declare OSVER="$1"
  238. case "${OSVER:?}" in
  239.         10.0* | 10.1* | 10.2* | 10.3*) \
  240.         die ERROR "$FUNCNAME: Unsupported OS version: $OSVER." 192 ;;
  241.  
  242.         10.4*) \
  243.         statusMessage passed "CHECK_OS: OS check: $OSVER successful!";
  244.         return 0;;
  245.  
  246.         10.5*) \
  247.         statusMessage passed "CHECK_OS: OS check: $OSVER successful!";
  248.         return 0;;
  249.        
  250.         10.6*) \
  251.         die ERROR "$FUNCNAME:$LINENO Unsupported OS:$OSVER is too new." 192 ;;
  252.  
  253.         *) \
  254.         die ERROR "CHECK_OS:$LINENO Unsupported OS:$OSVER unknown error" 192 ;;
  255. esac
  256. return 1
  257. checkDNS(){
  258.         declare SERVER_HOSTNAME="$1" SERVER_IP="$2"
  259.         statusMessage header "$SERVER_HOSTNAME ($SERVER_IP)"
  260.         if host "${SERVER_HOSTNAME:-null}" &>/dev/null ; then
  261.         statusMessage passed "FORWARD[A]:$SERVER_HOSTNAME ($SERVER_IP)"
  262.         else
  263.         statusMessage error "FORWARD[A]:$SERVER_HOSTNAME ($SERVER_IP)"
  264.         fi
  265.         if host "${SERVER_IP:-null}" &>/dev/null ; then
  266.         statusMessage passed "REVERSE[PTR]:$SERVER_HOSTNAME ($SERVER_IP)"      
  267.         else
  268.         statusMessage error "REVERSE[PTR]:$SERVER_HOSTNAME ($SERVER_IP)"
  269.         fi
  270.         printf "\n"
  271. }
  272.  
  273. } # END checkSystemVersion()
  274. statusMessage verbose "Using file: $CSV_FILE"
  275. checkLineEndings "$CSV_FILE"
  276. declare -x PLIST="${SAVE_DIRECTORY:="."}/${CSV_FILE%%.csv}.plist"
  277.  
  278. $plistbuddy -c "Add :listName string ${CSV_FILE%%.csv}" "$PLIST"
  279. $plistbuddy -c "Add :uuid string $($uuidgen)" "$PLIST"
  280.  
  281. OLDIFS="$IFS"
  282. IFS=$'\n'
  283. declare -ix N=0
  284. for LINE in `$cat "${CSV_FILE:?"Must specify a file with -f"}"` ; do
  285.         let LINE_NUM++
  286.         if  [ $LINE_NUM -eq 1 ] ; then
  287.                 statusMessage progress "Skipping first line:$LINE"
  288.                 continue
  289.         fi
  290.         # statusMessage verbose "processing $LINE"
  291.         declare -x SITE_ID="$(printf "$LINE" | $awk -F',' '{print $1;exit}')"
  292.  
  293.         declare -x SERVER_ETHERNET[0]="$(printf "$LINE" | $awk -F',' '{print $12;exit}')"
  294.         declare -x SERVER_ETHERNET[1]="$(printf "$LINE" | $awk -F',' '{print $13;exit}')"
  295.         declare -x SERVER_HW_SERIAL="$(printf "$LINE" | $awk -F',' '{print substr($15,1,8);exit}')"
  296.         # AdminUser
  297.         declare -x ADMIN_USER_EXISTS="false"
  298.         declare -x ADMIN_USER_NAME="$(printf "$LINE" | $awk -F',' '{print $16;exit}')"
  299.         declare -x ADMIN_USER_PASSWORD="$(printf "$LINE" | $awk -F',' '{print $17;exit}')"
  300.         declare -x ADMIN_USER_REALNAME="$ADMIN_USER_NAME"
  301.         declare -ix ADMIN_USER_UID="501"
  302.        
  303.         # HostName
  304.         declare -x HOST_NAME="$(printf "$LINE" | $awk -F',' '{print $4;exit}')"
  305.         statusMessage verbose "HOST_NAME: $HOST_NAME"
  306.         # Bonjour
  307.         declare -x BONJOUR_ENABLED="true"
  308.         declare -x BONJOUR_NAME="$(printf $HOST_NAME | $awk -F'.' '{print $1;exit}')"
  309.  
  310.         # ComputerName
  311.         declare -x COMPUTER_NAME="$(printf $HOST_NAME | $awk -F'.' '{print $1;exit}')"
  312.  
  313.         # DS
  314.         declare -x DS_TYPE="Standalone"
  315.  
  316.         # DefaultGroupName
  317.         declare -x DEFAULT_GROUP_LONGNAME="Work Group"
  318.         declare -x DEFAULT_GROUP_SHORTNAME="workgroup"
  319.  
  320.         # EnableARD
  321.         declare -x ENABLE_ARD="true"
  322.  
  323.  
  324.  
  325.         # InstallLanguage
  326.         declare -x INSTALL_LAUNGUAGE="English"
  327.  
  328.         # Keyboard
  329.         declare -x KEYBOARD_DEFAULT_FORMAT="0"
  330.         declare -x KEYBOARD_DEFAULT_SCRIPT="0"
  331.         declare -x KEYBOARD_RESNAME="U.S."
  332.         declare -ix KEYBOARD_SCRIPTID="0"
  333.         declare -ix KEYBOARD_KBRESID="0"
  334.  
  335.         # NetworkInterfaces
  336.         declare -ax INTERFACE_ACTIVEAT[0]="false"
  337.         declare -ax INTERFACE_ACTIVETCPIP[0]="true"
  338.         declare -ax DNS_DOMAIN[0]="$(printf "$LINE" | $awk -F',' '{print $11;exit}')"
  339.         declare -ax DNS_SERVER_PRIMARY[0]="$(printf "$LINE" | $awk -F',' '{print $8;exit}')"
  340.         declare -ax DNS_SERVER_SECONDARY[0]="$(printf "$LINE" | $awk -F',' '{print $9;exit}')"
  341.         declare -ax DNS_SERVER_TERIARY[0]="$(printf "$LINE" | $awk -F',' '{print $10;exit}')"
  342.         declare -ax INTERFACE_DEVICENAME[0]="en0"
  343.         declare -ax ETHERNET_ADDRESS[0]="00:00:00:00:00:00"
  344.         declare -ax IPV6_TYPE[0]="3"
  345.         declare -ax PORT_NAME[0]="Ethernet 1"
  346.         declare -ax IP_ADDRESS[0]="$(printf "$LINE" | $awk -F',' '{print $5;exit}')"
  347.         declare -ax ROUTER[0]="$(printf "$LINE" | $awk -F',' '{print $7;exit}')"
  348.         declare -ax SUBNET_MASK[0]="$(printf "$LINE" | $awk -F',' '{print $6;exit}')"
  349.         declare -ax INTERFACE_TYPE[0]="Manual Configuration"
  350.  
  351.         declare -ax INTERFACE_ACTIVEAT[1]="false"
  352.         declare -ax INTERFACE_ACTIVETCPIP[1]="false"
  353.         declare -ax INTERFACE_DEVICENAME[1]="en1"
  354.         declare -ax ETHERNET_ADDRESS[1]="00:00:00:00:00:00"
  355.         declare -ax IPV6_TYPE[1]="3"
  356.         declare -ax PORT_NAME[1]="Ethernet 2"
  357.         declare -ax IP_ADDRESS[1]="192.168.50.90"
  358.         declare -ax ROUTER[1]="0.0.0.0"
  359.         declare -ax SUBNET_MASK[1]="255.255.255.0"
  360.         declare -ax INTERFACE_TYPE[1]="Manual Configuration"
  361.  
  362.         # PrimaryLanguage
  363.         declare -x PRIMARY_LANGUAGE="English"
  364.         # Serial Number
  365.         declare -x SERIAL_NUMBER="$(printf "$LINE" | $awk -F',' '{print $14;exit}')"
  366.         # Service NTP
  367.         declare -x HOST_NTP="false"
  368.         declare -x HOST_NTP_SERVER="$(printf "$LINE" | $awk -F',' '{print $21;exit}')"
  369.         declare -x USE_NTP="true"
  370.         # TimeZone
  371.         declare -x TIME_ZONE="$(printf "$LINE" | $awk -F',' '{print $20;exit}')"
  372.         # Version Number
  373.         declare -ix VERSION_NUMBER="3"
  374.         declare SERVER_MAC="$(printf "%s" "${SERVER_ETHERNET[0]}" | $awk 'BEGIN{FS=""}{print $1$2":"$3$4":"$5$6":"$7$8":"$9$10":"$11$12}')"
  375.         $plistbuddy -c "Add :items array" "$PLIST"
  376.                 $plistbuddy -c "Add :items: dict" "$PLIST"
  377.                                 $plistbuddy -c "Add :items:$N::hardwareAddress string $SERVER_MAC" "$PLIST"
  378.                                 $plistbuddy -c "Add :items:$N::hostname string $HOST_NAME" "$PLIST"
  379.                                 $plistbuddy -c "Add :items:$N::name string $HOST_NAME" "$PLIST"
  380.                                 $plistbuddy -c "Add :items:$N::networkAddress string ${IP_ADDRESS[0]}" "$PLIST"
  381.                                 $plistbuddy -c "Add :items:$N::preferHostname bool false" "$PLIST"
  382.         let N++
  383. done
  384. OLDIFS="$IFS"