Skip to main content
Question

Autoupdate Java by shell


Show first post

34 replies

Forum|alt.badge.img+1
  • New Contributor
  • 2 replies
  • February 22, 2017

Great script!

I've made the following modifications to my copy of the script.

The main reason was to only download the manual.jsp file once, but also make it work on clients that hasn't been updated in a long time (and therefore only have two letters for the currentvermin string).

#!/bin/sh

# https://www.jamf.com/jamf-nation/discussions/17222/autoupdate-java-by-shell

# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# Download the jsp file for extrating version and url for the dmg
oracle_jsp=/tmp/oracle_manual.jsp
curl -L http://www.java.com/en/download/manual.jsp > $oracle_jsp

# check if newer version exists
plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist"
if [ -f "$plugin" ]
then
    currentver=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info.plist" CFBundleShortVersionString`
    echo " Current version is $currentver"
    currentvermain=`echo "$currentver" | awk '{print$2}'`
    echo "Installed main version is $currentvermain"
    currentvermin=`echo "$currentver" | awk '{print$4}'`
    echo "Installed minor version is $currentvermin"
    onlineversionmain=`cat < $oracle_jsp | grep "Recommended Version" | awk '{ print $4}'`
    echo "Online main: $onlineversionmain"
    onlineversionmin=`cat < $oracle_jsp | grep "Recommended Version" | awk '{ print $6}' | awk -F "<" '{ print $1}'`
    echo "Online minor: $onlineversionmin"
    if [ -z "${currentvermain}" ] || [ "${onlineversionmain}" -gt "${currentvermain}" ]
    then
        echo "Let's install Java! Main online version is higher than installed version."
        installjava=1
    fi
    if [ "${onlineversionmain}" = "${currentvermain}" ] && [ "${onlineversionmin}" -gt "${currentvermin}" ]
    then
        echo "Let's install Java! Main online version is equal than installed version, but minor version is higher."
        installjava=1
    fi
    if [ "${onlineversionmain}" = "${currentvermain}" ] && [ "${onlineversionmin}" = "${currentvermin}" ]
    then
        echo "Java is up-to-date!"
    fi
else
    echo "No java installed, let's install"
    installjava=1
fi


# Find Download URL
fileURL=`cat < $oracle_jsp | grep "Download Java for Mac OS X" | head -1 | awk -F """ '{ print $4}'`


# Specify name of downloaded disk image

java_eight_dmg="/tmp/java_eight.dmg"

if [[ ${osvers} -lt 7 ]]; then
  echo "Oracle Java 8 is not available for Mac OS X 10.6.8 or earlier."
  exit 0
fi

if [ "$installjava" = 1 ]
then
    echo "Start installing Java"
    if [[ ${osvers} -ge 7 ]]; then

        # Download the latest Oracle Java 8 software disk image

        /usr/bin/curl --retry 3 -Lo "$java_eight_dmg" "$fileURL"

        # Specify a /tmp/java_eight.XXXX mountpoint for the disk image

        TMPMOUNT=`/usr/bin/mktemp -d /Volumes/java_eight.XXXX`

        # Mount the latest Oracle Java disk image to /tmp/java_eight.XXXX mountpoint

        hdiutil attach "$java_eight_dmg" -mountpoint "$TMPMOUNT" -nobrowse -noverify -noautoopen

        # Install Oracle Java 8 from the installer package.

        if [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.pkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.pkg`
        elif [[ -e "$(/usr/bin/find $TMPMOUNT -name *Java*.mpkg)" ]]; then    
            pkg_path=`/usr/bin/find $TMPMOUNT -name *Java*.mpkg`
        fi

        # Before installation, the installer's developer certificate is checked 

        if [[ "${pkg_path}" != "" ]]; then
                echo "installing Java from ${pkg_path}..."
                /usr/sbin/installer -dumplog -verbose -pkg "${pkg_path}" -target "/" > /dev/null 2>&1

        fi

        # Clean-up

        # Unmount the disk image from /tmp/java_eight.XXXX

        /usr/bin/hdiutil detach -force "$TMPMOUNT"

        # Remove the /tmp/java_eight.XXXX mountpoint

        /bin/rm -rf "$TMPMOUNT"

        # Remove the downloaded disk image

        /bin/rm -rf "$java_eight_dmg"

        # Remove xml file
        /bin/rm -rf /tmp/au-1.8.0_20.xml
    fi
fi
# Remove the jsp file
rm $oracle_jsp

edit: added the " | head -1 " suggested by @jpiperbe, since this seems to still be the latest version of the script


jpiperbe
Forum|alt.badge.img+2
  • New Contributor
  • 4 replies
  • March 3, 2017

@pmex I had to add a

| head -1

between the grep and awk on line 49, for some reason the jsp had "Download for Mac OS X" twice so there were two URLS. (May have been a recent change) but other than that, worked great.


ImAMacGuy
Forum|alt.badge.img+23
  • Esteemed Contributor
  • 1310 replies
  • March 3, 2017

is this for Java or the JDK?


Forum|alt.badge.img+1
  • New Contributor
  • 2 replies
  • April 24, 2017

@jpiperbe I fond that out as well. Unfourtunately I didn't see your post until I went here today to correct myself ;-)


Forum|alt.badge.img+8
  • Contributor
  • 119 replies
  • May 2, 2017

I am running the latest version of the script (2/22/17 at 8:53 AM by @pmex ) with @jpiperbe 's | head -1 fix.

The script runs, and I am pretty sure it's installing, as the next time I run, it shows java installed as 8 U 131, which matches the latest version, so it quits. However, when I go into Terminal, and type java --version, it says I have no JRE.

So, looking at the script, it seems like this is updating the Internet Plug-in. Indeed, Safari sees and recognizes it.

So, how can I modify this script to install the actual JRE? We use PowerSchool, and the gradebook requires a JRE.

Thanks for this great script!


Forum|alt.badge.img+1
  • New Contributor
  • 2 replies
  • September 15, 2017

@JayDuff Its been a while since you asked the question, but here it goes anyways. The results you are getting from your script seem to be accurate. Running java --version in terminal look for the JSE and will try to install if its not found. This script is dealing with Java JRE, not JSE.

You can check the current version via System Preferences or Safari > Preferences > Websites > Plugins.


Forum|alt.badge.img+3
  • New Contributor
  • 3 replies
  • January 23, 2018

Thank you for this script. I've been using for several months now and it has been a huge help with updating/installing the Java JRE in our environment.

I am running into a (hopefully) simple issue, however. The script is running flawlessly, as written, except it doesn't currently update to the current Patch Management version. Currently, the JRE version in Patch Management in the JSS is 8u162, but the manual download page is currently recommending 8u161. Running the script does upgrade all of our users to 8u161, but we still show are 0% patched since Patch Management is looking for 8u162.

We rely on the Patch Management reports to generate our monthly reports. These reports are presented to upper management, so I'd like to find a way to not need to document exceptions each month.

Are there others who are experiencing this? How have you solved the issue?


Forum|alt.badge.img+10
  • Contributor
  • 39 replies
  • February 28, 2018

@dmuncey I have seen the same issue and it is still happening now, a month after your post. I am not sure why this is happening the newest version released by Oracle is 161.


Forum|alt.badge.img+11
  • Employee
  • 40 replies
  • February 28, 2018

Hello @dmuncey and @techjason ,

We recently released two Software Titles specifically for the CPU release of Java 8. CPU meaning critical patch update and that it follows Oracle's recommended security update path.

The Software titles are named: Java SE Runtime Environment JRE8 CPU
Java SE Development Kit 8 CPU

The other Software Titles contain both the CPU and PSU updates and that's why it shows the PSU version of .162 as the latest. The above mentioned will show .161 as the latest.

For more information about the difference between PSU and CPU versions please reference: http://www.oracle.com/technetwork/java/javase/cpu-psu-explained-2331472.html

Hope this helps!

Jonny


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings