Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- TODAY=$(date)
- HOST=$(hostname)
- # Modify the new user creation helpdesk script to handle STDERR.
- # Make sure the script is being executed with superuser privileges.
- if [[ "${UID}" -eq 0 ]]
- then
- echo 'You are root.' >&2
- else
- echo 'You are not root.' >&2
- echo 'Please use root privileges.' >&2
- exit 1
- fi
- # If the user doesn't supply at least one argument, then give them help.
- if [[ "${#}" -lt 1 ]]
- then
- echo "Usage: ${0} USER_NAME [COMMENT]..." >&2
- echo 'Create an account on the local system with the name of USER_NAME and a comments field of COMMENT.' >&2
- exit 1
- fi
- # The first parameter is the user name.
- USER_NAME="${1}"
- # The rest of the parameters are for the account comments.
- shift
- COMMENT="${@}"
- # Generate a password.
- PASSWORD=$(date +%s%N | sha256sum | head -c48)
- # Create a user with the password.
- useradd -c "${COMMENT}" -m ${USER_NAME} &> /dev/null
- # Check to see if the useradd command succeeded.
- if [[ "${?}" -ne 0 ]]
- then
- echo 'The account could not be created.' >&2
- exit 1
- fi
- # Set the password.
- echo ${PASSWORD} | passwd --stdin ${USER_NAME} &> /dev/null
- # Check to see if the passwd command succeeded.
- if [[ "${?}" -ne 0 ]]
- then
- echo 'The password for the account could not be set.' >&2
- exit 1
- fi
- # Force password change on first login.
- passwd -e ${USER_NAME} &> /dev/null
- # Display the username, password, and the host where the user was created.
- echo 'username:'
- echo "${USER_NAME}"
- echo
- echo 'password:'
- echo "${PASSWORD}"
- echo
- echo 'host:'
- echo "${HOSTNAME}"
- exit 0
Add Comment
Please, Sign In to add comment