Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.12 KB | None | 0 0
  1. syntax_error()
  2. {
  3.     echo "Syntax: add <group> [prefix]"
  4.     echo " "
  5.     echo "group:  the initial group"
  6.     echo "prefix: the generated username will begin with this prefix"
  7.     echo " "
  8.     echo "The full name of all new users needs to be in newusers.txt"
  9.     echo "A file named newusers.csv will be generated."
  10.     echo " "
  11.     echo "You need to be root to run this script"
  12.  
  13.     exit 0
  14. }
  15.  
  16. #Check that we have at least one argument (group) but not more than two
  17. if [ $# == 0 -o $# -gt 2 ]; then
  18.     syntax_error
  19. fi
  20.  
  21. #Use the second arg, if given, as PREFIX
  22. prefix=""
  23. if [ $# = 2 ]; then
  24.     prefix=$2
  25. fi
  26.  
  27.  
  28. #Use the first arg as GROUP
  29. group=$1
  30.  
  31.  
  32. #You need to be root to run the script
  33. if [ "$(whoami)" != "root" ]; then
  34.     syntax_error
  35. fi
  36.  
  37.  
  38. #Check that the the group is existing
  39. tmp=$(grep $group /etc/group| wc -l)
  40. if [ $tmp != 1 ]; then
  41.     syntax_error
  42. fi
  43.  
  44.  
  45. #If group folder is not existing, create it
  46. if [ ! -d /home/$group ]; then
  47.     mkdir /home/$group
  48. fi
  49.  
  50.  
  51. while read line; do
  52.         if [ "$line" != "" ]; then
  53.             #Create username and password
  54.             name=$line
  55.             user=$prefix$(echo $line|sed -e's/\ /./g' -e's/å/a/g' -e's/ä/a/g' -e's/ö/o/g' -e's/Å/A/g' -e's/Ä/A/g' -e's/Ö/O/g' -e's/é/e/g' -e's/è/e/g' -e's/-//g' |tr A-Z a-z)
  56.             password=$(apg -n 1 -m 7 -x 7 -M nl -E 1l)
  57.             #PASSWORD="skolning"
  58.             encpassword=$(perl -e 'print crypt($ARGV[0], "password")' $password)
  59.  
  60.             #Print out the information
  61.             echo " "
  62.             echo "Name: $name"
  63.             echo "Username: $user"
  64.             echo "Password: $password"
  65.             echo " "
  66.  
  67.  
  68.             #Save the new user to a CSV file
  69.             echo "\"$name\",\"$user\",\"$password\"" >> newusers.csv
  70.  
  71.             #Add the user to the system
  72.             useradd -c "$name" -p $encpassword -b /home/$group -g $group -m -s /bin/bash -G lp,cdrom,floppy,audio,video,plugdev,fuse,saned $user
  73.  
  74.             chmod 700 /home/$group/$user
  75.  
  76.             #Copy quota settings from netuser
  77.             setquota -u -p netuser $user /home
  78.         fi
  79.  
  80. done < newusers.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement