write rename_user standalone script
parent
33fee98309
commit
0d8b370b5d
|
@ -0,0 +1,37 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
"""This script wraps the usermod command to allow user account renames via sudoers."""
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def rename_user(username, new_username):
|
||||||
|
# usermod -l new_username -m -d /home/{new_username} username
|
||||||
|
args = [
|
||||||
|
'usermod',
|
||||||
|
'-l',
|
||||||
|
new_username,
|
||||||
|
'-m',
|
||||||
|
'-d',
|
||||||
|
os.path.join('/home', new_username),
|
||||||
|
username
|
||||||
|
]
|
||||||
|
subprocess.run(args, check=True)
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv):
|
||||||
|
if len(argv) < 3:
|
||||||
|
print('[rename_user] Too few arguments passed.', file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
rename_user(argv[1], argv[2])
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print('[rename_user] {}'.format(e), file=sys.stderr)
|
||||||
|
return 2
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
exit(main(sys.argv))
|
|
@ -34,6 +34,7 @@ def validate_displayname(display_name):
|
||||||
if not DISPLAY_NAME_RE.match(display_name):
|
if not DISPLAY_NAME_RE.match(display_name):
|
||||||
raise ValidationError("Valid characters: a-z, A-Z, 0-9, -, _, and '.")
|
raise ValidationError("Valid characters: a-z, A-Z, 0-9, -, _, and '.")
|
||||||
|
|
||||||
|
|
||||||
def validate_pubkey(pubkey):
|
def validate_pubkey(pubkey):
|
||||||
# TODO see if I can get the type out
|
# TODO see if I can get the type out
|
||||||
key = ssh.SSHKey(pubkey, strict_mode=False, skip_option_parsing=True)
|
key = ssh.SSHKey(pubkey, strict_mode=False, skip_option_parsing=True)
|
||||||
|
|
Loading…
Reference in New Issue