Advertisement
Guest User

Untitled

a guest
Oct 9th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. #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
  4.  
  5. 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: "
  6. read csv_file
  7.  
  8. #exit if not root
  9. if [[ `id -u` != 0 ]]; then
  10. echo "Must be root to run script"
  11. exit 1
  12. fi
  13.  
  14.  
  15. if [[ ! -f "$csv_file" ]]
  16. then
  17. echo "ERROR! File $csv_file does not exist. Run again with a valid file path"
  18.  
  19. else
  20. #read csv file which contains first name, last name, student number
  21. while IFS=, read fn ln sn
  22. do
  23. #get first char of first csv field (first letter of first name)
  24. user1=${fn:0:1}
  25. #get first last name
  26. user2=$(echo "$ln" | cut -f 1 -d ' ')
  27. if [[ "$user2" == "de" ]]
  28. then
  29. #dirty method. awk would probs be better but I wanna finish this quickly
  30. sfln=$(echo "$ln" | cut -f 2 -d ' ')
  31. user2+="$sfln"
  32. echo "$user2"
  33. fi
  34. #combine lowercasefirst letter of last name and first last name) into username variable
  35. user=$(echo "$user1$user2" | awk '{print tolower($0)}')
  36. passwd=$sn
  37. #change password for created user. I don't like this method, but I was having problems passing a hashed password
  38. useradd -m $user
  39. echo "$user:$passwd" | chpasswd
  40. echo "$user:$passwd"
  41. if [ $? -eq 0 ]
  42. then
  43. echo "Created User:$user"
  44. echo "Used Password:$passwd"
  45. else
  46. echo "Error! Could not create user $user."
  47. fi
  48.  
  49. done < $csv_file;
  50. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement