Skip to content

Instantly share code, notes, and snippets.

@ahadsheriff
Last active December 14, 2022 03:49
Show Gist options
  • Save ahadsheriff/a2301dc12b6be3887cdf011c3d71b1bc to your computer and use it in GitHub Desktop.
Save ahadsheriff/a2301dc12b6be3887cdf011c3d71b1bc to your computer and use it in GitHub Desktop.
Python script to add users to Linux groups.
"""
Author: Ahad Sheriff
Description:
Python script to add users to Linux groups. You can easily change the content
of this script to fit your needs.
Sample CSV file that I used to test is
included as `sampleAccounts.csv`
Data:
The data in the csv file must be of the form:
"Full Name", "Office Number", "Phone Extension #", "Department Name"
"""
import os
import csv
def readFile(file):
with open(file, 'r') as f:
data = [row for row in csv.reader(f.read().splitlines())]
for i in data:
fullName = i[0]
splitName = [x.strip() for x in fullName.split(',')]
firstAndMiddle = [c.strip() for c in splitName[1].split(' ')]
first = firstAndMiddle[0].lower()
last = splitName[0].lower()
"""
Capitalize only the first letter of the names
"""
newFirst = first[0]
newFirst = newFirst.upper()
firstName = newFirst + first[1:]
newLast = last[0]
newLast = newLast.upper()
lastName = newLast + last[1:]
if ((len(firstAndMiddle)) > 1):
middle = firstAndMiddle[1].lower()
newMiddle = middle[0]
newMiddle = newMiddle.upper()
middleName = newMiddle + middle[1:]
else:
middleName = ""
officeNumber = i[1]
extension = i[2]
department = i[3]
"""
generate unique user id's for each user
the script will give each member a user id that consists of:
The first letter of their first name
Their last name
Their office extension number
This uid prevents problems occuring with multiple users of the same name
"""
uid = firstName[0].lower() + lastName.lower() + extension
"""
All users should use the default shell
/bin/bash
except Engineering folks who's default path is
/bin/csh
"""
if department == "Engineering":
shell = "/bin/csh"
else:
shell = "/bin/bash"
print("Adding user: \n" + firstName + " " + middleName + " " + lastName + " | " + officeNumber
+ " | " + extension + " | " + department + " | " + uid + " | " + shell + "\n")
os.system("useradd " + uid + " -c " + "'" + firstName + " " + middleName + " " + lastName + "'" + " -g "
+ department + " -d /home/" + department + "/" + uid + " -s " + shell)
def main():
file = input("Enter a filename: ")
readFile(file)
main()
ADLER, CHARLES DAVID 00-9388 x0753 Engineering
ANSELL, ROBERT D 14-2675 x1624 Sales
BIANCHI, CHRISTOPHER 13-7302 x3280 Marketing
CAPOZZI, MICHAEL B 47-1742 x8035 Manufacturing
CLAYPOOLE, AARON JON 00-8739 x4424 Engineering
CORRIGAN, DANIEL W 13-15483 x4673 Sales
DAVOLI, MARK STEPHEN 18-6493 x2023 Marketing
DZIAK, JAMES WILLIAM 89-5180 x8812 Manufacturing
ELFSTRAND, MARSHALL J 18-3232 x5952 Engineering
FOSS, AARON D 14-47792 x6780 Sales
GAULT, ROBERT B 28-82496 x5465 Marketing
GRENETZ, DAVID R 00-5230 x4105 Administrative
KARANIKIS, ANDREW MIC 16-90069 x3607 Manufacturing
MORSE, MICHAEL A 14-8031 x8434 Sales
PEREZ, DANIEL 18-11360 x0431 Administrative
PORTNOY, GARY 16-20948 x8121 Manufacturing
RANKIN, SCOTT E 37-3857 x0511 Engineering
RUSSELL, KEVIN C 96-7377 x3892 Sales
SNELL, DAVID ALLEN 24-86054 x0055 Manufacturing
STERON, ROBERT 18-1414 x1903 Engineering
TRAN, THANH T 18-2173 x2630 Sales
WALKER, CHRISTIE 88-5927 x2432 Marketing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment