Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #/bin/bash
  2. #
  3. # This script is used to Create a User Account with SUDO Privileges!!!,
  4. # Display the Password and force the User to change that Password upon the first Login!
  5. #
  6. # Version: 1.0
  7. # Author: ham5ter@ham5ter.de
  8. # Date: 02.08.2016
  9.  
  10.  
  11. main(){
  12. # Default values. These Values can be overwritten with Comandline Arguments
  13. FORCE_PASSWORD_CHANGE=true
  14. ADD_TO_SUDO_GROUP=true
  15. # Parse arguments
  16. parse_arguments $@
  17. # Add the User
  18. if id -u "$1" >/dev/null 2>&1; then
  19. echo "Error: The User \"$USERNAME\" already exists!"
  20. exit 1
  21. else
  22. adduser --disabled-password --gecos "" $USERNAME
  23. fi
  24. # Add the User to the SUDO Group
  25. if [ "$ADD_TO_SUDO_GROUP" = true ] ; then
  26. usermod -aG sudo $USERNAME
  27. fi
  28. # Set the Password
  29. if [ "$USE_PASSWORD" = true ] ; then
  30. PASSWORD_OF_THE_NEW_USER=$PROVIDED_PASSWORD
  31. else
  32. PASSWORD_OF_THE_NEW_USER=$(genpasswd)
  33. fi
  34. # Force Password Change on Login!
  35. echo $USERNAME:$PASSWORD_OF_THE_NEW_USER | chpasswd
  36. if [ "$FORCE_PASSWORD_CHANGE" = true ] ; then
  37. chage -d 0 $1
  38. fi
  39. # Display Informations
  40. echo ""
  41. echo "###"
  42. echo "# The Password for the Account \"$USERNAME\" is: \"$PASSWORD_OF_THE_NEW_USER\""
  43. echo "###"
  44. }
  45.  
  46. genpasswd() {
  47. local l=$1
  48. [ "$l" == "" ] && l=16
  49. tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
  50. }
  51.  
  52. usage() {
  53. cat 1>&2 <<EOF
  54. Usage $(basename $0) USERNAME [OPTIONS]
  55. This script is used to Create a User Account with SUDO Privileges.
  56. -v|--verbose Make the Script verbose.
  57. -h|--help Print this help message.
  58. -p|--password <p> Set the Password of the new user to <p>.
  59. -n|--no-reset Dont ask the user to change his Password.
  60. -s|--no-sudo Dont add the user to the sudo Group.
  61. EOF
  62. }
  63.  
  64. parse_arguments(){
  65. if [ $# -eq 0 ]; then
  66. echo "Error: Missing Argument"
  67. usage
  68. exit 1
  69. fi
  70. PARAMS=""
  71. while (( "$#" )); do
  72. case "$1" in
  73. -v|--verbose)
  74. set -e
  75. set -x
  76. shift 1
  77. ;;
  78. -h|--help)
  79. usage
  80. exit 0
  81. ;;
  82. -n|--no-reset)
  83. FORCE_PASSWORD_CHANGE=false
  84. shift 1
  85. ;;
  86. -s|--no-sudo)
  87. ADD_TO_SUDO_GROUP=false
  88. shift 1
  89. ;;
  90. -p|--password)
  91. PROVIDED_PASSWORD=$2
  92. USE_PASSWORD=true
  93. shift 2
  94. ;;
  95. --) # end Argument parsing
  96. shift
  97. break
  98. ;;
  99. -*|--*=) # unsupported Argument
  100. echo "Error: Unsupported Argument '$1'" >&2
  101. usage
  102. exit 1
  103. ;;
  104. *) # preserve positional Arguments
  105. USERNAME="$PARAMS$1"
  106. shift
  107. ;;
  108. esac
  109. done
  110. }
  111.  
  112. main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement