Mac - Generic DMG Installer - JumpCloud
Name
Mac - Generic DMG Installer - JumpCloud
Content-
#!/bin/bash
# *** USAGE *** Version: 1.2
# *NOTE* this template is only designed to work with DMG files and does not support .pkg, .zip files or DMGs that contain .pkg installers.
# Update the DownloadUrl="" variable with the URL of the target .DMG download file. This URL must resolve to a .DMG file directly or point to a URL which has the .DMG file in the curl header Location field.
# The command: 'curl -s --head $DownloadUrl' can be used to query a given URLs header to determine if the Location field points to a .DMG file.
Process -
1. Log in to the JumpCloud Admin Portal.
2. Go to Devices > Commands.
3. Click the "+" to create a new command.
4. Select macOS as the type.
5. Enter a name and the shell script command.
6. Assign it to a device or device group.
7. Click Save.
Command-
DownloadUrl="DOWNLOAD_URL"
### Modify below this line at your own risk!
# Locate DMG Download Link From URL
regex='^https.*.dmg$'
if [[ $DownloadUrl =~ $regex ]]; then
echo "URL points to direct DMG download"
validLink="True"
else
echo "Searching headers for download links"
urlHead=$(curl -s --head "$DownloadUrl")
locationSearch=$(echo "$urlHead" | grep https:)
if [ -n "$locationSearch" ]; then
locationRaw=$(echo "$locationSearch" | awk '{print $2}')
locationFormatted="$(echo "${locationRaw}" | tr -d '[:space:]')"
regex='^https.*'
if [[ $locationFormatted =~ $regex ]]; then
echo "Download link found"
DownloadUrl="$locationFormatted"
else
echo "No https location download link found in headers"
exit 1
fi
else
echo "No location download link found in headers"
exit 1
fi
fi
# Create Temp Folder
DATE=$(date '+%Y-%m-%d-%H-%M-%S')
TempFolder="Download-$DATE"
mkdir -p "/tmp/$TempFolder"
# Navigate to Temp Folder
cd "/tmp/$TempFolder" || exit
# Download File into Temp Folder
curl -s -O "$DownloadUrl"
# Capture name of Download File
DownloadFile="$(ls)"
echo "Downloaded $DownloadFile to /tmp/$TempFolder"
# Verify DMG File
regex='\.dmg$'
if [[ $DownloadFile =~ $regex ]]; then
DMGFile="$DownloadFile"
echo "DMG File Found: $DMGFile"
else
echo "File: $DownloadFile is not a DMG"
rm -r "/tmp/$TempFolder"
echo "Deleted /tmp/$TempFolder"
exit 1
fi
# Mount DMG File (-nobrowse prevents the volume from popping up in Finder)
hdiutilAttach=$(hdiutil attach "/tmp/$TempFolder/$DMGFile" -nobrowse)
echo "Used hdiutil to mount $DMGFile"
err=$?
if [ ${err} -ne 0 ]; then
echo "Could not mount $DMGFile Error: ${err}"
rm -r "/tmp/$TempFolder"
echo "Deleted /tmp/$TempFolder"
exit 1
fi
regex='\/Volumes\/.*'
if [[ $hdiutilAttach =~ $regex ]]; then
DMGVolume="${BASH_REMATCH[0]}"
echo "Located DMG Volume: $DMGVolume"
else
echo "DMG Volume not found"
rm -r "/tmp/$TempFolder"
echo "Deleted /tmp/$TempFolder"
exit 1
fi
# Identify the mount point for the DMG file
DMGMountPoint=$(hdiutil info | grep "$DMGVolume" | awk '{ print $1 }')
echo "Located DMG Mount Point: $DMGMountPoint"
# Capture name of App file
cd "$DMGVolume" || exit
AppName=$(ls | grep '.app')
cd ~ || exit
echo "Located App: $AppName"
# Test to ensure App is not already installed
ExistingSearch=$(find "/Applications/" -name "$AppName" -depth 1)
if [ -n "$ExistingSearch" ]; then
echo "$AppName already present in /Applications folder"
hdiutil detach "$DMGMountPoint"
echo "Used hdiutil to detach $DMGFile from $DMGMountPoint"
rm -r "/tmp/$TempFolder"
echo "Deleted /tmp/$TempFolder"
exit 1
else
echo "$AppName not present in /Applications folder"
fi
DMGAppPath=$(find "$DMGVolume" -name "*.app" -depth 1)
# Copy the contents of the DMG file to /Applications/
# Preserves all file attributes and ACLs
cp -pPR "$DMGAppPath" /Applications/
err=$?
if [ ${err} -ne 0 ]; then
echo "Could not copy $DMGAppPath Error: ${err}"
hdiutil detach "$DMGMountPoint"
echo "Used hdiutil to detach $DMGFile from $DMGMountPoint"
rm -r "/tmp/$TempFolder"
echo "Deleted /tmp/$TempFolder"
exit 1
fi
echo "Copied $DMGAppPath to /Applications"
# Unmount the DMG file
hdiutil detach "$DMGMountPoint"
echo "Used hdiutil to detach $DMGFile from $DMGMountPoint"
err=$?
if [ ${err} -ne 0 ]; then
echo "Could not detach DMG: $DMGMountPoint Error: ${err}"
fi
# Remove Temp Folder and download
rm -r "/tmp/$TempFolder"
echo "Deleted /tmp/$TempFolder"
Examples of some Dmg Download URL's -