Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!bin/bash
- function check()
- {
- arg1=$1
- arg1="'"$arg1"'"
- arg2="/etc/$2"
- arg3="^$3"
- arg4=$4
- commandLine="awk -F':' $arg1 $arg2 |grep $arg3 > $arg4"
- eval $commandLine
- return $?
- }
- function checkUserExists(){
- check 1 passwd $1 /dev/null
- return $?
- }
- function checkGroupExists(){
- check 1 group $1 /dev/null
- return $?
- [user1@localhost ~]$ vi check.sh
- [user1@localhost ~]$ more check.sh
- #!bin/bash
- function check()
- {
- arg1=$1
- arg1="'"$arg1"'"
- arg2="/etc/$2"
- arg3="^$3"
- arg4=$4
- commandLine="awk -F':' $arg1 $arg2 |grep $arg3 > $arg4"
- eval $commandLine
- return $?
- }
- function checkUserExists(){
- check 1 passwd $1 /dev/null
- return $?
- }
- function checkGroupExists(){
- check 1 group $1 /dev/null
- return $?
- }
- function checkUIDExists(){
- check 3 passwd $1 /dev/null
- return $?
- }
- function checkGroupUIDExists(){
- check 3 group $1 /dev/null
- return $?
- }
- function usage(){
- echo "How to\n"
- echo "PARAMETERS : \n"
- echo "'-u' <user> : create user
- echo "'-ug' <user> <group> : create user and add it to the group"
- echo "Req : Launch the program as Administrator\n"
- }
- function main(){
- if [[ $EUID -ne 0 ]] ; then
- echo "You must run this program as root !"
- exit 1
- else
- if [ $# -ne 3 -o $# -ne 2 ] ; then
- echo "Error in the parameters"
- exit 1
- else
- case "$1" in
- -u) if [ $# -e 2 ] ; then
- createUser $2
- else
- echo "Error incorrect number of param for '-u' , need a username"
- exit 3
- fi
- ;;
- -ug) echo "Not implemented yet"
- exit 4
- fi
- ;;
- *) echo "INVALID OPTION"
- exit 6
- ;;
- esac
- fi
- fi
- return 0
- }
- function createUser(){
- checkUserExists $1
- if [ $? -e 0 ]; then
- uid=$(awk -F":" 'END { print $3 }' /etc/passwd)
- uid=$((uid+1))
- if [ $uid -le 500 ]; then
- uid=501
- fi
- checkUIDExists() $uid
- if [ $? -ne 0 ]; then
- checkGroupUIDExists $uid
- fi
- while [ $? = 0 ]; do
- uid=$((uid+1))
- checkUIDExists $uid
- if [ $? -ne 0 ]; then
- checkGroupUIDExists $uid
- fi
- done
- echo "$1:x:$uid:$uid:$1:/home/$1:/bin/bash" >> /etc/passwd
- echo "$1:x:$uid:" >> /etc/group
- mkdir /home/$1
- cp -vR /etc/skel/.bash* /home/$1
- chmod -vR 0755 /home/$1
- chown -vR $1:$1 /home/$1
- passwd $1
- }
- main $1 $2 $3 $4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement