0

these are the error messages I am receiving from the terminal when I run add_user.py

sh: -c: line 0: unexpected EOF while looking for matching `''

sh: -c: line 1: syntax error: unexpected end of file

After some reading online I am led to believe that these errors are thrown when I am missing a matching parenthesis or quotation mark?I have looked through the file several times but since it has been a few hours, I might just be looking straight over the error. If it is not a missing quotation mark or parenthesis, please explain what might be causing the issue so I can fix it. Thanks to all who help!

import csv
import os

filename = "/root/Downloads/linux_users.csv"

def create_username(aList):
    if (len(aList) < 2):
        print("INVALID RECORD")
    else:
        tokens = list(aList[2])
        username = tokens[0].lower() + aList[1].lower()
        return username

with open(filename) as f_handle:
    csv_reader = csv.reader(f_handle)
    next(csv_reader)
    userList = []
    groupList = []
    recordCopy = []
    rep = 0
    tick = True

    for record in csv_reader:
        if (record[7] == ""):
            record.remove(record[7])

        for data in record:
            if (data == ""):
                print("ERROR: MISSING INFORMATION. USER ID: " + record[0] + " HAS NOT BEEN ADDED")
                tick = False
        
        if ((record[6] not in groupList) and (tick == True)):
            groupList.append(record[6])

        if (tick == True):
            username = create_username(record)
            if (username in userList):
                username += str(rep)
                rep += 1
            userList.append(username)
            recordCopy.append(record)

        else:
            tick = True
    
    for group in groupList:
        os.system("groupadd " + group)
        print("GROUP: " + group + " HAS SUCCESSFULLY BEEN CREATED")
    
    i = 0
    for record in recordCopy:
        if (record[5] == "ceo"):
            os.system("adduser " + userList[i] + " -g " + record[6] +
            " -d /home/" + record[5] + " -s /bin/csh -u " + record[0] + " -p password")
            os.system("passwd -e " + userList[i])
        else:
            os.system("adduser " + userList[i]  + " -g " + record[6] +
            " -d /home/" + record[5] + " -s /bin/bash -u " + record[0] + " -p password")
            os.system("passwd -e " + userList[i])
        i += 1
        print("USER: " + record[0] + " HAS SUCCESSFULLY BEEN ADDED")

        
            

        
  • 1
    How are you attempting to run this? – khelwood Oct 12 '21 at 19:51
  • The error means your `os.system()` has unmatched quotes – Charles Duffy Oct 12 '21 at 19:56
  • But you shouldn't use `os.system()` at all in the first place. Switch to `subprocess.run()` without `shell=True` and the whole problem goes away. – Charles Duffy Oct 12 '21 at 19:56
  • That is to say: `subprocess.run(["adduser", userList[i], "-g", record[6], "-d", f"/home/{record[5]}", "-s", "/bin/bash", "-u", record[0], "-p", "password"])` – Charles Duffy Oct 12 '21 at 19:57
  • Also, far fewer security issues with `subprocess.run()` and no shell -- you don't want someone saying their username is `$(rm -rf ~)'$(rm -rf ~)'` with the code you currently have or you'll have a very, _very_ bad day. – Charles Duffy Oct 12 '21 at 19:58
  • Anyhow -- see [How to escape `os.system()` calls](https://stackoverflow.com/questions/35817/how-to-escape-os-system-calls) – Charles Duffy Oct 12 '21 at 20:00

0 Answers0