Skip to main content
Solved

Java 1.7 Extension Attribute Grep Issue


Forum|alt.badge.img+15

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:

1#!/bin/sh
2
3if [ -e /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java ]; then
4result="$(/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version)"
5echo "$result"
6fi

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

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

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

Any ideas?

Best answer by mcrispin

Got it...

1/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 original
Did this topic help you find an answer to your question?

14 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • October 19, 2012

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"


Forum|alt.badge.img+15
  • Author
  • Valued Contributor
  • 95 replies
  • October 19, 2012

Thank you! so, if I:

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

my output is:

1java 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:

11.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:

1system_profiler SPPrefPaneDataType

But that seems a tad more complicated.

Any thoughts?


Forum|alt.badge.img+15
  • Author
  • Valued Contributor
  • 95 replies
  • Answer
  • October 19, 2012

Got it...

1/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


  • 0 replies
  • October 22, 2012

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.


Forum|alt.badge.img+13
  • Valued Contributor
  • 478 replies
  • January 14, 2013

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:

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

Runtime:

1#!/bin/sh
2#this is to find the current version of java on a machine.
3
4if [ -e /Library/Java/Home ]; then
5 javaVersion=`javac -version 2>&1 | sed 's/.*javac //'`
6 echo "<result>$javaVersion</result>"
7else
8 echo "<result>Not Installed</result>"
9fi
10exit 0

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


Forum|alt.badge.img+7
  • Contributor
  • 28 replies
  • January 14, 2013

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
Forum|alt.badge.img+36
  • Hall of Fame
  • 4293 replies
  • January 14, 2013

Thanks for the EA, just what we needed!

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

rob_potvin
Forum|alt.badge.img+23
  • Employee
  • 209 replies
  • January 16, 2013

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

1/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/
2Home/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

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

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


Forum|alt.badge.img+18

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

1#!/bin/sh
2
3PLUGIN_DIR='/Library/Internet Plug-Ins'
4PLUGIN_NAME='JavaAppletPlugin.plugin'
5
6# Verify plugin exists
7if [ -d "$PLUGIN_DIR/$PLUGIN_NAME" ]; then
8 #echo "Found $PLUGIN_DIR/$PLUGIN_NAME"
9
10 # Verify plugin info plist exists
11 if [ -f "$PLUGIN_DIR/$PLUGIN_NAME/Contents/Info.plist" ]; then
12 #echo "Found $PLUGIN_DIR/$PLUGIN_NAME/Contents/Info.plist"
13 version=`defaults read "$PLUGIN_DIR/$PLUGIN_NAME/Contents/Info" CFBundleVersion`
14 echo "<result>$version</result>"
15 else
16 echo "<result>Plugin Not Directory</result>"
17 fi
18else
19
20 echo "<result>Not Installed</result>"
21fi

Forum|alt.badge.img+13
  • Valued Contributor
  • 478 replies
  • January 16, 2013

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
Forum|alt.badge.img+35
  • Hall of Fame
  • 1799 replies
  • January 16, 2013

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


Forum|alt.badge.img+13
  • Valued Contributor
  • 478 replies
  • January 16, 2013

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
Forum|alt.badge.img+23
  • Employee
  • 209 replies
  • January 16, 2013

@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


Forum|alt.badge.img+13
  • Valued Contributor
  • 478 replies
  • January 16, 2013

@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)


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