Last active
January 26, 2016 18:37
-
-
Save rmanly/f3e3d59a2dce96f96ec3 to your computer and use it in GitHub Desktop.
use SystemConfiguration to get console user vs. stat'ing /dev/console etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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