Skip to content

Instantly share code, notes, and snippets.

@jackpot51
Created July 14, 2017 16:01
Show Gist options
  • Save jackpot51/9827c4b84ad5850c32bb8e07d4d6d9c7 to your computer and use it in GitHub Desktop.
Save jackpot51/9827c4b84ad5850c32bb8e07d4d6d9c7 to your computer and use it in GitHub Desktop.
#!/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