Effective management of user accounts is a cornerstone of IT security. Among the various measures to maintain a secure environment, monitoring inactive or unused accounts is critical. Inactive accounts can be potential entry points for malicious activities if left unchecked. To address this, we will delve into a robust Bash script that alerts administrators when user accounts remain inactive for a specified number of days.
Background
Inactive user accounts pose significant risks in any IT infrastructure. They can be exploited for unauthorized access, leading to potential data breaches. Managed Service Providers (MSPs) and IT professionals need efficient tools to track and manage such accounts. This script serves as a powerful solution, offering automated alerts for inactive accounts on Linux systems, enhancing the overall security posture.
The Script:
#!/usr/bin/env bash # # Description: Alerts when there is an inactive/unused account that has not logged in for the specified number of days. # # Preset Parameter: --daysInactive "90" # Alert if account has been inactive for x days. # # Preset Parameter: --showDisabled # Includes disabled accounts in alert and report. # # Preset Parameter: --multilineField "ReplaceMeWithNameOfYourMultilineField" # Name of an optional multiline custom field to save the results to. # # Preset Parameter: --wysiwygField "ReplaceMeWithNameOfYourWYSIWYGField" # Name of an optional WYSIWYG custom field to save the results to. # # Preset Parameter: --help # Displays some help text. # # Release Notes: Initial Release # By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use. # Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. # Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. # Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. # Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. # Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. # Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. # EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA). # # These are all our preset parameter defaults. You can set these = to something if you would prefer the script defaults to a certain parameter value. _arg_daysInactive= _arg_showDisabled="off" _arg_multilineField= _arg_wysiwygField= # Help text function for when invalid input is encountered print_help() { printf '\n\n%s\n\n' 'Usage: --daysInactive|-d <arg> [--multilineField|-m <arg>] [--wysiwygField|-w <arg>] [--showDisabled] [--help|-h]' printf '%s\n' 'Preset Parameter: --daysInactive "90"' printf '\t%s\n' "Alert if account has been inactive for x days." printf '%s\n' 'Preset Parameter: --showDisabled' printf '\t%s\n' "Includes disabled accounts in alert and report." printf '%s\n' 'Preset Parameter: --multilineField "ReplaceMeWithNameOfYourMultilineField"' printf '\t%s\n' "Name of an optional multiline custom field to save the results to." printf '%s\n' 'Preset Parameter: --wysiwygField "ReplaceMeWithNameOfYourWYSIWYGField"' printf '\t%s\n' "Name of an optional WYSIWYG custom field to save the results to." printf '\n%s\n' 'Preset Parameter: --help' printf '\t%s\n' "Displays this help menu." } # Determines whether or not help text is necessary and routes the output to stderr die() { local _ret="${2:-1}" echo "$1" >&2 test "${_PRINT_HELP:-no}" = yes && print_help >&2 exit "${_ret}" } # Converts a string input into an HTML table format. convertToHTMLTable() { local _arg_delimiter=" " local _arg_inputObject # Process command-line arguments for the function. while test $# -gt 0; do _key="$1" case "$_key" in --delimiter | -d) test $# -lt 2 && echo "[Error] Missing value for the required argument" >&2 && return 1 _arg_delimiter=$2 shift ;; --*) echo "[Error] Got an unexpected argument" >&2 return 1 ;; *) _arg_inputObject=$1 ;; esac shift done # Handles missing input by checking stdin or returning an error. if [[ -z $_arg_inputObject ]]; then if [ -p /dev/stdin ]; then _arg_inputObject=$(cat) else echo "[Error] Missing input object to convert to table" >&2 return 1 fi fi local htmlTable="<table>\n" htmlTable+=$(printf '%b' "$_arg_inputObject" | head -n1 | awk -F "$_arg_delimiter" '{ printf "<tr>" for (i=1; i<=NF; i+=1) { printf "<th>"$i"</th>" } printf "</tr>" }') htmlTable+="\n" htmlTable+=$(printf '%b' "$_arg_inputObject" | tail -n +2 | awk -F "$_arg_delimiter" '{ printf "<tr>" for (i=1; i<=NF; i+=1) { printf "<td>"$i"</td>" } print "</tr>" }') htmlTable+="\n</table>" printf '%b' "$htmlTable" '\n' } # Parses command-line arguments and sets script variables accordingly. parse_commandline() { while test $# -gt 0; do _key="$1" case "$_key" in --daysInactive | --daysinactive | --days | -d) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_daysInactive=$2 shift ;; --daysInactive=*) _arg_daysInactive="${_key##--daysInactive=}" ;; --multilineField | --multilinefield | --multiline | -m) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_multilineField=$2 shift ;; --multilineField=*) _arg_multilineField="${_key##--multilineField=}" ;; --wysiwygField | --wysiwygfield | --wysiwyg | -w) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_wysiwygField=$2 shift ;; --wysiwygField=*) _arg_wysiwygField="${_key##--wysiwygField=}" ;; --showDisabled | --showdisabled) _arg_showDisabled="on" ;; --help | -h) _PRINT_HELP=yes die 0 ;; *) _PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1 ;; esac shift done } # Parses the command-line arguments passed to the script. parse_commandline "$@" # If dynamic script variables are used replace the commandline arguments with them. if [[ -n $daysInactive ]]; then _arg_daysInactive="$daysInactive" fi if [[ -n $includeDisabled && $includeDisabled == "true" ]]; then _arg_showDisabled="on" fi if [[ -n $multilineCustomFieldName ]]; then _arg_multilineField="$multilineCustomFieldName" fi if [[ -n $wysiwygCustomFieldName ]]; then _arg_wysiwygField="$wysiwygCustomFieldName" fi # Check if _arg_daysInactive contains any non-digit characters or is less than zero. # If any of these conditions are true, display the help text and terminate with an error. if [[ -z $_arg_daysInactive || $_arg_daysInactive =~ [^0-9]+ || $_arg_daysInactive -lt 0 ]]; then _PRINT_HELP=yes die "FATAL ERROR: Days Inactive of '$_arg_daysInactive' is invalid! Days Inactive must be a positive number." 1 fi # Check if both _arg_multilineField and _arg_wysiwygField are set and not empty. if [[ -n "$_arg_multilineField" && -n "$_arg_wysiwygField" ]]; then # Convert both field names to uppercase to check for equality. multiline=$(echo "$_arg_multilineField" | tr '[:lower:]' '[:upper:]') wysiwyg=$(echo "$_arg_wysiwygField" | tr '[:lower:]' '[:upper:]') # If the converted names are the same, it means both fields cannot be identical. # If they are, terminate the script with an error. if [[ "$multiline" == "$wysiwyg" ]]; then _PRINT_HELP=no die 'FATAL ERROR: Multline Field and WYSIWYG Field cannot be the same name. https://ninjarmm.zendesk.com/hc/en-us/articles/360060920631-Custom-Fields-Configuration-Device-Role-Fields' fi fi # Retrieves the list of user accounts with UniqueIDs greater than 499 (typically these are all not service accounts) from the local user directory. userAccounts=$(cut -d ":" -f1,3 /etc/passwd | grep -v "nobody" | awk -F ':' '$2 >= 1000 {print $1}') # Sets up a header string for the table that will display user account information. header=$(printf '%s' "Username" ';' "Password Last Set" ';' "Last Logon" ';' "Enabled") # Initializes an empty string to store information about relevant user accounts. relevantAccounts= # Iterates over each user account retrieved earlier. for userAccount in $userAccounts; do # Extracts the last login information for that user, filtering out unnecessary lines and formatting. lastLogin=$(last -RF1 "$userAccount" | grep -v "wtmp" | grep "\S" | tr -s " " | cut -f3-7 -d " ") # Converts the last login date to seconds since the epoch, for easier date comparison. if [[ -n $lastLogin ]]; then lastLogin=$(date -d "$lastLogin" +"%s") fi # Calculates the cutoff date in seconds since the epoch for inactivity comparison, based on the days inactive argument. if [[ $_arg_daysInactive -gt 0 ]]; then cutoffDate=$(date -d "${_arg_daysInactive} days ago" +"%s") fi # Retrieves the timestamp when the password was last set for the user account and converts it to a readable format. passwordLastSet=$(passwd -S "$userAccount" | cut -f3 -d " ") # Checks if the user account is part of the group that defines disabled accounts, setting the 'enabled' variable accordingly. unlockedaccount=$(passwd -S "$userAccount" | cut -f2 -d " " | grep -v "L") nologin=$(grep "$userAccount" /etc/passwd | cut -d ":" -f7 | grep "nologin") if [[ -n $unlockedaccount && -z $nologin ]]; then enabled="true" else enabled="false" fi # Checks if the account is inactive based on the last login date and cutoff date or if the account should be included regardless of its active status. if [[ $_arg_daysInactive == "0" || -z "$lastLogin" || $lastLogin -le $cutoffDate ]]; then # Formats the last login date or sets it to "Never" if the user has never logged in. if [[ -n $lastLogin ]]; then lastLogin=$(date -d "@$lastLogin") else lastLogin="Never" fi # Skips adding disabled accounts to the output if they should not be shown. if [[ $_arg_showDisabled == "off" && $enabled == "false" ]]; then continue fi # Appends the account information to the 'relevantAccounts' string if it meets the criteria. relevantAccounts+=$(printf '%s' '\n' "$userAccount" ';' "$passwordLastSet" ';' "$lastLogin" ';' "$enabled") foundInactiveAccounts="true" fi done # Checks if there are any inactive accounts found. if [[ $foundInactiveAccounts == "true" ]]; then # Formats a nice table for easier viewing tableView="$header" tableView+=$(printf '%s' '\n' "--------" ';' "-----------------" ';' "----------" ';' "-------") tableView+="$relevantAccounts" tableView=$(printf '%b' "$tableView" | column -s ';' -t) # Output to the activity log echo "" echo 'WARNING: Inactive accounts detected!' echo "" printf '%b' "$tableView" echo "" else # If no inactive accounts were found, outputs a simple message. echo "No inactive accounts detected." fi # Checks if there is a multiline custom field set and if any inactive accounts have been found. if [[ -n $_arg_multilineField && $foundInactiveAccounts == "true" ]]; then echo "" echo "Attempting to set Custom Field '$_arg_multilineField'..." # Formats the relevantAccounts data for the multiline custom field. multilineValue=$(printf '%b' "$relevantAccounts" | grep "\S" | awk -F ";" '{ print "Username: "$1 print "Password Last Set: "$2 print "Last Logon: "$3 print "Enabled: "$4 print "" }') # Tries to set the multiline custom field using ninjarmm-cli and captures the output. if ! output=$(printf '%b' "$multilineValue" | /opt/NinjaRMMAgent/programdata/ninjarmm-cli set --stdin "$_arg_multilineField" 2>&1); then echo "[Error] $output" >&2 EXITCODE=1 else echo "Successfully set Custom Field '$_arg_multilineField'!" fi fi # Checks if there is a WYSIWYG custom field set and if any inactive accounts have been found. if [[ -n $_arg_wysiwygField && $foundInactiveAccounts == "true" ]]; then echo "" echo "Attempting to set Custom Field '$_arg_wysiwygField'..." # Initializes an HTML formatted string with headers and account details. htmlObject="$header" htmlObject+="$relevantAccounts" # Converts the text data to an HTML table format. htmlObject=$(convertToHTMLTable --delimiter ';' "$htmlObject") # Tries to set the WYSIWYG custom field using ninjarmm-cli and captures the output. if ! output=$(echo "$htmlObject" | /opt/NinjaRMMAgent/programdata/ninjarmm-cli set --stdin "$_arg_wysiwygField" 2>&1); then echo "[Error] $output" >&2 EXITCODE=1 else echo "Successfully set Custom Field '$_arg_wysiwygField'!" fi fi # Checks if an error code is set and exits the script with that code. if [[ -n $EXITCODE ]]; then exit "$EXITCODE" fi
Access over 300+ scripts in the NinjaOne Dojo
Detailed Breakdown
Let’s break down how this script operates to maintain user account hygiene on your Linux systems.
Preset Parameters
The script starts by defining preset parameters:
- –daysInactive: The number of days an account has to be inactive to trigger an alert.
- –showDisabled: Whether to include disabled accounts in the report.
- –multilineField: The name of a multiline custom field to save the results.
- –wysiwygField: The name of a WYSIWYG custom field to save the results.
- –help: Displays help text.
Command-Line Argument Parsing
The script employs a function to parse command-line arguments, setting script variables accordingly. If incorrect arguments are provided, the script displays a help message and exits.
Inactive Account Detection
The core functionality involves detecting inactive user accounts. It retrieves a list of user accounts with UniqueIDs greater than 499 (typically non-service accounts) from the local user directory. It then checks each account’s last login date and compares it with the current date minus the –daysInactive parameter. Accounts meeting the inactivity criteria are flagged.
Report Generation
If inactive accounts are detected, the script generates a formatted table displaying:
- Username
- Password Last Set
- Last Logon
- Enabled Status
Custom Field Integration
The script can save the results to specified custom fields. It formats the data into either plain text or HTML, depending on whether it’s saved to a multiline or WYSIWYG field, respectively.
Potential Use Cases
Consider an IT administrator managing a company’s Linux servers. Regular audits of user accounts are part of the security protocol. The administrator schedules this script to run weekly. One week, the script detects several inactive accounts that haven’t logged in for over 90 days. Upon reviewing the report, the administrator decides to disable these accounts until further verification, mitigating potential security risks.
Comparisons
Other methods to achieve similar results might include manual auditing or using more complex user management tools. Manual auditing is time-consuming and prone to human error. Advanced tools can be costly and may require extensive setup and configuration. This script offers a straightforward, cost-effective solution, making it an excellent middle ground.
FAQs
- What if I want to check for inactivity for a different number of days? Adjust the –daysInactive parameter accordingly. For instance, use –daysInactive 60 to check for 60 days of inactivity.
- Can I exclude disabled accounts from the report? Yes, the script includes the –showDisabled parameter to toggle the inclusion of disabled accounts.
- How do I save the results to a custom field? Specify the custom field names using the –multilineField or –wysiwygField parameters.
- What happens if I provide incorrect parameters? The script will display a help message and exit, ensuring you provide the correct inputs.
Implications
Regularly monitoring inactive accounts has profound implications for IT security. It helps prevent unauthorized access, reduces the attack surface, and ensures compliance with security policies. By using this script, administrators can automate a crucial aspect of user account management, enhancing overall security and efficiency.
Recommendations
- Schedule the script to run at regular intervals, such as weekly or monthly.
- Review the inactive accounts report promptly and take appropriate actions.
- Use the custom fields feature to integrate the results with your existing IT management tools.
Final Thoughts
Efficiently managing inactive user accounts is vital for maintaining IT security. This script provides a practical and automated solution, empowering administrators to stay ahead of potential security threats. By incorporating this tool into your security protocols, you can ensure a more secure and compliant IT environment.
NinjaOne offers comprehensive IT management solutions that seamlessly integrate with scripts like this, providing enhanced functionality and centralized management for your IT infrastructure. Embrace automation and robust security practices with NinjaOne to safeguard your organization effectively.