Skip to main content
Question

Recon User Names

  • June 13, 2012
  • 11 replies
  • 69 views

Forum|alt.badge.img+6

Hi,

We use the following script

#!/bin/sh

/usr/sbin/jamf recon -endUsername $3

to collect the username on a recon. The recon runs at login and at 12pm. Our users are all local users. I can see the machines are being reconned, but the username is wrong. Is their a better way to do this in v8.52? This was written for v6.

Best wishes

Michael

11 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • June 13, 2012

The $3 variable will only work at login and a few other times, not using a regular recon, which may be why you're getting incorrect names. You mentioned running it at 12 noon in addition to login.

Try getting the current logged in user like this instead-

/usr/bin/who | awk '/console/{ print $1 }'


Forum|alt.badge.img+24
  • Valued Contributor
  • June 13, 2012

or

ls -la /dev/console | cut -d " " -f 4

bentoms
Forum|alt.badge.img+35
  • Hall of Fame
  • June 13, 2012

Forum|alt.badge.img+6
  • Author
  • Contributor
  • February 13, 2013

Hi,

I finally got round to trying this and tried

/usr/bin/who | awk '/console/{ print $1 }'

in a script and it didn't work for me. I tried to use Casper Remote as well, but that still didn't return a username.

I liked Jared cli, but I wasn't sure how you would script that to put the answer back in the JSS.

Best wishes

Michael


Forum|alt.badge.img+24
  • Valued Contributor
  • February 13, 2013

What's the endgame here, filling in user info in the Location tab?


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • February 13, 2013

You need to define a variable in the script prior to running the recon command and then use that variable instead of $3. In other words, using Jared's suggestion:

#!/bin/sh

loggedInUser=$( ls -la /dev/console | cut -d " " -f 4 )

/usr/sbin/jamf recon -endUsername $loggedInUser

If you wanted to use the above in Casper Remote without uploading a script, you'd need combine it all into one line for the Run Command field, like this:

/usr/sbin/jamf recon -endUsername $( ls -la /dev/console | cut -d " " -f 4 )

Forum|alt.badge.img+6
  • Author
  • Contributor
  • February 14, 2013

Worked like a charm, thank you.

Cheers

Michael


Forum|alt.badge.img+11
  • New Contributor
  • July 21, 2015

My favorite way to get the owner of /dev/console is to use the stat(1) command, like this:

loggedInUsername="$(/usr/bin/stat -f%Su /dev/console)"

This should return the owner of /dev/console, the username of the person currently logged into the GUI, or "root", if no one is logged in.

/usr/sbin/jamf recon -endUserName "$(stat -f%Su /dev/console)"

Also, to get the user id instead of the user name, use

loggedInUserID="$(/usr/bin/stat -f%u /dev/console)"

-Florin


  • August 18, 2015

@Fveja just noticed that with the jamf binary version 9.65, recon's flag is -endUsername not -endUserName.


Forum|alt.badge.img+16
  • Valued Contributor
  • August 18, 2015

If you want the last logged in user even if no one is logged in this might work better for gathering the user name.

The_user=$(last | awk '/console/ && !/root/' | head -n 1)

ryan_peterson
Forum|alt.badge.img+7

This has been changed to a Files and Processes Payload > Execute Command:

/usr/local/bin/jamf recon -endUsername $( ls -la /dev/console | cut -d " " -f 4 )