-
Couldn't load subscription status.
- Fork 769
shell scripting project #1 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
norfluxX
wants to merge
11
commits into
LondheShubham153:main
Choose a base branch
from
norfluxX:proj1-shell-script
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bd99c61
shell scripting project #1
norfluxX 89b33c2
Add pam-main.sh with recommended changes
norfluxX 8a74275
Fixed while loop validation and safer delete guard in pam-main.sh
norfluxX 115dd6a
Fixed break to continue in pam-main.sh
norfluxX 1c2c1e9
Update pam-main.sh
norfluxX 373a869
Update pam-main.sh
norfluxX c6aaf8c
modified pam-main.sh with working enchancements
norfluxX 0dca2ad
Merge branch 'proj1-shell-script' of https://github.com/norfluxX/Shel…
norfluxX 70d4b4c
modified pam-main.sh with working enchancements
norfluxX e2a9eec
modified pam-main.sh with backup validation
norfluxX 0edfd17
modified pam-main.sh to make nologin more stealth
norfluxX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| #/bin/bash | ||
| # Date: 10/09/2025 | ||
| # This is script for user & group management in linux. | ||
| # Usage: pam-main.sh | ||
| # Author: Bhikesh Khute | ||
| RED='\033[0;31m' | ||
| GREEN='\033[0;32m' | ||
| YELLOW='\033[1;33m' | ||
| WHITE='\033[1;37m' | ||
| PURPLE='\033[0;35m' | ||
| CYAN='\033[0;36m' | ||
| NC='\033[0m' | ||
| echo "------------------------------------------------------" | ||
| echo -e "=======\033[45m Welcome to Login Management System \033[0m=======" | ||
| echo "------------------------------------------------------" | ||
| while true; do | ||
| if [[ $EUID -ne 0 ]]; then | ||
| echo "This script must be run as root (sudo)."; exit 1 | ||
| fi | ||
| done | ||
| while true; do | ||
| echo -e "${RED}1. Add user${NC}" | ||
| echo -e "${GREEN}2. Delete user${NC}" | ||
| echo -e "${PURPLE}3. Modify user - Add user to multiple groups${NC}" | ||
| echo -e "${CYAN}4. Modify user - Changing Login Shell${NC}" | ||
| echo -e "${YELLOW}5. Modify user - Disable Login${NC}" | ||
| echo -e "${GREEN}6. Modify user - Enable Login${NC}" | ||
| echo -e "${CYAN}7. Modify user - Add user to sudo group${NC}" | ||
| echo -e "${RED}8. Add group${NC}" | ||
| echo -e "${GREEN}9. Backup directories${NC}" | ||
| echo -e "${PURPLE}10. Exit${NC}" | ||
| read -p "Select the option: " choice | ||
| case $choice in | ||
| 1) | ||
| read -rp "Enter first and last name of the user(Example - Ramesh Sippy): " fullname #Example - Ramesh Sippy | ||
| username="$(awk '{print tolower($1 $2)}' <<<"$fullname")" | ||
| if id -u "$username" >/dev/null 2>&1; then | ||
| echo "User '$username' already exists."; echo "-----------------------------------------------"; unset username; break | ||
norfluxX marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fi | ||
| if useradd -c "$fullname" -m -s /bin/bash "$username" >/dev/null 2>&1; then | ||
| randpass="$(openssl rand -base64 14 2>/dev/null || tr -dc 'A-Za-z0-9!@#%+=' </dev/urandom | head -c16)" | ||
| printf '%s:%s\n' "$username" "$randpass" | chpasswd | ||
| passwd --expire "$username" >/dev/null | ||
| echo "$username created. Temporary password: $randpass (will be forced to change on first login)" | ||
| else | ||
| echo "Failed to create user '$username'." | ||
| fi | ||
| echo "-----------------------------------------------" | ||
| unset username | ||
| unset randpass | ||
| ;; | ||
| 2) | ||
| awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " | ||
| read -r -p "Select user NUMBER to be deleted: " choice | ||
| [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } | ||
| username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" | ||
| if [[ -z "${username:-}" ]]; then echo "Invalid selection."; else | ||
| current="${SUDO_USER:-$USER}" | ||
| if [[ "$username" == "$current" ]]; then echo "Refusing to delete the current user '$current'."; else | ||
| read -r -p "Type the username '$username' to confirm deletion: " confirm | ||
| if [[ "$confirm" == "$username" ]]; then | ||
| if userdel -r "$username" >/dev/null 2>&1; then | ||
| echo "$username deleted." | ||
| else | ||
| echo "Failed to delete '$username'." | ||
| fi | ||
| else | ||
| echo "Confirmation mismatch. Aborting." | ||
| fi | ||
| fi | ||
| fi | ||
| echo "-------------------------------------------------" | ||
| unset username | ||
| unset choice | ||
| unset current | ||
| ;; | ||
| 3) | ||
| echo "------------------Normal Users List------------------" | ||
| awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users | ||
| echo "------------------Group List-------------------------" | ||
| getent group | awk -F: '{print $1}' | nl -w1 -s". " | ||
| read -p "Select the existing user NUMBER: " choice | ||
| read -p "Select the group NUMBER to be added: " gchoice | ||
| username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") | ||
| grpname=$(awk -F: '$3 >= 1000 {print $1}' /etc/group | sed -n "${gchoice}p") | ||
| if [[ -n "${username:-}" && -n "${grpname:-}" ]]; then | ||
| usermod -aG "$grpname" "$username" && echo "$username added to group $grpname" | ||
| else | ||
| echo "Invalid selection." | ||
norfluxX marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fi | ||
| echo "-------------------------------------------------" | ||
| unset grpname | ||
| unset username | ||
| unset choice | ||
| unset gchoice | ||
| ;; | ||
| 4) | ||
| echo "------------------Normal Users List------------------" | ||
| awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users | ||
| read -p "Select the existing username: " choice | ||
| username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") | ||
| curr_shell=$(getent passwd "$username" | cut -d: -f7) | ||
| if [ "$curr_shell" = "/bin/sh" ]; then | ||
| chsh -s /bin/bash "$username" | ||
| echo "$username's shell is successfully changed to bash" | ||
| echo "-------------------------------------------------" | ||
| elif [ "$curr_shell" = "/bin/bash" ]; then | ||
| chsh -s /bin/sh "$username" | ||
| echo "$username's shell is successfully changed to sh" | ||
| echo "-----------------------------------------------" | ||
| fi | ||
norfluxX marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| unset username | ||
| unset curr_shell | ||
| unset choice | ||
| ;; | ||
| 5) | ||
| echo "------------------Normal Users List------------------" | ||
| awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users | ||
| read -p "Select the existing username: " choice | ||
| username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") | ||
| if getent passwd "$username" > /dev/null; then | ||
| nologin_path="$(command -v nologin || echo /sbin/nologin)" | ||
| echo "Changing $username shell to $nologin_path" | ||
| usermod -s "$nologin_path" "$username" | ||
| echo "$username's login disabled." | ||
| echo "-------------------------------" | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else | ||
| echo "User $username not found!" | ||
| fi | ||
| unset username | ||
| unset choice | ||
| unset nologin_path | ||
| ;; | ||
| 6) | ||
| echo "------------------Normal Users List------------------" | ||
| awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users | ||
| read -rp "Select the existing user: " choice | ||
| [[ "$choice" =~ ^[0-9]+$ ]] || { echo "Invalid selection."; continue; } | ||
| username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" | ||
| if [[ -n "${username}" ]] && getent passwd "$username" > /dev/null; then | ||
| echo "Changing $username shell to /bin/bash" | ||
| usermod -s /bin/bash "$username" | ||
| echo "---------------------------------------------------" | ||
| else | ||
| echo "User $username not found!" | ||
| fi | ||
| unset username | ||
| unset choice | ||
| ;; | ||
| 7) | ||
| echo "------------------Normal Users List------------------" | ||
| awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " #Filtering only normal users | ||
| read -p "Enter the existing username: " choice | ||
| username=$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p") | ||
| admin_group="$(getent group sudo >/dev/null 2>&1 && echo sudo || echo wheel)" | ||
| usermod -aG "$admin_group" "$username" | ||
| echo "$username added to $admin_group group." | ||
| echo "----------------------------------------------------------------------" | ||
| unset username | ||
| unset choice | ||
| unset admin_group | ||
| ;; | ||
| 8) | ||
| read -r -p "Enter the group name to be added: " grpname | ||
| if getent group "$grpname" >/dev/null 2>&1; then | ||
| echo "Group '$grpname' already exists." | ||
| elif groupadd "$grpname" >/dev/null 2>&1; then | ||
| echo "--------------------$grpname added.------------------" | ||
| else | ||
| echo "Failed to add group '$grpname'." | ||
| fi | ||
| ;; | ||
| 9) | ||
| mkdir -p /opt/dir_backups | ||
| awk -F: '$3 >= 1000 {print $1}' /etc/passwd | nl -w1 -s". " | ||
| read -r -p "Select the user NUMBER to associate with the backup: " choice | ||
| username="$(awk -F: '$3 >= 1000 {print $1}' /etc/passwd | sed -n "${choice}p")" | ||
| read -r -p "Specify the absolute path of the directory to be backed up: " dirback | ||
| if [[ -d "$dirback" ]]; then | ||
| ts="$(date +%F_%H-%M-%S)" | ||
| tar -czf "/opt/dir_backups/${username}_$ts.tar.gz" -C "$dirback" . >/dev/null 2>&1 && \ | ||
| echo "Backup completed successfully" || echo "Backup failed" | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else | ||
| echo "Directory '$dirback' not found." | ||
| fi | ||
| echo "-----------------------------" | ||
| unset username | ||
| unset dirback | ||
| unset choice | ||
| unset ts | ||
| ;; | ||
| 10) | ||
| break; | ||
| ;; | ||
| *) | ||
| echo "Invalid option. Try again." | ||
| ;; | ||
| esac | ||
| done | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.