Posted on 08-24-2017 06:52 PM
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?
Solved! Go to Solution.
Posted on 08-25-2017 04:47 AM
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 + "
");'`
Posted on 08-24-2017 07:56 PM
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!
Posted on 08-24-2017 08:19 PM
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.
Posted on 08-25-2017 02:10 AM
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
Posted on 08-25-2017 04:47 AM
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 + "
");'`
Posted on 08-25-2017 05:56 AM
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.