Recon User Names

bazcurtis
New Contributor III

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 11

mm2270
Legendary Contributor III

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 }'

jarednichols
Honored Contributor

or

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

bentoms
Release Candidate Programs Tester

bazcurtis
New Contributor III

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

jarednichols
Honored Contributor

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

mm2270
Legendary Contributor III

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 )

bazcurtis
New Contributor III

Worked like a charm, thank you.

Cheers

Michael

Fveja
New Contributor III

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

Not applicable

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

Look
Valued Contributor III

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
New Contributor II
New Contributor II

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 )