Advertisement
Guest User

CJ's User account creation tool (6/16/2017)

a guest
Jun 16th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #!/bin/bash
  2. echo "==============================v2============================="
  3. echo " CJ'S USER AND GROUP CREATION TOOL"
  4. echo " Please keep your input limited to one argument"
  5. echo " - Run the script as root/sudo for guarenteed completion - "
  6. echo "================= PRESS ANY KEY TO CONTINUE ================="
  7. read -n 1 -s -p $'=============================================================\n'
  8.  
  9. #Creating Username Variable
  10. echo ""
  11. echo -n "Desired Username: "
  12. read username
  13.  
  14. #Checking that user input != nothing
  15. while [ "$username" = "" ];
  16. do
  17. echo ""
  18. echo "Username field cannot be empty."
  19. sleep 0.5
  20. echo -n "Please enter a username: "
  21. read username
  22. done
  23.  
  24.  
  25. #Checking if username already exists within the /etc/passwd file
  26. while grep -q -F $username /etc/passwd;
  27. do
  28. echo ""
  29. echo "Username already exists. Please try again."
  30. sleep 0.5
  31. echo -n "Desired Username: "
  32. read username
  33. while [ "$username" = "" ];
  34. do
  35. echo ""
  36. echo "Username field cannot be empty."
  37. sleep 0.5
  38. echo -n "Please enter a username: "
  39. read username
  40. done
  41. done
  42. echo ""
  43. echo -n "Desired Groupname: "
  44. read groupname
  45.  
  46. #Checking if user enters nothing, therefore prompting the user to enter a groupname.
  47. while [ "$groupname" = "" ];
  48. do
  49. echo ""
  50. echo "Groupname field cannot be empty."
  51. sleep 0.5
  52. echo -n "Please enter a groupname: "
  53. read groupname
  54. done
  55.  
  56. #Checking if groupname already exists within /etc/group file
  57. while grep -q -F $groupname /etc/group;
  58. do
  59. echo ""
  60. echo "Group already exists. Please try again."
  61. echo -n "Desired Groupname: "
  62. read groupname
  63. while [ "$username" = "" ];
  64. do
  65. echo ""
  66. echo "Groupname field cannot be empty."
  67. echo -n "Please enter a groupname: "
  68. read username
  69. done
  70. done
  71. echo ""
  72. echo -n "Desired Password: "
  73. read -s password
  74.  
  75. #Litle bit of code that sends message if the password is less than 5 chars long
  76. while [ ${#password} -lt 5 ];
  77. do
  78. echo ""
  79. echo "Your password must contain 5+ characters"
  80. sleep 0.2
  81. echo "Please try again."
  82. sleep 0.5
  83. echo ""
  84. echo -n "Desired Password: "
  85. read -s password
  86. done
  87. echo ""
  88. sleep 1
  89. read -p "Initialize user/group creation? (Y/N)" yn
  90. case $yn in
  91. [Yy]* ) ;;
  92. [Nn]* ) exit;;
  93. * ) echo "Please answer yes or no.";;
  94. esac
  95. #Creating the user and group and adding the user to the group
  96. sudo useradd $username
  97. sudo groupadd $groupname
  98. echo $username:$password | sudo chpasswd
  99. sudo adduser $username $groupname
  100. sleep 1
  101. echo "No errors = Successful"
  102. sleep 0.2
  103. echo "Thank you for using the tool!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement