Why user is created but password does not work?

Asked

Viewed 42 times

0

Good afternoon guys I am building a script to create user and password on linux (Ubuntu 16.04LTS). The problem is that after running my script the user is created but I can’t log in with the password that was passed by the script. Anyone know why?? Thanks for your help.

import subprocess
import sys

#criar usuario e senha 
nome  = input('Digite o nome do usuario: ')
senha = input('Digite a senha do usuario: ')
dominio = input('Digite o Dominio: ')

# user
user_sufix = nome
user_name = 'teste_' + user_sufix

# password
user_pass = senha

# domain
domain = dominio


#Cria o usuario na maquina
useradd_result = subprocess.run(["sudo", "useradd","-m","-p",user_pass, "-s", "/bin/bash",])
#passwd_result = subprocess.run(['sudo','passwd',user_name,user_pass])

1 answer

1


When you use the command useradd passing the password, it should already be encrypted.

Then do it using the command openssl passwd before. Capture the output of this command with the attribute stdout, passing the argument subprocess.PIPE for the parameter stdout of the method run().

The result is a sequence of bytes, which you must decode and remove the character newline of the end.

It would look like this, before the execution of useradd:

user_pass = subprocess.run(['openssl', 'passwd', senha], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()

After that you can use user_pass as an argument for the useradd.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.