Java 1.7 Extension Attribute Grep Issue

mcrispin
Contributor II

I want an extension attribute to tell me the version of the Oracle installed (new) Java JKE 7.

So a little script goes like this:

#!/bin/sh

if [ -e /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java ]; then
result="$(/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version)"
echo "$result"
fi

Problem is -- I can't seem to grep the results, although my output is like this:

java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

I can't output this to a text file either.

Any ideas?

1 ACCEPTED SOLUTION

mcrispin
Contributor II

Got it...

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version 2>&1 | grep "java" | awk '{ print substr($3, 2, length($3)-2); }'

had to play around with some things seen elsewhere in discussions -- thanks for everything - Mike

View solution in original post

14 REPLIES 14

mm2270
Legendary Contributor III

That's a common problem with the java -version command. Try to pipe stdout and sterr to the grep, like so-

2>&1 | grep "regex"

mcrispin
Contributor II

Thank you! so, if I:

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version 2>&1 | grep "java"

my output is:

java version "1.7.0_09"

That's good enough for smart searching -- but I'm not skilled enough at "cut", et. al to get this trimmed down to something like:

1.7.0_09

My other thought was that since the new Java is a preference pane you should also be able to grep the output of:

system_profiler SPPrefPaneDataType

But that seems a tad more complicated.

Any thoughts?

mcrispin
Contributor II

Got it...

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version 2>&1 | grep "java" | awk '{ print substr($3, 2, length($3)-2); }'

had to play around with some things seen elsewhere in discussions -- thanks for everything - Mike

Not applicable

Not entirely sure where I found this, but here's the EA I had running:

#!/bin/bash

#Check if java_home is defined
javaHome=/usr/libexec/java_home 2> /dev/null;

if [ ! -z $javaHome ]
then
vers=java -version 2>&1 | grep "java version" | awk '{print substr($3,2,length($3)-2);}'
echo "<result>$vers</result>"
else
echo "<result>Not Installed</result>"
fi

Then I created an advanced computer search for Java version is not blank, ticked Java version as a display field, and voila.

JPDyson
Valued Contributor

The complicating factor is that you can get differing results based on what you check. It's incredibly common for a machine to report 1.7 when checking the plugin, but 1.6 when checking Java home.

So I look for both in two separate EAs.

Plugin:

#!/bin/bash

if [ -e /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java ]; then
    java_version=`/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version 2>&1 | head -n 1 | cut -d" -f 2`
    echo "<result>$java_version</result>"
else
    echo "<result>Not Installed</result>"
fi
exit 0

Runtime:

#!/bin/sh
#this is to find the current version of java on a machine.

if [ -e /Library/Java/Home ]; then
    javaVersion=`javac -version 2>&1 | sed 's/.*javac //'`
    echo "<result>$javaVersion</result>"
else
    echo "<result>Not Installed</result>"
fi
exit 0

(btw, no really good reason I'm checking javac on the latter - just am)

mzago
New Contributor III

Remember that if you capture plug-ins for inventory you actually already have the Java Internet-Plugin version without an extension attribute.

It's identified by the name: JavaAppletPlugin.plugin
Oracle version numbers are identified by Java 7 Update x (where x is the update). Computers with older Apple Java plug-ins identify with numbers like 14.0 or n/a for older Java installs.

donmontalvo
Esteemed Contributor III

Thanks for the EA, just what we needed!

**$** /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/
Home/bin/java -version 2>&1 | head -n 1 | cut -d" -f 2
1.7.0_11
**$**
--
https://donmontalvo.com

rob_potvin
Contributor III
Contributor III

Not working for me...
Run this from the command line and

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/
Home/bin/java -version 2>&1 | head -n 1 | cut -d" -f 2

I get 1.7.0_11

Here is my extension attribute and I am still not getting anything displayed in that field

#!/bin/sh

if [ -e /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java ]; then
result="$(/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version 2>&1 | grep "java" | awk '{ print substr($3, 2, length($3)-2); }')"
echo "<result>$result</result>"
else
echo "<result>Not installed</result>"
fi

any ideas? Java is installed on the test box where I run this...

charles_hitch
Contributor II

Here's another way and it avoids all the grepping and such...

#!/bin/sh

PLUGIN_DIR='/Library/Internet Plug-Ins'
PLUGIN_NAME='JavaAppletPlugin.plugin'

# Verify plugin exists
if [ -d "$PLUGIN_DIR/$PLUGIN_NAME" ]; then
    #echo "Found $PLUGIN_DIR/$PLUGIN_NAME"

    # Verify plugin info plist exists
    if [ -f "$PLUGIN_DIR/$PLUGIN_NAME/Contents/Info.plist" ]; then
        #echo "Found $PLUGIN_DIR/$PLUGIN_NAME/Contents/Info.plist"
        version=`defaults read "$PLUGIN_DIR/$PLUGIN_NAME/Contents/Info" CFBundleVersion`
        echo "<result>$version</result>"
    else
        echo "<result>Plugin Not Directory</result>"
    fi
else

    echo "<result>Not Installed</result>"
fi

JPDyson
Valued Contributor

Can you clarify what you mean by "not working"? That command is giving you the correct result for what version should be at that location, and your script returns the same value wrapped in result tags.

Remember that EA's are captured during recon. Did you do a recon on the box before checking the EA again?

stevewood
Honored Contributor II
Honored Contributor II

You might also want to take a look at Rich Trouton's post on his blog about Java EAs:

http://derflounder.wordpress.com/2012/10/31/casper-extension-attribute-scripts-to-report-java-browser-plug-in-info/

I'm using his method to find the vendor and the other method he posted from Christoph von Gabler-Sahm to find the version. Both are working as expected for me.

external image link

JPDyson
Valued Contributor

mzago, yes... but the EA is still super handy for reports (try getting a plug-in to show up in inventory search results). Also, that plug-in section in inventory doesn't tell you if it's an early-access build (we've got devs).

rob_potvin
Contributor III
Contributor III

@JPDyson
by not working i mean http://cl.ly/image/1H383Y0Z0r0z nothing is showing up, even when it works on the command line.

@stevewood
Awesome find, thanks will have a look pronto, that is exactly what I want to do

@JPSyson
Don't have devs here just students and a typing programme for kids and math software for the big kids that needs java.

Thanks guys

JPDyson
Valued Contributor

@rpotvin, I don't know what to tell you - I copied your script into an EA on my jss, ran recon on a machine that has Java installed, and the field populated fine.

http://cl.ly/image/2B1i2A05271w
http://cl.ly/image/341H1g3s3Z2U

(note that the text is wrapping in the script field; i just copied and pasted your script exactly as typed above)