Logged in user returning "username<br/>"

jphillips
Contributor

Trying to get the username of the currently logged in user during a Login policy. I have tried all sorts of ways: $3, stat -f%Su /dev/console, and through /dev/console.

I wasn't getting the desired results from the script. I echoed the variable back and checked the policy logs. Every user comes back with <br/> appended to it. I even tried trimming the last 5 characters, but it doesn't remove the break code.

An example of the log:
Script result: Current logged in user: hopeg14<br/>User home: /Users/hopeg14<br/>

I have only started noticing this since the JSS upgrade to 9.100.0
Any thoughts?

1 ACCEPTED SOLUTION

tthurman
Contributor III
loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

View solution in original post

5 REPLIES 5

stevevalle
Contributor III

I have used this to get the last logged in user (which should also be the currently logged in user!)

defaults read /Library/Preferences/com.apple.loginwindow lastUserName

I also use the following in an extension attribute to display the last logged in user.

#!/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

Hope that helps!

cbrewer
Valued Contributor II

I don't think the line breaks in the logs are your problem. Ive seen those in most of my policy logs in the last few 9.x versions. $3 should give you what you want.

daz_wallace
Contributor III
Contributor III

Hi @jphillips

This blog maybe of use to you : )

HOW TO: GET THE CURRENTLY LOGGED IN USER, IN A MORE APPLE APPROVED WAY

Hope that helps!

Darren

tthurman
Contributor III
loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

jphillips
Contributor

Thanks guys. I actually tried each of the suggestions above, but the break keeps appearing in the logs. I was able to test if the variable was actually saving the break by just touching a file in user's home, which it created as expected.

Long story short: the break is only in the log file. There must have been something else in the script that was not working correctly.