Provide production-ready shell script templates for common Linux system administration tasks including backups, monitoring, user management, log analysis, and automation. These scripts serve as building blocks for security operations and penetration testing environments.
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
A direct command skips the review prompt. Inspect the source before running it.
Provide production-ready shell script templates for common Linux system administration tasks including backups, monitoring, user management, log analysis, and automation. These scripts serve as building blocks for security operations and penetration testing environments.
Provide production-ready shell script templates for common Linux system administration tasks including backups, monitoring, user management, log analysis, and automation. These scripts serve as building blocks for security operations and penetration testing environments.
#!/bin/bash
backup_dir="/path/to/backup"
source_dir="/path/to/source"# Create a timestamped backup of the source directory
tar -czf "$backup_dir/backup_$(date +%Y%m%d_%H%M%S).tar.gz""$source_dir"echo"Backup completed: backup_$(date +%Y%m%d_%H%M%S).tar.gz"
Remote Server Backup
#!/bin/bash
source_dir="/path/to/source"
remote_server="user@remoteserver:/path/to/backup"# Backup files/directories to a remote server using rsync
rsync -avz --progress "$source_dir""$remote_server"echo"Files backed up to remote server."
Backup Rotation Script
#!/bin/bash
backup_dir="/path/to/backups"
max_backups=5
# Rotate backups by deleting the oldest if more than max_backupswhile [ $(ls -1 "$backup_dir" | wc -l) -gt "$max_backups" ]; do
oldest_backup=$(ls -1t "$backup_dir" | tail -n 1)
rm -r "$backup_dir/$oldest_backup"echo"Removed old backup: $oldest_backup"doneecho"Backup rotation completed."
#!/bin/bash
threshold=90
# Monitor CPU usage and trigger alert if threshold exceeded
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d. -f1)
if [ "$cpu_usage" -gt "$threshold" ]; thenecho"ALERT: High CPU usage detected: $cpu_usage%"# Add notification logic (email, slack, etc.)# mail -s "CPU Alert" admin@example.com <<< "CPU usage: $cpu_usage%"fi
Disk Space Monitor
#!/bin/bash
threshold=90
partition="/dev/sda1"# Monitor disk usage and trigger alert if threshold exceeded
disk_usage=$(df -h | grep "$partition" | awk '{print $5}' | cut -d% -f1)
if [ "$disk_usage" -gt "$threshold" ]; thenecho"ALERT: High disk usage detected: $disk_usage%"# Add alert/notification logic herefi
CPU Usage Logger
#!/bin/bash
output_file="cpu_usage_log.txt"# Log current CPU usage to a file with timestamp
timestamp=$(date'+%Y-%m-%d %H:%M:%S')
cpu_usage=$(top -bn1 | grep 'Cpu(s)' | awk '{print $2}' | cut -d. -f1)
echo"$timestamp - CPU Usage: $cpu_usage%" >> "$output_file"echo"CPU usage logged."
System Health Check
#!/bin/bash
output_file="system_health_check.txt"# Perform system health check and save results to a file
{
echo"System Health Check - $(date)"echo"================================"echo""echo"Uptime:"uptimeecho""echo"Load Average:"cat /proc/loadavg
echo""echo"Memory Usage:"
free -h
echo""echo"Disk Usage:"df -h
echo""echo"Top Processes:"
ps aux --sort=-%cpu | head -10
} > "$output_file"echo"System health check saved to $output_file"
Phase 3: User Management Scripts
User Account Creation
#!/bin/bash
username="newuser"# Check if user exists; if not, create new userifid"$username" &>/dev/null; thenecho"User $username already exists."else
useradd -m -s /bin/bash "$username"echo"User $username created."# Set password interactively
passwd "$username"fi
Password Expiry Checker
#!/bin/bash
output_file="password_expiry_report.txt"# Check password expiry for users with bash shellecho"Password Expiry Report - $(date)" > "$output_file"echo"=================================" >> "$output_file"
IFS=$'\n'for user in $(grep "/bin/bash" /etc/passwd | cut -d: -f1); do
password_expires=$(chage -l "$user" 2>/dev/null | grep "Password expires" | awk -F: '{print $2}')
echo"User: $user - Password Expires: $password_expires" >> "$output_file"doneunset IFS
echo"Password expiry report saved to $output_file"
Phase 4: Security Scripts
Password Generator
#!/bin/bash
length=${1:-16}# Generate a random password
password=$(openssl rand -base64 48 | tr -dc 'a-zA-Z0-9!@#$%^&*' | head -c"$length")
echo"Generated password: $password"
#!/bin/bash
directory="${1:-/tmp}"
days="${2:-7}"echo"Cleaning files older than $days days in $directory"# Remove files older than specified days
find "$directory" -type f -mtime +"$days" -execrm -v {} \;
echo"Cleanup completed."
Folder Size Checker
#!/bin/bash
folder_path="${1:-.}"echo"Folder Size Analysis: $folder_path"echo"===================================="# Display sizes of subdirectories sorted by sizedu -sh "$folder_path"/* 2>/dev/null | sort -rh | head -20
echo""echo"Total size:"du -sh "$folder_path"
Phase 9: System Information
System Info Collector
#!/bin/bash
output_file="system_info_$(hostname)_$(date +%Y%m%d).txt"
{
echo"System Information Report"echo"Generated: $(date)"echo"========================="echo""echo"Hostname: $(hostname)"echo"OS: $(uname -a)"echo""echo"CPU Info:"
lscpu | grep -E "Model name|CPU\(s\)|Thread"echo""echo"Memory:"
free -h
echo""echo"Disk Space:"df -h
echo""echo"Network Interfaces:"
ip -br addr
echo""echo"Logged In Users:"who
} > "$output_file"echo"System info saved to $output_file"
#!/bin/bash
remote_server="${1:-user@remote-server}"
remote_script="${2:-/path/to/remote/script.sh}"# Execute a script on a remote server via SSH
ssh "$remote_server""bash -s" < "$remote_script"echo"Remote script executed on $remote_server"