Guest User

Untitled

a guest
Sep 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Make sure the script is being executed with superuser privileges.
  4. if [[ "${EUID}" -ne 0 ]]; then
  5. echo 'The script requires root priveledges'
  6. exit 1
  7. fi
  8.  
  9. # supply at least one argument otherwise return an exit status of 1
  10. if [[ "${#}" -lt 1 ]]
  11. then
  12. echo "Usage: ${0} USER_NAME [COMMENT]..."
  13. echo "Create an account name USER_NAME with a comment field of COMMENT"
  14. exit 1
  15. fi
  16.  
  17. # The first parameter is the user name.
  18. USER_NAME = "${1}"
  19. echo "${USER_NAME}"
  20.  
  21. # The rest of the parameters are for the account comments.
  22. shift
  23. COMMENT="${@}"
  24.  
  25. # Generate password.
  26. PASSWORD=$(date +%s%N | sha256sum | head -c48)
  27.  
  28. # Create the user with the password.
  29. useradd -c "${COMMENT}" -m ${USER_NAME}
  30.  
  31. # Check to see if the useradd command succeeded.
  32. if [[ "${?}" -ne 0 ]]
  33. then
  34. echo 'The account could not be created.'
  35. exit 1
  36. fi
  37.  
  38. # Set the password.
  39. echo ${PASSWORD} | passwd --stdin ${USER_NAME}
  40.  
  41. # Check to see if the passwd command succeeded.
  42. if [[ "${?}" -ne 0 ]]
  43. then
  44. echo 'The password for the account could not be set.'
  45. exit 1
  46. fi
  47.  
  48. # Force password change on first login.
  49. passwd -e ${USER_NAME}
  50.  
  51. # Display the username, password, and the host where the user was created.
  52. echo 'username:'
  53. echo "${USER_NAME}"
  54. echo 'password:'
  55. echo "${PASSWORD}"
  56. echo 'host:'
  57. echo "${HOSTNAME}"
  58. exit 0
Add Comment
Please, Sign In to add comment