Best extension attribute for currently logged in or last logged in user?

jclark27
New Contributor III

Hey all!

What is a great ext attribute to determine which user is most recently logged in or currently logged in? I'm curious as we use the one provided in a JAMF repository and has worked fairly solidly for us, but wondering if there are any pitfalls to using this.

 

#!/bin/sh
lastUser=`/usr/bin/last -1 -t console | awk '{print $1}'`

if [ $lastUser == "wtmp" ]; then
echo "<result>No logins</result>"
else
echo "<result>$lastUser</result>"
fi

 

 

3 REPLIES 3

pete_c
Contributor III
lastUser=$(defaults read /Library/Preferences/com.apple.loginwindow lastUserName)

aparten
New Contributor III

My current Extension Attribute is below, have not seen any issues with it so far.

#!/bin/sh
lastUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`

if [ $lastUser == "" ]; then
	echo "<result>No logins</result>"
else
	echo "<result>$lastUser</result>"
fi

jclark27
New Contributor III

Thanks all! I'm going to give those a shot, actually have a similar script in our server utilizing that same defaults read option. As I've said, the original one I posted has been working pretty well, but sometimes gives us some "no logins" on computers where the defaults read one will work better and give us an output. appreciate it!