Notes
Notes - notes.io |
# menu.sh - A script for managing users and logging actions
# Interrupt handler
interrupt_manager() {
echo
echo -e " 33[0;31mOperation terminated by user."
echo "Exited with error code 1."
exit 1
}
trap interrupt_manager SIGINT
# Log action function
log_action() {
local action="$1"
local username="$2"
local status="$3"
local details="$4"
local user_group="$(id -gn)"
LOG_FILE="/var/log/user_management.log"
echo "$(date '+%Y-%m-%d %H:%M:%S') - Action: $action - User: $username - Group: $user_group - Status: $status - Details: $details" >> "$LOG_FILE"
}
# Add user function
add_user() {
read -rp "Please enter your desired username: " username
read -rsp "Please enter your desired password: " password
echo
read -rp "Please enter your full name: " fullname
read -rp "Please enter your room number: " roomnumber
read -rp "Please enter your work phone: " workphone
read -rp "Please enter your home phone: " homephone
if useradd "$username" && echo "$username:$password" | chpasswd && echo "$username:$fullname:$roomnumber" >> /etc/user_info.txt && chfn -f "$fullname" -r "$roomnumber" -w "$workphone" -h "$homephone" "$username"; then
log_action "CREATE" "$username" "SUCCESS" "Added user with full name $fullname and room $roomnumber."
echo "User $username added successfully."
return 0
else
log_action "CREATE" "$username" "FAILED" "Error adding user."
echo "Failed to add user $username."
return 1
fi
}
# Modify user function
modify_user() {
read -rp "Enter the username to modify: " username
if id "$username" &>/dev/null; then
while true; do
echo "What would you like to modify for $username?"
options=("Full name" "Room number" "Work phone" "Home phone" "Password" "Cancel")
select opt in "${options[@]}"; do
case $opt in
"Full name")
read -rp "Enter new full name: " fullname
if chfn -f "$fullname" "$username"; then
log_action "MODIFY" "$username" "SUCCESS" "Full name updated to $fullname."
return 0
else
log_action "MODIFY" "$username" "FAILED" "Failed to update full name."
return 3
fi
break
;;
"Room number")
read -rp "Enter new room number: " room
if chfn -r "$room" "$username"; then
log_action "MODIFY" "$username" "SUCCESS" "Room number updated to $room."
return 0
else
log_action "MODIFY" "$username" "FAILED" "Failed to update room number."
return 3
fi
break
;;
"Work phone")
read -rp "Enter new work phone: " work_phone
if chfn -w "$work_phone" "$username"; then
log_action "MODIFY" "$username" "SUCCESS" "Work phone updated to $work_phone."
return 0
else
log_action "MODIFY" "$username" "FAILED" "Failed to update work phone."
return 3
fi
break
;;
"Home phone")
read -rp "Enter new home phone: " home_phone
if chfn -h "$home_phone" "$username"; then
log_action "MODIFY" "$username" "SUCCESS" "Home phone updated to $home_phone."
return 0
else
log_action "MODIFY" "$username" "FAILED" "Failed to update home phone."
return 3
fi
break
;;
"Password")
if passwd "$username"; then
log_action "MODIFY" "$username" "SUCCESS" "Password updated."
return 0
else
log_action "MODIFY" "$username" "FAILED" "Failed to update password."
return 3
fi
break
;;
"Cancel")
return 0
;;
*)
echo -e " 33[0;33mInvalid option. Please try again. 33[0;0m"
;;
esac
done
done
else
log_action "MODIFY" "$username" "FAILED" "User does not exist."
echo -e " 33[0;31mUser $username does not exist. 33[0;0m"
return 3
fi
}
# Delete user function
delete_user() {
if [ ! -f /etc/user_info.txt ]; then
echo "User information file not found."
return 3
fi
read -rp "Enter the username to delete: " username
if id "$username" &>/dev/null; then
if userdel -r "$username" && grep -v "^$username:" /etc/user_info.txt > /etc/user_info_tmp.txt && mv /etc/user_info_tmp.txt /etc/user_info.txt; then
log_action "DELETE" "$username" "SUCCESS" "User deleted successfully."
echo "User $username deleted successfully."
return 0
else
log_action "DELETE" "$username" "FAILED" "Error deleting user."
echo "Failed to delete user $username."
return 3
fi
else
log_action "DELETE" "$username" "FAILED" "User does not exist."
echo "User $username does not exist."
return 3
fi
}
# List users function
list_users() {
if [ ! -f /etc/user_info.txt ]; then
echo "User information file not found."
return 3
fi
echo "Username | Full Name | Room Number"
echo "-----------------------------------------------"
while IFS=":" read -r username fullname roomnumber; do
printf "%-10s | %-20s | %-10sn" "$username" "$fullname" "$roomnumber"
done < /etc/user_info.txt
log_action "LIST" "N/A" "SUCCESS" "Listed all users."
}
# Main script
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 2
fi
echo "This script allows users to manage users of the current device via a simple interface."
PS3="Menu Choice: "
options=("Add user" "Delete user" "Modify user" "List all users" "Exit")
while true; do
echo -e "nPlease choose an option from the menu below:"
select option in "${options[@]}"
do
case $option in
"Add user")
echo -e " 33[0;34mYou chose to add a user.n 33[0;0m"
add_user
break
;;
"Delete user")
echo -e " 33[0;34mYou chose to delete a user.n 33[0;0m"
delete_user
break
;;
"Modify user")
echo -e " 33[0;34mYou chose to modify a user.n 33[0;0m"
modify_user
break
;;
"List all users")
echo -e " 33[0;34mYou chose to list all users.n 33[0;0m"
list_users
break
;;
"Exit")
echo -e " 33[0;32mYou chose to exit. Exited with error code 0. 33[0;0m"
exit 0
;;
*) # Invalid input
echo -e " 33[0;33mInvalid Input. Enter only the number of the option. 33[0;0m"
;;
esac
echo -e "n"
done
done
|
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team