Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- syntax_error()
- {
- echo "Syntax: add <group> [prefix]"
- echo " "
- echo "group: the initial group"
- echo "prefix: the generated username will begin with this prefix"
- echo " "
- echo "The full name of all new users needs to be in newusers.txt"
- echo "A file named newusers.csv will be generated."
- echo " "
- echo "You need to be root to run this script"
- exit 0
- }
- #Check that we have at least one argument (group) but not more than two
- if [ $# == 0 -o $# -gt 2 ]; then
- syntax_error
- fi
- #Use the second arg, if given, as PREFIX
- PREFIX=""
- if [ $# = 2 ]; then
- PREFIX=$2
- fi
- #Use the first arg as GROUP
- GROUP=$1
- #You need to be root to run the script
- if [ "$(whoami)" != "root" ]; then
- syntax_error
- fi
- #Check that the the group is existing
- TMP=$(grep $GROUP /etc/group| wc -l)
- if [ $TMP != 1 ]; then
- syntax_error
- fi
- #If group folder is not existing, create it
- if [ ! -d /home/$GROUP ]; then
- mkdir /home/$GROUP
- fi
- while read LINE; do
- if [ "$LINE" != "" ]; then
- #Create username and password
- NAME=$LINE
- 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)
- PASSWORD=$(apg -n 1 -m 7 -x 7 -M nl -E 1l)
- #PASSWORD="skolning"
- ENCPASSWORD=$(perl -e 'print crypt($ARGV[0], "password")' $PASSWORD)
- #Print out the information
- echo " "
- echo "Name: $NAME"
- echo "Username: $USER"
- echo "Password: $PASSWORD"
- echo " "
- #Save the new user to a CSV file
- echo "\"$NAME\",\"$USER\",\"$PASSWORD\"" >> newusers.csv
- #Add the user to the system
- useradd -c "$NAME" -p $ENCPASSWORD -b /home/$GROUP -g $GROUP -m -s /bin/bash -G lp,cdrom,floppy,audio,video,plugdev,fuse,saned $USER
- #Copy quota settings from netuser
- setquota -u -p netuser $USER /home
- fi
- done < newusers.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement