SHARE
TWEET

Wi-Fi Configuration Profile Creator

MichalMMac Jan 10th, 2014 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # Author: MichalM.Mac
  3. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4.  
  5. # Config
  6. PAYLOAD_SAVE_LOCATION='/Users/Shared'
  7.  
  8. PAYLOAD_ORGANIZATION='cz.osxadmin'
  9. PAYLOAD_VERSION=1
  10.  
  11. PAYLOAD_REMOVAL_DISALLOWED='true'
  12. PROFILE_PASSWORD='password'
  13.  
  14. # CSV:: SSID;password;room_number;room_name
  15. CSV_DELIMITER=','
  16. # Programs
  17. BUDDY='/usr/libexec/PlistBuddy -c'
  18.  
  19. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  20.  
  21.  
  22. error_printer() {
  23.   echo "$@" 1>&2;
  24. }
  25.  
  26. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  27.  
  28. add_payload_removal_password(){ # $1: ProfileIdentifier, $2: Path to .mobileconfig file
  29.  
  30.   PROFILE="$2"
  31.   PASSWORD_PAYLOAD_UUID=$(uuidgen)
  32.  
  33.   # New Dict
  34.   $BUDDY "Add :PayloadContent:1 dict" "$PROFILE"
  35.  
  36.   # Profile Specific
  37.   $BUDDY "Add :PayloadContent:1:RemovalPassword string \"${PROFILE_PASSWORD}\"" "$PROFILE"
  38.  
  39.   # Generated
  40.   $BUDDY "Add :PayloadContent:1:PayloadIdentifier string \"${1}.com.apple.profileRemovalPassword.${PASSWORD_PAYLOAD_UUID}\"" "$PROFILE"
  41.   $BUDDY "Add :PayloadContent:1:PayloadUUID string \"${PASSWORD_PAYLOAD_UUID}\"" "$PROFILE"
  42.   $BUDDY "Add :PayloadContent:1:PayloadVersion integer \"${PAYLOAD_VERSION}\"" "$PROFILE"
  43.  
  44.   # Static
  45.   $BUDDY "Add :PayloadContent:1:PayloadDisplayName string \"Profile Removal\"" "$PROFILE"
  46.   $BUDDY "Add :PayloadContent:1:PayloadType string \"com.apple.profileRemovalPassword\"" "$PROFILE"
  47.   $BUDDY "Add :PayloadContent:1:PayloadDescription string \"Configures a password for profile removal\"" "$PROFILE"
  48.  
  49. }
  50.  
  51. add_payload_wifi(){ # $1: Wi-Fi name, $2: Wi-Fi password, $3: ProfileIdentifier $4: Path to .mobileconfig file
  52.   PROFILE="$4"
  53.   WIFI_PAYLOAD_IDENTIFIER=$(uuidgen)
  54.   WIFI_PAYLOAD_UUID=$(uuidgen)
  55.  
  56.   # New Dict
  57.   $BUDDY "Add :PayloadContent:0 dict" "$PROFILE"  
  58.  
  59.   # Wi-Fi Specific
  60.   $BUDDY "Add :PayloadContent:0:SSID_STR string \"${1}\"" "$PROFILE"
  61.   $BUDDY "Add :PayloadContent:0:Password string \"${2}\"" "$PROFILE"
  62.   $BUDDY "Add :PayloadContent:0:AutoJoin bool true" "$PROFILE"
  63.   $BUDDY "Add :PayloadContent:0:HIDDEN_NETWORK bool false" "$PROFILE"
  64.   $BUDDY "Add :PayloadContent:0:IsHotspot bool false" "$PROFILE"
  65.   $BUDDY "Add :PayloadContent:0:_UsingHotspot20 bool false" "$PROFILE"
  66.   $BUDDY "Add :PayloadContent:0:EncryptionType string \"WPA\"" "$PROFILE"
  67.   $BUDDY "Add :PayloadContent:0:ProxyType string \"None\"" "$PROFILE"
  68.  
  69.   # Generated
  70.   $BUDDY "Add :PayloadContent:0:PayloadIdentifier string \"${3}.com.apple.wifi.managed.${WIFI_PAYLOAD_IDENTIFIER}\"" "$PROFILE"
  71.   $BUDDY "Add :PayloadContent:0:PayloadUUID string \"${WIFI_PAYLOAD_UUID}\"" "$PROFILE"
  72.   $BUDDY "Add :PayloadContent:0:PayloadVersion integer \"${PAYLOAD_VERSION}\"" "$PROFILE"
  73.  
  74.   # Payload Default
  75.   $BUDDY "Add :PayloadContent:0:PayloadDisplayName string \"WiFi ${1}\"" "$PROFILE"
  76.   $BUDDY "Add :PayloadContent:0:PayloadType string \"com.apple.wifi.managed\"" "$PROFILE"
  77.   $BUDDY "Add :PayloadContent:0:PayloadDescription string \"Configures Wi-Fi settings\"" "$PROFILE"
  78. }
  79.  
  80. generate_profile() { # $1: Wi-Fi name, $2: Wi-Fi password, $3: Path to .mobileconfig file
  81.  
  82.   PROFILE="$3"
  83.   PAYLOAD_IDENTIFIER="script.$(uuidgen)"
  84.   PAYLOAD_UUID=$(uuidgen)
  85.  
  86.  
  87.   $BUDDY "Add :PayloadDisplayName string \"Wi-Fi ${1}\"" "$PROFILE"
  88.   $BUDDY "Add :PayloadIdentifier string \"$PAYLOAD_IDENTIFIER\"" "$PROFILE"
  89.   $BUDDY "Add :PayloadOrganization string $PAYLOAD_ORGANIZATION" "$PROFILE"
  90.   $BUDDY "Add :PayloadType string \"Configuration\"" "$PROFILE"
  91.   $BUDDY "Add :PayloadUUID string \"$PAYLOAD_UUID\"" "$PROFILE"
  92.   $BUDDY "Add :PayloadVersion integer \"$PAYLOAD_VERSION\"" "$PROFILE"
  93.  
  94.   #Payload
  95.   $BUDDY "Add :PayloadContent array" "$PROFILE"
  96.   #Payload item 0
  97.   add_payload_wifi "$1" "$2" $PAYLOAD_IDENTIFIER "$PROFILE"
  98.  
  99.   if [ "$PAYLOAD_REMOVAL_DISALLOWED" = "true" ]; then
  100.     $BUDDY "Add :PayloadRemovalDisallowed bool true" "$PROFILE"
  101.    
  102.     if [ ! -z "$PROFILE_PASSWORD" ]; then
  103.       $BUDDY "Add :HasRemovalPasscode bool true" "$PROFILE"
  104.       #Payload item 1
  105.       add_payload_removal_password $PAYLOAD_IDENTIFIER "$PROFILE"
  106.     else
  107.       $BUDDY "Add :HasRemovalPasscode bool false" "$PROFILE"
  108.     fi
  109.    
  110.   else
  111.     $BUDDY "Add :PayloadRemovalDisallowed bool false" "$PROFILE"
  112.   fi
  113.  
  114. }
  115.  
  116. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  117. # Main Body
  118. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  119.  
  120. if [ $# -eq 0 ]; then
  121.   error_printer "Error: No input file. Please add input file with wi-fi network as an argument"
  122.   exit 1
  123. fi  
  124.  
  125. if [ ! -f "$1" ]; then
  126.     echo "Input file not found!"
  127. fi
  128.  
  129. while read LINE; do
  130.   SSID="$(echo $LINE | cut -d${CSV_DELIMITER} -f1)"
  131.   PASSWORD="$(echo $LINE | cut -d${CSV_DELIMITER} -f2)"
  132.   generate_profile "$SSID" "$PASSWORD" "${PAYLOAD_SAVE_LOCATION}/Wi-Fi_${SSID}.mobileconfig"
  133. done < "$1"
RAW Paste Data
Top