2

I'm creating a python script that will create user accounts with an username and a password from an excile file. I'm having trouble finding a one line command that will create an user with a password though. I have tried "useradd" with the p-flag but it tells me that the password is wrong when I'm trying to login. I've also tried "adduser" but I haven't found a way yet. It seems like the user himself has to enter the password.

What I want to do: Create an user account with a password preferably with an one-line command.

Here is my python code at the moment that just creates the user accounts without a password:

#!/usr/bin/env python

import os
import openpyxl
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

excel_file = os.open("Names_mod.xlsx", os.O_RDONLY)
wb = openpyxl.load_workbook("Names_mod.xlsx")
sheet = wb.active
max_row = sheet.max_row

for i in range(1, max_row + 1):
    name = sheet.cell(row = i, column = 5)  
    password = sheet.cell(row = i, column = 6)
    addUser = "sudo useradd -m  " + name.value
    os.system(addUser)
  • 1
    Related, on StackOverflow: stackoverflow.com/q/2150882. The accepted answer likely doesn't work, as stated in comments, but chpasswd (from the second ranked answer) may be what you are looking for. All readers: feel free to make a full answer from this. – fra-san May 7 at 18:14
  • Related: newusers (provided by passwd on Ubuntu): unix.stackexchange.com/q/21022/315749 – fra-san May 7 at 20:04
0

I haven't tested it yet but it looks like you need to pass in a password that has already been encrypted from man useradd on ubuntu (that's probably why it didn't work before):

       -p, --password PASSWORD
           The encrypted password, as returned by crypt(3). The default is to disable the password.

           Note: This option is not recommended because the password (or encrypted password) will be visible by users listing the processes.

           You should make sure the password respects the system's password policy.

The crypt module exists in python 2 and 3. For python 3 you can use crypt.crypt(password) This worked for me:

python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import crypt
>>> crypt.crypt("hello")
'$6$AFKk6qImS5R6bf//$Voc7MRbOX5R2R8GvmmEHxVgx/wVorKO4y2Vuufgkph798mo/SJ6ON8vKJWj1JTsRdwNyb9oiTmHiNYGiOL4Q20'
>>> 

For python 2 you need to pass the salt as the second argument as well. Be careful about choosing different salts for different users.

So tl;dr in python3 try:

import crypt

...

for i in range(1, max_row + 1):
    name = sheet.cell(row = i, column = 5)  
    password = sheet.cell(row = i, column = 6)
    encrypted_password = crypt.crypt(password)
    addUser = "sudo useradd -m  " + name.value + " -p " + encrypted_password
    os.system(addUser)

EDIT: From the original poster this doesn't work in python2, the password is still wrong. I haven't tested the actual login in python3 either though. For python2 this doesn't work:

for i in range(1, max_row + 1):
    name = sheet.cell(row = i, column = 5)  
    password = sheet.cell(row = i, column = 6)
    encrypted_password = crypt.crypt(password, name.value)
    # This doesnt work!
    addUser = "sudo useradd -m  " + name.value + " -p " + encrypted_password
    os.system(addUser)
  • Great answer, thank you! This worked! – xypo May 7 at 20:06
  • Never mind, I thought it worked but now I'm getting this error. Traceback (most recent call last): File "./uppgift2.py", line 18, in <module> encrypted_password = crypt.crypt(password) TypeError: crypt() takes exactly 2 arguments (1 given) – xypo May 7 at 20:25
  • @KarlOlsson I think you are using python2, you have to give it a salt too. Try crypt.crypt(password, name.value) (im not sure what the best salt to give it though in python2) – Patrick Riordan May 7 at 21:23
  • Oh, for some reason I thought I used python 3, my bad! Now it runs without problem but it still tells me that the password is wrong when I try and login with one of the accounts. I appreciate the help mate! – xypo May 7 at 22:07
  • ah dang.. when i tried with python3 it looked a little better, maybe it doesn't work in python2, or there is some other mistake... Your welcome! hopefully it helped anyways! – Patrick Riordan May 8 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.