Advertisement
Guest User

Untitled

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