Last active
April 23, 2026 19:53
-
-
Save jurandysoares/f554b747921e7640f097224d88ba5160 to your computer and use it in GitHub Desktop.
Create Linux users with animal names base in the CSV file https://gist.github.com/jurandysoares/cb63fe7a057ea7b153073ab9b75cd1af
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 | |
| from csv import DictReader | |
| import subprocess | |
| import platform | |
| import pwd | |
| import grp | |
| import os | |
| import sys | |
| if platform.system() != 'Linux': | |
| print("Este script deve ser executado no Linux.") | |
| sys.exit(1) | |
| else: | |
| os_release: dict = platform.freedesktop_os_release() | |
| # TODO: Salvar informações sobre a distribuição Linux | |
| if os.geteuid() != 0: | |
| print("Este script precisa ser executado como root.") | |
| sys.exit(1) | |
| else: | |
| print("Executando como root, permissões adequadas.") | |
| # Campos: Emoji, Português, English, Shortcode | |
| with open('emoji-animais.csv') as arq_emojis, open('emoji-passwd', mode='w', encoding='utf-8') as arq_usuarios: | |
| leitor = DictReader(arq_emojis) | |
| try: | |
| dados_grupo = grp.getgrnam('animal') | |
| except KeyError: | |
| subprocess.run(['/usr/sbin/groupadd', 'animal']) | |
| dados_grupo = grp.getgrnam('animal') | |
| for linha in leitor: | |
| emoji = linha['Emoji'] | |
| nome = linha['Português'] | |
| name = linha['English'] | |
| code = linha['Shortcode'].replace(':', '') | |
| try: | |
| dados_animal = pwd.getpwnam(nome) | |
| except KeyError: | |
| dados_animal = None | |
| if not dados_animal: | |
| campos = { | |
| 'user': nome, | |
| 'pwd': name, | |
| 'gecos': f'{nome.title()},{name.title()},{code},{emoji}', | |
| 'home': f'/home/{nome}', | |
| 'shell': f'/usr/bin/zsh', | |
| } | |
| useradd_args = [ | |
| '/usr/sbin/useradd', | |
| '-m', | |
| '-c', f'{campos["gecos"]}', | |
| '-d', f'{campos["home"]}', | |
| '-s', f'{campos["shell"]}', | |
| nome | |
| ] | |
| subprocess.run(useradd_args) | |
| chpasswd_in = f"{nome}:{name}" | |
| subprocess.run(['/usr/sbin/chpasswd'], input=chpasswd_in.encode(), check=True) | |
| usermod_args = [ | |
| '/usr/sbin/usermod', | |
| '-aG', 'animal', | |
| nome | |
| ] | |
| subprocess.run(usermod_args) | |
| chpasswd_args = [ | |
| '/usr/bin/chage', | |
| '-d', | |
| '0', | |
| nome | |
| ] | |
| subprocess.run(chpasswd_args) | |
| else: | |
| print(f'O usuário "{nome}" {emoji} já existe no sistema.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment