Skip to content

Instantly share code, notes, and snippets.

@djanatyn
Forked from asvnpr/-
Last active October 10, 2016 02:43
Show Gist options
  • Save djanatyn/e62eb5e9cb7d2d2df9cb86842808fc69 to your computer and use it in GitHub Desktop.
Save djanatyn/e62eb5e9cb7d2d2df9cb86842808fc69 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from passlib.hash import sha512_crypt
from subprocess import call
import csv
import sys
def load_users(csv_file):
""" Load a CSV file and return a dictionary representing each user.
Hash the password using SHA_512 and a random salt.
"""
users = {}
with open(csv_file, 'rb') as f:
reader = csv.reader(f)
for row in reader:
username = "{0}{1}".format(row[0][0], row[1]).lower()
password = row[2]
salted_password = sha512_crypt.encrypt(password)
users[username] = salted_password
return users
def create_user(username, password):
""" Create a user with subprocess.call. Returns the exit status. """
command = ['/usr/sbin/useradd', username, '-p', password]
return call(command)
def main():
""" Load a CSV script and generate passwords. """
# looking for one command line arguments
if len(sys.argv) != 2:
sys.exit("usage: {0} <file.csv>".format(sys.argv[0]))
csv_file = sys.argv[1]
print("loading users...")
users = load_users(csv_file)
for username, password in users.iteritems():
print("creating user '{0}'".format(username))
if create_user(username, password) != 0:
sys.exit("ABORTING - DID NOT EXECUTE SUCCESSFULLY")
print("complete!")
if __name__ == '__main__':
main()
#! /bin/bash
#script for batch generating users for AECC's raspberry pi. All passwords are temporary and will need to be prompted for new passwords on first login
echo -ne "This script must be run as root!\nScript for creating users for AECC raspberry pi from a csv file.\nEnter path for csv file: "
read csv_file
#exit if not root
if [[ `id -u` != 0 ]]; then
echo "Must be root to run script"
exit 1
fi
if [[ ! -f "$csv_file" ]]
then
echo "ERROR! File $csv_file does not exist. Run again with a valid file path"
else
#read csv file which contains first name, last name, student number
while IFS=, read fn ln sn
do
#get first char of first csv field (first letter of first name)
user1=${fn:0:1}
#get first last name
user2=$(echo "$ln" | cut -f 1 -d ' ')
if [[ "$user2" == "de" ]]
then
#dirty method. awk would probs be better but I wanna finish this quickly
sfln=$(echo "$ln" | cut -f 2 -d ' ')
user2+="$sfln"
echo "$user2"
fi
#combine lowercasefirst letter of last name and first last name) into username variable
user=$(echo "$user1$user2" | awk '{print tolower($0)}')
passwd=$sn
#change password for created user. I don't like this method, but I was having problems passing a hashed password
useradd -m $user && echo "$user:$passwd" | chpasswd
#echo "$user:$passwd"
if [ $? -eq 0 ]
then
echo "Created User:$user"
echo "Used Password:$passwd"
else
echo "Error! Could not create user $user."
fi
done < $csv_file;
fi
$ sudo ./create_users.py users.csv
loading users...
creating user 'asalvador-vega-nogales'
creating user 'jdoe'
creating user 'jsmith'
creating user 'jstrickland'
complete!
$ su jdoe
Password:
[jdoe@garnet alejandro-users]$
jonathan strickland password1
alejandro salvador-vega-nogales password2
john doe password3
jane smith password4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment