1#!/bin/bash
2
3#############################################################################################################
4## Information ##
5#############################################################################################################
6## Created by Clay Nelson on 2/15/19 ##
7## Sources: ##
8## Shannon Johnson - https://www.jamf.com/jamf-nation/discussions/29555/script-to-install-update-java ##
9## Lewis Lebentz - https://lew.im/2017/03/auto-update-chrome/ ##
10## Joe Farage - https://www.jamf.com/jamf-nation/third-party-products/files/764/firefox-install-update ##
11#############################################################################################################
12
13# Variables
14 dmgfile="javaUpdate.dmg"
15 logfile="/Library/Logs/JavaInstallScript.log"
16
17# Get the latest version number of Java available.
18 latestver=`/usr/bin/curl -s http://javadl-esd-secure.oracle.com/update/baseline.version | grep "1.8.0"`
19 latestJavaVer=`echo "$latestver" | awk -F "." '{print $2}'`
20 latestMinorVer=`echo "$latestver" | awk -F "_" '{print $2}'`
21 /bin/echo "Latest Version is: $latestver"
22 /bin/echo "`date`: Latest Version is: $latestver" >> ${logfile}
23
24# Get the version number of the currently-installed Java, if any.
25 if [ -e "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin" ]; then
26 currentInstalledVer=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/deploy/JavaControlPanel.prefPane/Contents/Info.plist" CFBundleShortVersionString`
27 /bin/echo "Current installed version is: $currentInstalledVer"
28 /bin/echo "`date`: Current installed version is: $currentInstalledVer" >> ${logfile}
29 currentJavaVer=`echo "$currentInstalledVer" | awk -F "." '{print $2}'`
30 currentMinorVer=`echo "$currentInstalledVer" | awk -F "_" '{print $2}'`
31 if [ ${latestver} = ${currentInstalledVer} ]; then
32 /bin/echo "Java is already up to date, running Java ${currentJavaVer} Update ${currentMinorVer}."
33 /bin/echo "`date`: Java is already up to date, running Java ${currentJavaVer} Update ${currentMinorVer}." >> ${logfile}
34 /bin/echo "--" >> ${logfile}
35 exit 0
36 fi
37 else
38 currentInstalledVer="none"
39 /bin/echo "Java is not installed"
40 /bin/echo "`date`: Java is not installed" >> ${logfile}
41 fi
42
43# Getting the latest version of the url for Java download
44 latestSite=`/usr/bin/curl -s https://www.oracle.com/technetwork/java/javase/downloads/index.html | grep "/technetwork/java/javase/downloads/jre8-downloads-" | awk -F '"' '{print $4}'`
45 url=`/usr/bin/curl -s https://www.oracle.com${latestSite} | grep "macosx-x64.dmg" | grep "${latestJavaVer}u${latestMinorVer}" | awk -F, '{print $3}' | awk -F '"' '{print $4}'`
46
47# Downloading and Installing
48 /bin/echo "Latest version of the URL is: $url"
49 /bin/echo "`date`: Download URL: $url" >> ${logfile}
50
51 /bin/echo "Downloading latest version..."
52 /bin/echo "`date`: Downloading latest version." >> ${logfile}
53 /usr/bin/curl -s -j -k -L -H "Cookie: oraclelicense=accept-securebackup-cookie" ${url} > /tmp/${dmgfile}
54
55 /bin/echo "Mounting installer disk image..."
56 /bin/echo "`date`: Mounting installer disk image." >> ${logfile}
57 /usr/bin/hdiutil attach /tmp/${dmgfile} -nobrowse -quiet
58
59 /bin/echo "Installing..."
60 /bin/echo "`date`: Installing..." >> ${logfile}
61 sudo installer -pkg /Volumes/Java ${latestJavaVer} Update ${latestMinorVer}/Java ${latestJavaVer} Update ${latestMinorVer}.app/Contents/Resources/JavaAppletPlugin.pkg -target /
62 /bin/sleep 10
63
64 /bin/echo "Unmounting installer disk image..."
65 /bin/echo "`date`: Unmounting installer disk image." >> ${logfile}
66 /usr/bin/hdiutil detach $(/bin/df | /usr/bin/grep "Java" | awk '{print $1}') -quiet
67
68 /bin/sleep 10
69
70 /bin/echo "Deleting disk image..."
71 /bin/echo "`date`: Deleting disk image." >> ${logfile}
72 /bin/rm /tmp/${dmgfile}
73
74# Double check to see if the new version got updated
75 newlyInstalledVer=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/deploy/JavaControlPanel.prefPane/Contents/Info.plist" CFBundleShortVersionString`
76 newlyInstalledJavaVer=`/bin/echo "${newlyInstalledVer}" | awk -F "." '{print $2}'`
77 newlyInstalledMinorVer=`/bin/echo "${newlyInstalledVer}" | awk -F "_" '{print $2}'`
78 if [ "${latestver}" = "${newlyInstalledVer}" ]; then
79 /bin/echo "SUCCESS: Java has been updated to: Java ${newlyInstalledJavaVer} Update ${newlyInstalledMinorVer}."
80 /bin/echo "`date`: SUCCESS: Java has been updated to: Java ${newlyInstalledJavaVer} Update ${newlyInstalledMinorVer}." >> ${logfile}
81 /bin/echo "--" >> ${logfile}
82 else
83 /bin/echo "ERROR: Java update unsuccessful, version remains at: Java ${currentJavaVer} Update ${currentMinorVer}."
84 /bin/echo "`date`: ERROR: Java update unsuccessful, version remains at: Java ${currentJavaVer} Update ${currentMinorVer}." >> ${logfile}
85 /bin/echo "--" >> ${logfile}
86 exit 1
87 fi
88
89 exit 0