HEX
Server: LiteSpeed
System: Linux CentOS-79-64-minimal 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: vishn3436 (5293)
PHP: 8.0.15
Disabled: NONE
Upload Files
File: //data/fileMonitor/checkFileChange.sh
#!/bin/bash

# Configuration
MONITOR_LIST="monitoringfiles.list"  # File containing the list of files to monitor
RECORD_DIR="./records"               # Directory to store hash and modification times for each file
NOTIFICATION_FILE="./notification_message.tmp" # Temporary file to build the notification message

# Check if monitoring list exists
if [[ ! -f "$MONITOR_LIST" ]]; then
    echo "Error: $MONITOR_LIST does not exist! Please create it and list files to monitor."
    exit 1
fi

# Create record directory if it doesn't exist
mkdir -p "$RECORD_DIR"
> "$NOTIFICATION_FILE" # Clear previous notification message

# Function to monitor a single file
monitor_file() {
    FILE="$1"
    RECORD_FILE="$RECORD_DIR/$(basename "$FILE").log"

    # Check if file exists
    if [[ ! -f "$FILE" ]]; then
        echo "Error: $FILE does not exist! Skipping..."
        return
    fi

    # Get current hash and modification time
    CURRENT_HASH=$(sha256sum "$FILE" | awk '{print $1}')
    CURRENT_MOD_TIME=$(stat -c %y "$FILE")

    # Initialize record file if it doesn't exist
    if [[ ! -f "$RECORD_FILE" ]]; then
        echo "$CURRENT_HASH|$CURRENT_MOD_TIME" > "$RECORD_FILE"
        echo "Monitoring started for $FILE. Initial values recorded."
        return
    fi

    # Read last record
    IFS="|" read LAST_HASH LAST_MOD_TIME < "$RECORD_FILE"

    # Check for changes
    if [[ "$CURRENT_HASH" != "$LAST_HASH" ]]; then
        # Add to consolidated notification message
        echo "šŸ“‚ *File*: $FILE\nšŸ”‘ *Previous Modification*: \`$LAST_MOD_TIME\`\nšŸ†• *Current Modification*: \`$CURRENT_MOD_TIME\`\n" >> "$NOTIFICATION_FILE"

        # Update the record file
        echo "$CURRENT_HASH|$CURRENT_MOD_TIME" > "$RECORD_FILE"
    fi
}

# Main script
while IFS= read -r FILE_PATH; do
    monitor_file "$FILE_PATH"
done < "$MONITOR_LIST"

# Send consolidated notification if there are changes
if [[ -s "$NOTIFICATION_FILE" ]]; then
    MESSAGE="🚨 *Security Alert: File Changes Detected* 🚨\n\n$(cat "$NOTIFICATION_FILE")\nāš ļø *Immediate Attention Required!*\n\nPlease verify the changes to ensure they're authorized."

    # Inline single-line `curl` request
    curl --location --request POST 'https://marketing.wp.karmanye.in/api/Karmanye/send-message' \
        --header 'Content-Type: application/json; charset=utf-8' \
        --header 'Accept: application/json' \
        --header 'Authorization: Bearer $2b$10$f7yAQHm3Y1eOxs92aiEJ9eohJ0AdUQ2YwXxUARzk3sVLgKIVw0k2q' \
        -d "{\"phone\": \"919033595842\", \"message\": \"$(echo -e "$MESSAGE" | sed ':a;N;$!ba;s/\n/\\n/g')\"}"

    # Print notification summary
    echo -e "$MESSAGE"
else
    echo "No files have changed."
fi