diff --git a/README.md b/README.md index 93ec37d..c0ef01b 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,7 @@ so they're easy to run when needed. - See [windows/README.md](windows/README.md) for Windows scripts. For general usage instructions check [docs/README.md](docs/README.md). + +For security guidelines see [docs/SECURITY.md](docs/SECURITY.md). +Troubleshooting tips are available in [docs/TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md). +To verify script syntax run `tests/run_tests.sh`. diff --git a/docs/README.md b/docs/README.md index e4f87b3..173e82f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,3 +16,5 @@ Example for Windows (run from PowerShell as Administrator): cd windows\Winget ./WingetFix.ps1 ``` + +For security information see [SECURITY.md](SECURITY.md). Troubleshooting tips are in [TROUBLESHOOTING.md](TROUBLESHOOTING.md). diff --git a/docs/SECURITY.md b/docs/SECURITY.md new file mode 100644 index 0000000..c6332f3 --- /dev/null +++ b/docs/SECURITY.md @@ -0,0 +1,6 @@ +# Security Considerations + +- Validate all user input to avoid unintended behavior. +- Run scripts with the minimal required privileges. +- Review scripts before execution, especially those requiring administrator rights. +- Log actions to help with auditing and troubleshooting. diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md new file mode 100644 index 0000000..af605a6 --- /dev/null +++ b/docs/TROUBLESHOOTING.md @@ -0,0 +1,5 @@ +# Troubleshooting + +- Ensure dependencies are installed (e.g., `bash`, `powershell`). +- Check log files mentioned in each script's README for errors. +- Run the tests in `tests/run_tests.sh` to verify syntax. diff --git a/linux/README.md b/linux/README.md index 177217b..72b76e2 100644 --- a/linux/README.md +++ b/linux/README.md @@ -11,6 +11,7 @@ This folder contains shell scripts for common Linux maintenance tasks. Each scri - `fix_dnf_lock/` - Clear DNF/Yum lock files. - `fix_permissions/` - Fix ownership and permissions in the user's home directory. - `network_restart/` - Restart a network interface. +- `system_health/` - Monitor basic CPU, memory, and disk usage. - `maintenance/` - Comprehensive maintenance script with many features. Each subdirectory contains its own `README.md` with detailed usage instructions. diff --git a/linux/fix_permissions/README.md b/linux/fix_permissions/README.md index dedd293..199757b 100644 --- a/linux/fix_permissions/README.md +++ b/linux/fix_permissions/README.md @@ -7,3 +7,5 @@ Corrects ownership and permission issues in the current user's home directory. ```bash sudo ./fix_permissions.sh ``` + +Logs are written to `/var/log/fix_permissions.log`. diff --git a/linux/fix_permissions/fix_permissions.sh b/linux/fix_permissions/fix_permissions.sh old mode 100644 new mode 100755 index 8730145..b5c4a02 --- a/linux/fix_permissions/fix_permissions.sh +++ b/linux/fix_permissions/fix_permissions.sh @@ -1,11 +1,19 @@ -#!/bin/bash -echo "Fixing ownership and permission issues in home directory..." +#!/usr/bin/env bash +set -euo pipefail -# Change ownership of all files in the user's home directory to the current user -sudo chown -R $USER:$USER /home/$USER +LOG_FILE="/var/log/fix_permissions.log" +log() { + echo "$(date '+%Y-%m-%d %H:%M:%S') : $1" | tee -a "$LOG_FILE" +} -# Fix common permission issues -sudo find /home/$USER -type d -exec chmod 755 {} \; -sudo find /home/$USER -type f -exec chmod 644 {} \; +HOME_DIR="/home/$USER" +if [[ ! -d "$HOME_DIR" ]]; then + echo "Home directory $HOME_DIR not found" >&2 + exit 1 +fi -echo "Permissions and ownership fixed." +log "Fixing permissions in $HOME_DIR" +sudo chown -R "$USER":"$USER" "$HOME_DIR" +sudo find "$HOME_DIR" -type d -exec chmod 755 {} \; +sudo find "$HOME_DIR" -type f -exec chmod 644 {} \; +log "Permissions fixed" diff --git a/linux/network_restart/README.md b/linux/network_restart/README.md index 90ad3d7..107dd45 100644 --- a/linux/network_restart/README.md +++ b/linux/network_restart/README.md @@ -5,7 +5,9 @@ Brings a specified network interface down and back up. ## Usage ```bash -sudo ./network_restart.sh eth0 +sudo ./network_restart.sh ``` -Replace `eth0` with the actual interface name. +Replace `` with the actual network interface name (e.g., `eth0`, `enp0s3`). The script validates that the interface exists before attempting the restart. + +Logs are written to `/var/log/network_restart.log`. diff --git a/linux/network_restart/network_restart.sh b/linux/network_restart/network_restart.sh old mode 100644 new mode 100755 index 6268aef..4737b08 --- a/linux/network_restart/network_restart.sh +++ b/linux/network_restart/network_restart.sh @@ -1,8 +1,27 @@ -#!/bin/bash -echo "Restarting network interface..." +#!/usr/bin/env bash +# Safe network interface restart script +set -euo pipefail -# Replace 'eth0' with the name of your network interface -sudo ifconfig eth0 down -sudo ifconfig eth0 up +LOG_FILE="/var/log/network_restart.log" +log() { + echo "$(date '+%Y-%m-%d %H:%M:%S') : $1" | tee -a "$LOG_FILE" +} -echo "Network interface restarted." +if [[ $# -ne 1 ]]; then + echo "Usage: $0 " >&2 + exit 1 +fi + +iface="$1" +if ! ip link show "$iface" &>/dev/null; then + echo "Error: interface '$iface' not found" >&2 + exit 1 +fi + +log "Restarting network interface '$iface'" +if sudo ip link set "$iface" down && sudo ip link set "$iface" up; then + log "Interface '$iface' restarted" +else + log "Failed to restart '$iface'" + exit 1 +fi diff --git a/linux/system_health/README.md b/linux/system_health/README.md new file mode 100644 index 0000000..6df748b --- /dev/null +++ b/linux/system_health/README.md @@ -0,0 +1,11 @@ +# System Health Monitor + +This script collects basic CPU, memory, and disk usage statistics. + +## Usage + +```bash +sudo ./system_health.sh +``` + +Logs are stored in `/var/log/system_health.log`. diff --git a/linux/system_health/system_health.sh b/linux/system_health/system_health.sh new file mode 100755 index 0000000..44d58b3 --- /dev/null +++ b/linux/system_health/system_health.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Basic system health monitoring script +set -euo pipefail + +LOG_FILE="/var/log/system_health.log" + +log_message() { + local msg="$1" + echo "$(date '+%Y-%m-%d %H:%M:%S') : $msg" | tee -a "$LOG_FILE" +} + +cpu_usage() { + top -bn1 | awk '/^%?Cpu/{print $2+$4}' +} + +mem_usage() { + free -m | awk '/Mem:/ {printf("%.2f", $3/$2*100)}' +} + +disk_usage() { + df -h / | awk 'NR==2 {print $5}' +} + +log_message "CPU usage: $(cpu_usage)%" +log_message "Memory usage: $(mem_usage)%" +log_message "Disk usage: $(disk_usage)" diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..fc77253 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,3 @@ +# Testing Scripts + +Run `./run_tests.sh` from the `tests` directory to validate shell and PowerShell script syntax. diff --git a/tests/run_tests.sh b/tests/run_tests.sh new file mode 100755 index 0000000..2d0ae1c --- /dev/null +++ b/tests/run_tests.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +set -euo pipefail + +DIR="$(cd "$(dirname "$0")"/.. && pwd)" + +failed=0 + +for script in $(find "$DIR/linux" -name '*.sh'); do + echo "Checking $script" + if ! bash -n "$script"; then + echo "Shell syntax error in $script" + failed=1 + fi + if command -v shellcheck >/dev/null 2>&1; then + shellcheck "$script" || failed=1 + fi +done + +if command -v pwsh >/dev/null 2>&1; then + for ps_script in $(find "$DIR/windows" -name '*.ps1'); do + echo "Checking $ps_script" + if ! pwsh -NoProfile -Command "[System.Management.Automation.PSParser]::Tokenize((Get-Content -Raw '$ps_script'), [ref]0) > \$null"; then + echo "PowerShell syntax error in $ps_script" + failed=1 + fi + done +else + echo "pwsh not available; skipping PowerShell checks" +fi + +if [[ $failed -eq 0 ]]; then + echo "All tests passed." +else + echo "Some tests failed." >&2 +fi +exit $failed diff --git a/windows/Clean-Disk/Clean-Disk.ps1 b/windows/Clean-Disk/Clean-Disk.ps1 index eb7fb98..da8dc78 100644 --- a/windows/Clean-Disk/Clean-Disk.ps1 +++ b/windows/Clean-Disk/Clean-Disk.ps1 @@ -1,9 +1,34 @@ +param( + [string]$LogPath = "$env:TEMP\Clean-Disk.log", + [switch]$Confirm +) + +Start-Transcript -Path $LogPath -Append | Out-Null function Clean-Disk { Write-Host "Cleaning up temporary files and system junk..." -ForegroundColor Cyan - Remove-Item -Path "$env:SystemRoot\Temp\*" -Force -Recurse - Remove-Item -Path "$env:SystemRoot\SoftwareDistribution\Download\*" -Force -Recurse - Remove-Item -Path "$env:USERPROFILE\AppData\Local\Temp\*" -Force -Recurse + + $paths = @( + "$env:SystemRoot\Temp", + "$env:SystemRoot\SoftwareDistribution\Download", + "$env:USERPROFILE\AppData\Local\Temp" + ) + + foreach ($p in $paths) { + if (Test-Path $p) { + try { + Remove-Item -Path "$p\*" -Force -Recurse -ErrorAction Stop + } catch { + Write-Host "Failed to clean $p : $_" -ForegroundColor Red + } + } + } + Write-Host "Disk cleanup completed." -ForegroundColor Green } -Clean-Disk +if ($Confirm) { + Clean-Disk +} else { + Write-Host "Run with -Confirm to perform disk cleanup." -ForegroundColor Yellow +} +Stop-Transcript | Out-Null diff --git a/windows/Clean-Disk/README.md b/windows/Clean-Disk/README.md index 2bf6fd8..ffd1095 100644 --- a/windows/Clean-Disk/README.md +++ b/windows/Clean-Disk/README.md @@ -39,22 +39,37 @@ The `Clean-Disk` PowerShell script is designed to clean up temporary files and s ## Code Explanation ```powershell +param( + [switch]$Confirm +) + function Clean-Disk { Write-Host "Cleaning up temporary files and system junk..." -ForegroundColor Cyan - # Remove system temporary files - Remove-Item -Path "$env:SystemRoot\Temp\*" -Force -Recurse - - # Remove Windows Update cache files - Remove-Item -Path "$env:SystemRoot\SoftwareDistribution\Download\*" -Force -Recurse - - # Remove user-specific temporary files - Remove-Item -Path "$env:USERPROFILE\AppData\Local\Temp\*" -Force -Recurse + $paths = @( + "$env:SystemRoot\Temp", + "$env:SystemRoot\SoftwareDistribution\Download", + "$env:USERPROFILE\AppData\Local\Temp" + ) + + foreach ($p in $paths) { + if (Test-Path $p) { + try { + Remove-Item -Path "$p\*" -Force -Recurse -ErrorAction Stop + } catch { + Write-Host "Failed to clean $p : $_" -ForegroundColor Red + } + } + } Write-Host "Disk cleanup completed." -ForegroundColor Green } -Clean-Disk +if ($Confirm) { + Clean-Disk +} else { + Write-Host "Run with -Confirm to perform disk cleanup." -ForegroundColor Yellow +} ``` - **Key Directories Cleaned**: @@ -71,4 +86,5 @@ Clean-Disk 1. **Irreversible Deletion**: The script permanently deletes files in the specified directories. Ensure there are no important files in these locations before running the script. 2. **Targeted Cleanup**: The script is limited to standard temporary and cache directories, minimizing the risk of affecting critical system files. -3. **Recommended Usage**: Use this script periodically to maintain system cleanliness and performance. \ No newline at end of file +3. **Recommended Usage**: Use this script periodically to maintain system cleanliness and performance. +Logs are stored in `$env:TEMP\Clean-Disk.log`. diff --git a/windows/README.md b/windows/README.md index a40b8a2..c7287cb 100644 --- a/windows/README.md +++ b/windows/README.md @@ -71,6 +71,18 @@ This repository contains a collection of PowerShell scripts designed to simplify --- +### 11. **System Health Monitor** +- **Description**: Logs basic CPU, memory, and disk usage. +- **Key Features**: + - Uses built-in PowerShell commands to gather statistics. + - Saves output to a configurable log file. + +### 12. **Registry Cleanup** +- **Description**: Removes invalid startup entries from the registry after creating a backup. +- **Key Features**: + - Backs up the HKCU hive before deleting entries. + - Requires the `-Confirm` flag to run. + ## How to Use 1. Navigate to the subfolder containing the desired script. diff --git a/windows/Registry-Cleanup/README.md b/windows/Registry-Cleanup/README.md new file mode 100644 index 0000000..011c379 --- /dev/null +++ b/windows/Registry-Cleanup/README.md @@ -0,0 +1,12 @@ +# Registry-Cleanup.ps1 + +Cleans invalid startup entries from the Windows registry. + +## Usage + +```powershell +./Registry-Cleanup.ps1 -Confirm +``` + +A backup of the HKCU hive is saved to `$env:TEMP\registry_backup.reg` before any changes are made. +Logs are written to `$env:TEMP\registry_cleanup.log`. diff --git a/windows/Registry-Cleanup/Registry-Cleanup.ps1 b/windows/Registry-Cleanup/Registry-Cleanup.ps1 new file mode 100644 index 0000000..5ad564b --- /dev/null +++ b/windows/Registry-Cleanup/Registry-Cleanup.ps1 @@ -0,0 +1,29 @@ +param( + [string]$LogPath = "$env:TEMP\registry_cleanup.log", + [string]$BackupPath = "$env:TEMP\registry_backup.reg", + [switch]$Confirm +) + +Start-Transcript -Path $LogPath -Append | Out-Null + +if ($Confirm) { + reg export HKCU "$BackupPath" /y | Out-Null + $runPaths = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run", + "HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce" + + foreach ($path in $runPaths) { + Get-ItemProperty $path | ForEach-Object { + $value = $_.PSChildName + $cmd = $_.$value + if ($cmd -and !(Test-Path ($cmd -split ' ')[0])) { + Write-Host "Removing invalid entry $value from $path" -ForegroundColor Yellow + Remove-ItemProperty -Path $path -Name $value + } + } + } + Write-Host "Registry cleanup completed. Backup stored at $BackupPath" -ForegroundColor Green +} else { + Write-Host "Run with -Confirm to perform registry cleanup." -ForegroundColor Yellow +} + +Stop-Transcript | Out-Null diff --git a/windows/SystemHealth/README.md b/windows/SystemHealth/README.md new file mode 100644 index 0000000..178f6e7 --- /dev/null +++ b/windows/SystemHealth/README.md @@ -0,0 +1,11 @@ +# SystemHealth.ps1 + +Checks basic system statistics and logs the output. + +## Usage + +```powershell +./SystemHealth.ps1 +``` + +By default logs are stored in `$env:TEMP\system_health.log`. diff --git a/windows/SystemHealth/SystemHealth.ps1 b/windows/SystemHealth/SystemHealth.ps1 new file mode 100644 index 0000000..b106206 --- /dev/null +++ b/windows/SystemHealth/SystemHealth.ps1 @@ -0,0 +1,11 @@ +param( + [string]$LogPath = "$env:TEMP\system_health.log" +) + +Start-Transcript -Path $LogPath -Append | Out-Null + +Write-Host "CPU Usage:" (Get-CimInstance -ClassName Win32_Processor | measure -Property LoadPercentage -Average | select -ExpandProperty Average) "%" +Write-Host "Memory Usage:" (Get-CimInstance -ClassName Win32_OperatingSystem | select -ExpandProperty FreePhysicalMemory) "KB free" +Write-Host "Disk Usage:" (Get-PSDrive -Name C | select -ExpandProperty Used) "bytes used" + +Stop-Transcript | Out-Null