Created
July 14, 2017 16:01
-
-
Save jackpot51/9827c4b84ad5850c32bb8e07d4d6d9c7 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import os; | |
import subprocess; | |
import sys; | |
if os.getuid() != 0: | |
sys.exit('Error: must be run as root') | |
if len(sys.argv) < 2: | |
sys.exit('Error: no command specified') | |
user_name = subprocess.check_output( | |
"who | awk -v vt=tty$(fgconsole) '$0 ~ vt {print $1}'", | |
shell=True | |
).decode('utf-8').rstrip('\n') | |
display_name = subprocess.check_output( | |
"who | awk -v vt=tty$(fgconsole) '$0 ~ vt {print $5}'", | |
shell=True | |
).decode('utf-8').rstrip('\n').lstrip('(').rstrip(')') | |
args = [ | |
"sudo", | |
"-u", user_name, | |
"DISPLAY=" + display_name, | |
"--" | |
] | |
for arg in sys.argv[1:]: | |
args.append(arg) | |
os._exit(subprocess.call(args)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment