main
Anthony 2025-04-10 23:26:58 +08:00
parent 8c918671bd
commit f1541e6669
2 changed files with 95 additions and 132 deletions

View File

@ -136,52 +136,7 @@ jps_log_system_info() {
jps_log "DEBUG" "SSH Status: $(systemctl status sshd | grep Active | awk '{print $2}')" "$log_file"
jps_log "DEBUG" "=============================================" "$log_file"
}
# Function to get detailed user information including creation date
# This replaces the functionality of the retired userlogs.sh script
jps_get_user_info() {
local log_file=${1:-$LOG_DIR/list_users.log}
jps_log "INFO" "Retrieving detailed user information" "$log_file"
# Get SFTP users
local users=$(find /home/sftpusers -maxdepth 1 -mindepth 1 -type d -exec basename {} \;)
if [ -z "$users" ]; then
jps_log "INFO" "No SFTP users found" "$log_file"
return 0
fi
jps_log "INFO" "Found users, retrieving creation dates" "$log_file"
local result=""
# Process each user
for user in $users; do
# Get creation date from directory timestamp
local creation_date=$(stat -c "%y" "/home/sftpusers/$user" 2>/dev/null | cut -d. -f1)
# Try to get last password change as fallback
if [ -z "$creation_date" ]; then
if id "$user" &>/dev/null; then
creation_date=$(chage -l "$user" 2>/dev/null | grep "Last password change" | cut -d: -f2)
fi
fi
if [ -n "$creation_date" ]; then
result="${result}Username: $user - Created: $creation_date\n"
else
result="${result}Username: $user - Created: Unknown\n"
fi
done
if [ -n "$result" ]; then
jps_log "SUCCESS" "User information retrieved successfully" "$log_file"
echo -e "$result"
else
jps_log "WARNING" "Could not retrieve user information" "$log_file"
echo ""
fi
}
EOF
# Make the logging script executable
chmod +x /home/jelastic/add-sftp-user-addon/log_helper.sh
@ -530,19 +485,20 @@ actions:
# Log list users action
jps_log "INFO" "======== LISTING SFTP USERS ========" "list_users.log"
# Note: This functionality replaces the retired userlogs.sh script
# with a more efficient and integrated approach
# List users with proper error handling
jps_log "INFO" "Retrieving list of SFTP users" "list_users.log"
# Get detailed user information
USER_INFO=$(jps_get_user_info "list_users.log")
# Use a safer approach to listing
USERS_LIST=$(ls -ld /home/sftpusers/* 2>/dev/null | grep -v "total" | awk '{printf "Username: %s - Created: %s %s %s\n", substr($9, 17), $6, $7, $8}')
# Output the result
if [ -z "$USER_INFO" ]; then
# Check if any users were found
if [ -z "$USERS_LIST" ]; then
jps_log "WARNING" "No SFTP users found" "list_users.log"
echo ""
else
jps_log "SUCCESS" "Retrieved list of SFTP users" "list_users.log"
echo "$USER_INFO"
jps_log "DEBUG" "Found users: $(echo "$USERS_LIST" | wc -l)" "list_users.log"
echo "$USERS_LIST"
fi
jps_log "INFO" "======== USER LISTING COMPLETED ========" "list_users.log"

View File

@ -0,0 +1,7 @@
#!/bin/bash
getent passwd | awk -F: '/^user/ {print $1}' | while read -r user; do
# Attempt to get the user's creation date from the shadow file
creation_date=$(sudo chage -l "$user" | grep 'Last password change' | cut -d: -f2)
echo "$user was created on $creation_date"
done