Skip to content

Instantly share code, notes, and snippets.

@asvnpr
Created October 9, 2016 21:43
Show Gist options
  • Save asvnpr/e215442d1ab1d398b7a4c122fead885b to your computer and use it in GitHub Desktop.
Save asvnpr/e215442d1ab1d398b7a4c122fead885b to your computer and use it in GitHub Desktop.
#! /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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment