Guest User

Untitled

a guest
Feb 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #!/bin/bash
  2. TODAY=$(date)
  3. HOST=$(hostname)
  4. # Modify the new user creation helpdesk script to handle STDERR.
  5.  
  6. # Make sure the script is being executed with superuser privileges.
  7. if [[ "${UID}" -eq 0 ]]
  8. then
  9. echo 'You are root.' >&2
  10. else
  11. echo 'You are not root.' >&2
  12. echo 'Please use root privileges.' >&2
  13. exit 1
  14. fi
  15.  
  16. # If the user doesn't supply at least one argument, then give them help.
  17. if [[ "${#}" -lt 1 ]]
  18. then
  19. echo "Usage: ${0} USER_NAME [COMMENT]..." >&2
  20. echo 'Create an account on the local system with the name of USER_NAME and a comments field of COMMENT.' >&2
  21. exit 1
  22. fi
  23.  
  24. # The first parameter is the user name.
  25. USER_NAME="${1}"
  26.  
  27. # The rest of the parameters are for the account comments.
  28. shift
  29. COMMENT="${@}"
  30.  
  31. # Generate a password.
  32. PASSWORD=$(date +%s%N | sha256sum | head -c48)
  33.  
  34. # Create a user with the password.
  35. useradd -c "${COMMENT}" -m ${USER_NAME} &> /dev/null
  36.  
  37. # Check to see if the useradd command succeeded.
  38. if [[ "${?}" -ne 0 ]]
  39. then
  40. echo 'The account could not be created.' >&2
  41. exit 1
  42. fi
  43.  
  44. # Set the password.
  45. echo ${PASSWORD} | passwd --stdin ${USER_NAME} &> /dev/null
  46.  
  47. # Check to see if the passwd command succeeded.
  48. if [[ "${?}" -ne 0 ]]
  49. then
  50. echo 'The password for the account could not be set.' >&2
  51. exit 1
  52. fi
  53.  
  54. # Force password change on first login.
  55. passwd -e ${USER_NAME} &> /dev/null
  56.  
  57. # Display the username, password, and the host where the user was created.
  58. echo 'username:'
  59. echo "${USER_NAME}"
  60. echo
  61. echo 'password:'
  62. echo "${PASSWORD}"
  63. echo
  64. echo 'host:'
  65. echo "${HOSTNAME}"
  66. exit 0
Add Comment
Please, Sign In to add comment