Guest User

Untitled

a guest
Jul 27th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.36 KB | None | 0 0
  1. #!/bin/bash
  2. ###############################################################
  3. #This Script is Created by                                    #
  4. #                USMAN AKRAM  (ajaonchat)                     #
  5. #                                       FA05-BTN-005          #
  6. #                            BTN-6                            #
  7. ###############################################################
  8. add_users()
  9. {
  10.     ROOT_UID=0      #The root user has a UID of 0
  11.     if      [ "$UID" -ne "$ROOT_UID" ]; then
  12.             echo "****You must be the root user to run this script!****"
  13.             exit
  14.     fi
  15.     echo
  16.     echo Identity Verified_You are the Root
  17.     echo
  18.  
  19.     echo -e "\n#########################################\n"
  20.     echo -e "Please Select the Mode!!!\n"
  21.     echo -e "1. Add the Users Manually\n
  22. 2.Read the Users Automatically  from Text File\n"
  23.     echo -e "###########################################"
  24.     read add_opt
  25.     case $add_opt in
  26.         1)
  27.         echo -e "Please Enter the User name:"
  28.         read usr_name
  29.         echo -e "Please enter the User Group"
  30.         read usr_group
  31.         groupadd $usr_group
  32.         useradd -g $usr_group -m $usr_name
  33.                 echo -e "Please enter the Password for User $usr_name"
  34.     passwd $usr_name ;;
  35.     2)
  36.        echo
  37.        echo "Present working directory is: `pwd`/users.txt"
  38.        echo
  39.        echo -e "Do you want to use the above Default PATH? Yes=1 & No=2"
  40.        read yn
  41. if [ $yn == 1 ]; then
  42.                       Path=$($pwd)users.txt
  43. else
  44.        echo -n "Please enter the correct path to the file (e.g. /root/folder/filename.txt): "
  45.        read Path
  46. fi
  47. if [ -e $Path ]; then           #If the file user specified exists
  48. Username=lucky
  49. num=1
  50.     while  [ $Username != "EOF" ]
  51.         do
  52.         Username=`grep "Username00$num" $Path | cut -f2 -d:`   #Extract Username from text file
  53. num=$(($num+1))
  54.         Password=`grep "Password" $Path | cut -f2 -d:`       #Extract Password from text file
  55.         Group=`grep "Group" $Path |cut -f2 -d:`              #Extract Group From text file
  56.        
  57.         groupadd $Group    
  58.        
  59.                        #Adds user to the system and gives them a password
  60.            if [ $Username == "EOF" ]; then
  61.                    clear
  62.                    main
  63.            fi
  64.                 #Adds user to the system
  65.        
  66.                 useradd -g $Group -s "/bin/bash" -d /home/$Username -m $Username
  67.                 #Add users password
  68.                 #echo $Password | /usr/bin/passwd --stdin $Username #user Password will be assigned
  69.                 echo -e "$Password\n$Password\n" | sudo passwd $Username
  70.     done
  71. else  #If the user Specified file doesn't Exists
  72.     echo -e "\n#############################################"
  73.     echo -e "\n######CANNOT FIND or LOCATE THE FILE!!!!#####"
  74.     echo -e "\n#############################################"
  75. fi;;
  76. *) echo -e "You have selected the Wrong Choice!!!"
  77. esac
  78.        
  79. }
  80. varify()
  81. {
  82.     echo -e "#################################"
  83.     echo -e "Please Select the Mode!!!\n"
  84.     echo -e "1.Varify All the Users of System\n
  85. 2.Varify All the Users of TEXT file\n"
  86.     echo -e "#################################"
  87.     read varify_user
  88. case $varify_user in
  89.         1) cat /etc/passwd |grep bash;;
  90.         2)
  91.     echo
  92.         echo "Present working directory is: `pwd`/users.txt"
  93.         echo
  94.         echo -e "Do you want to use the above Default PATH? Yes=1 & No=2"
  95.         read yn
  96. if [ $yn == 1 ]; then
  97.                      Path=$($pwd)users.txt
  98. else
  99.         echo -n "Please enter the correct path to the file (e.g. /root/folder/filename.txt): "
  100.         read Path
  101. fi
  102. if [ -e $Path ]; then
  103.     Path=$($pwd)users.txt
  104.     varify=`grep "varify" $Path |cut -f2 -d:`
  105.       cat  /etc/passwd | grep $varify
  106.   echo -e "\nYou have Currently "
  107.           cat /etc/passwd | grep $varify |wc -l
  108.       echo  "users added from your Text file"
  109. else  #If the user Specified file doesn't Exists
  110.       echo -e "\n#############################################"
  111.       echo -e "\n######CANNOT FIND or LOCATE THE FILE!!!!#####"
  112.       echo -e "\n#############################################"
  113. fi ;;
  114. *) echo -e "Wrong Choice"
  115. esac
  116.                      
  117. }
  118.  
  119. del_users()
  120. {
  121. #This Script will delete the Users from the HOME DIRECTORY!!!!
  122. ROOT_UID=0      #The root user has a UID of 0
  123.     if      [ "$UID" -ne "$ROOT_UID" ]; then
  124.             echo "****You must be the root user to run this script!****"
  125.             exit
  126.     fi
  127.     echo
  128.     echo Identity Verified_You are the Root
  129.     echo
  130.    
  131.     echo
  132.     echo "Present working directory is: `pwd`/students.txt"
  133.     echo
  134.    
  135. #This is the Menu to select the mode to Deletion the users, either delete selected user or to delete all the users you have in the TEXT file...???
  136. echo -e "####################################"
  137. echo -e "\nPlease select the Mode!!!\n
  138. 1.Delete Specific User\n
  139. 2.Delete all Users Specified in the TEXT File\n"
  140. echo -e "####################################"
  141. read del_opt
  142. case $del_opt in
  143.     1)
  144.         echo -e "\n\nYou have currently following USERS Added to your System\n"
  145.          cat  /etc/passwd |grep bash
  146.         echo -e "\n\n Type the Name of the User you wants to Delete :"
  147.         read user_name
  148.         userdel -r $user_name ;;
  149.     2)
  150.        echo
  151.        echo "Present working directory is: `pwd`/users.txt"
  152.        echo
  153.        echo -e "Do you want to use the above Default PATH? Yes=1 & No=2"
  154.        read yn
  155. if [ $yn == 1 ]; then
  156.        Path=$($pwd)users.txt
  157. else
  158.        echo -n "Please enter the correct path to the file (e.g. /root/folder/filename.txt): "
  159.        read Path
  160. fi
  161. if [ -e $Path ]; then       #If the file user specified exists
  162.         num=1
  163. Username=lucky     
  164.         while  [ $Username != "EOF" ]
  165.         do
  166.         Username=`grep "Username00$num" $Path | cut -f2 -d:`   #Extract Username from text file
  167.    
  168.         if [ $Username == "EOF" ]; then
  169.                 clear
  170.             main
  171.         fi
  172.                                        
  173.                 userdel -r $Username
  174. num=$(($num+1))
  175.     done
  176. else  #If the user Specified file doesn't Exists
  177.     echo -e "\n#############################################"
  178.     echo -e "\n######CANNOT FIND or LOCATE THE FILE!!!!#####"
  179.     echo -e "\n#############################################"
  180. fi ;;
  181. *) echo -e "Wrong Choice"
  182. esac
  183. }
  184.  
  185. main()
  186. {
  187.     opt=1
  188. while [ $opt -le 4 ]
  189. do
  190.     clear
  191. echo -e "           ### MENU ###\n
  192.             1. ADD USERS\n
  193.             2. Varify Users\n
  194.             3. Delete Users\n
  195.             4. EXIT\n"
  196. read opt
  197. case $opt in
  198.     1) add_users ;;
  199.     2) varify ;;
  200.     3) del_users ;;
  201.     4) exit 0 ;;
  202.     *) echo -e "You have Entered the Wrong Choice!!!"
  203. esac
  204. echo -e "\nWant to run Script Again Yes=1 & No=4."
  205. read opt
  206. done
  207. }
  208. main
  209. exit 0
Add Comment
Please, Sign In to add comment