Advertisement
mastechr

Untitled

Dec 12th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #!/bin/bash
  2. # Admin Script for adding / modifying / deleting users
  3. # Script created by Robert Pearce
  4. # Oct 2013
  5.  
  6. # id -u is used to identify the userid (number) of who
  7. # is currently running this script.
  8. # The value is then compared to 0 (zero), as root UID is 0 zero.
  9. # We know that we need to be root for these types of operation. It's
  10. # kind of pointless to do anything else unless we identify who
  11. # is running the script
  12.  
  13. id=`id -u`
  14. if [[ $id -ne 0 ]]; then
  15. # echoing the fact that some ran this as someone other than root
  16. echo "This script needs to be run as root"
  17. else
  18. # just testing to see what happens otherwise
  19. echo "You are root, or equivaent, so we can continue"
  20. fi
  21.  
  22. # Clear the screen to clean it up
  23. clear
  24.  
  25. # Declaring a variable called menu - setting it to yes. While this is true
  26. # the loop will continue to clear the screen, and draw the menu.
  27.  
  28. menu=yes
  29. while [ $menu = yes ];
  30. do
  31. clear
  32.  
  33.  
  34. # Using echo to display the menu. \t\t are tabs, and \n is a new line
  35. echo -e "\t\t User Account Admin Menu\n"
  36. echo -e "1.\t Add a New User\n"
  37. echo -e "2.\t Edit a User\n"
  38. echo -e "3.\t Delete an Existing User\n"
  39. echo -e "4.\t Exit the Program\n"
  40. echo -n "Enter Selection: "
  41. # read is a builtin command capturing the users selection
  42. # we called the variable "selection"
  43. read selection
  44.  
  45. echo $selection
  46.  
  47. # In this scenario, we can use a case - or menu style logic structure
  48. case $selection in
  49. 1) #Adding a User
  50. echo -e "Enter a user name:"
  51. read name
  52.  
  53. echo "Enter a comment:"
  54. read comment
  55.  
  56. echo "Enter a password: (warning not encrypted)"
  57. read password
  58.  
  59. echo "Adding "$name" "$comment""
  60. useradd -m -c $comment -p $password -s /bin/bash $name
  61.  
  62. ;;
  63. 2) #Edit a User
  64.  
  65. echo -e "\t\t User Modifications Admin Menu\n"
  66. echo -e "1.\t Change user password\n"
  67. echo -e "2.\t Add user to group\n"
  68. echo -e "3.\t Set user password to expire\n"
  69. echo -e "4.\t Exit the Program\n"
  70. echo -n "Enter Selection: "
  71.  
  72. read selection
  73. echo $selection
  74.  
  75. case $selection in
  76. 1)
  77. echo "Password Change"
  78. echo "Enter a username"
  79. read name
  80. echo "Enter a new password"
  81. read password
  82. passwd $name
  83.  
  84. ;;
  85. 2)
  86. echo "Add user to group:"
  87. echo "Enter a username:"
  88. read name
  89. echo "Enter a supp group name:"
  90. read group
  91. usermod -aG $group $name
  92.  
  93. ;;
  94. 3)
  95. echo "Set password expiry:"
  96. echo "Enter a username:"
  97. read name
  98. passwd -e $name
  99.  
  100. ;;
  101. 4)
  102. echo "Exiting"
  103. ;;
  104.  
  105. ;;
  106. 3) #Deleting a User
  107. echo "User Deletion *Warning*"
  108. echo "Enter a username"
  109. read name
  110. echo "Deleting..." $name
  111. userdel -rf $name
  112. ;;
  113. 4) #Exit Program
  114. echo "Exiting program..."
  115.  
  116. menu=no
  117. ;;
  118. *) #Capture all other input (error handling)
  119. echo "Unrecognized selection"
  120.  
  121. exit 1
  122. esac
  123. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement