1

First of all, here is my code:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from xlrd import open_workbook
import subprocess
import sys

START_ROW = 0
col_name = 0
col_room = 2
col_phone = 3
col_usr_name = 4
col_password = 5

book = open_workbook('Names_mod.xls',formatting_info=True)
sheet = book.sheet_by_index(0)

for row_index in range(START_ROW, sheet.nrows): 
    username = sheet.cell(row_index, col_usr_name).value
    pwrd = sheet.cell(row_index, col_password).value
    name = sheet.cell(row_index, col_name).value
    room = sheet.cell(row_index, col_room).value
    room = ''.join(i for i in pwrd if i.isdigit())
    phone = sheet.cell(row_index, col_phone).value
    phone = ''.join(i for i in pwrd if i.isdigit())
    comment = name".", room".", phone"."
    if col_name != "":
        subprocess.call(['useradd -c', comment, username])
        subprocess.call(['passwd', username])

When I run this script I get this error code:

Traceback (most recent call last):
File "./lab5uppgift2.py", line 30, in <module>
    subprocess.call(['useradd -c', comment, username])
File "/usr/local/lib/python2.7/subprocess.py", line 524, in call
    return Popen(*popenargs, **kwargs).wait()
File "/usr/local/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
File "/usr/local/lib/python2.7/subprocess.py", line 1308, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

I am trying to add users with passwords and comments from an excel file. I don't get what I'm doing wrong. Can someone please explain this for me?

Thanks!

Timmy
  • 47
  • 1
  • 6
  • First of all, what is this `comment = name".", room".", phone"."`? Secondly - `useradd` is not available for normal users; are you running the script as root? If not, you need to pass `sudo` as one of your arguments in `subprocess.call` – Burhan Khalid Oct 28 '13 at 06:14
  • Yes, I'm running it as root. I've changed comment now. Is it better? – Timmy Oct 28 '13 at 06:43
  • This can't be the code you are running because it will never execute. – Burhan Khalid Oct 28 '13 at 07:13

1 Answers1

2

You should pass all args to subprocess.call in separate elements of a list, try

subprocess.call(['useradd', '-c', comment, 'username', '-p', 'password'])

I replaced your password username to 'password' since '-p' argument to useradd must be encripted with crypt. Following snippet will help you with this:

import os
import crypt
import random

saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

def salt():
    return random.choice(saltchars) + random.choice(saltchars)

def encript_pwd(password):
    return crypt.crypt(password,salt())

If you want password to equal username, use

subprocess.call(['useradd', '-c', comment, 'username', '-p', 
    encript_pwd(username)])
alko
  • 46,136
  • 12
  • 94
  • 102
  • Used: subprocess.call(['useradd', '-c', comment, username, '-p', username]) Get: Traceback (most recent call last): File "./lab5uppgift2.py", line 29, in subprocess.call(['useradd', '-c', comment, username, '-p', username]) File "/usr/local/lib/python2.7/subprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() File "/usr/local/lib/python2.7/subprocess.py", line 711, in __init__ errread, errwrite) File "/usr/local/lib/python2.7/subprocess.py", line 1308, in _execute_child raise child_exception TypeError: execv() arg 2 must contain only strings – Timmy Oct 28 '13 at 06:11
  • Username is: username = sheet.cell(row_index, col_usr_name).value – Timmy Oct 28 '13 at 06:14
  • see that link for this **new** question http://stackoverflow.com/questions/7612727/python-subprocess-and-unicode-execv-arg-2-must-contain-only-strings – alko Oct 28 '13 at 06:23
  • I don't really get it. Should it be i.e. comment.encode('utf8')? Btw, I've updated my code and error message. – Timmy Oct 28 '13 at 06:31
  • not a good practice, you should preserve your original question, or answers can't be understood later. and we (at least I) write them not only to solve current problem, for further reference. I advise you to restore original question and open new question on this problem, or add your new problem just after the first. – alko Oct 28 '13 at 06:35
  • I've changed it back. I don't have much experience with python or stackoverflow so thanks for helping me. I still can't get it to work though. This is my new subprocess.call: subprocess.call(['useradd', '-c', comment, username, '-p', encript_pwd(pwrd)]).encode('utf8') – Timmy Oct 28 '13 at 06:41
  • What are aguments, `comment` and `username`, values when error occurs? I have a quick solution: `args = ['useradd',...]` and `subprocess.call(map(lambda x: x.encode('utf8'), args)` but I am not sure it is good and robust, and I am not aware of `useradd` handling utf8. You'd better create a new question. – alko Oct 28 '13 at 06:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40095/discussion-between-alko-and-timmy) – alko Oct 28 '13 at 06:48