Skip to content

Instantly share code, notes, and snippets.

@rmanly
Last active January 26, 2016 18:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmanly/f3e3d59a2dce96f96ec3 to your computer and use it in GitHub Desktop.
Save rmanly/f3e3d59a2dce96f96ec3 to your computer and use it in GitHub Desktop.
use SystemConfiguration to get console user vs. stat'ing /dev/console etc.
# frogor user check v1
# http://osx.michaellynn.org/freenode-osx-server/freenode-osx-server_2013-04-09.html
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
from objc import NULL
import sys
username = (SCDynamicStoreCopyConsoleUser(NULL, NULL, NULL) or [NULL])[0]
username = [username,''][username in [u"loginwindow", None]]
sys.stdout.write(username + "\n")
# v2
# http://osx.michaellynn.org/freenode-osx-server/freenode-osx-server_2013-04-09.html
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
from objc import NULL
import sys
username = (SCDynamicStoreCopyConsoleUser(NULL, NULL, NULL) or [NULL])[0]
username = [username,''][username in [u"loginwindow", u""]]
sys.stdout.write(username + "\n")
# v3
# http://osx.michaellynn.org/freenode-osx-server/freenode-osx-server_2014-10-28.html
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
from objc import NULL
import sys
username = (SCDynamicStoreCopyConsoleUser(NULL, NULL, NULL) or [None])[0]
username = [username,''][username in [u"loginwindow", None, u""]]
sys.stdout.write(username + "\n")
# v4
# http://osx.michaellynn.org/freenode-osx-server/freenode-osx-server_2014-10-28.html
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 + "\n")
# embed in bash script like so
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 + "\n");'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment