#!/bin/bash # Detect the Tomcat config directory automatically path="/usr/share" if [[ -e "$path/tomcat9/conf" ]] then path="$path/tomcat9/conf" elif [[ -e "$path/tomcat8/conf" ]] then path="$path/tomcat8/conf" elif [[ -e "$path/tomcat7/conf" ]] then path="$path/tomcat7/conf" else echo "No tomcat dirs exist / Something went wrong" exit 1 fi # Database connection details (read from Tomcat's server.xml) db_host="$(cat $path/server.xml | grep url= | sed -e 's/url\=\"jdbc\:mysql\:\/\///g' | sed -e 's/\/terminalfour.*//g' | tr -d '[:space:]')" db_user="$(cat $path/server.xml | grep username= | sed -e 's/username\=\"//g' | sed -e 's/\"//g' | tr -d '[:space:]')" db_password="$(cat $path/server.xml | grep password= | sed -e 's/password\=\"//g' | sed -e 's/\"//g' | tr -d '[:space:]')" db_name="terminalfour" # Function to check if a value exists in any table check_tables_for_value() { local value="$1" # Escape the search term so it is matched literally and can't break the query: # 1. backslash -> \\ (so LIKE treats it as a literal backslash) # 2. % and _ -> \% and \_ (so they aren't treated as LIKE wildcards) # 3. backslash -> \\ again (for the SQL string literal itself) # 4. ' -> \' (so a quote doesn't end the string) local escaped escaped=$(printf '%s' "$value" | sed -e 's/\\/\\\\/g' -e 's/%/\\%/g' -e 's/_/\\_/g' -e 's/\\/\\\\/g' -e "s/'/\\\\'/g") # Get list of tables in the database tables=$(mysql -h "$db_host" -u "$db_user" -p"$db_password" --ssl "$db_name" -e "SHOW TABLES;" | tail -n +2) # Loop through each table for table in $tables; do # Get list of columns in the table columns=$(mysql -h "$db_host" -u "$db_user" -p"$db_password" --ssl "$db_name" -e "SHOW COLUMNS FROM $table;" | awk '{print $1}' | tail -n +2) # Loop through each column for column in $columns; do # Check if the value exists in the column result=$(mysql -h "$db_host" -u "$db_user" -p"$db_password" --ssl "$db_name" -e "SELECT * FROM $table WHERE $column LIKE '%$escaped%';" | tail -n +2) if [ -n "$result" ]; then echo "Found in table $table, column $column:" echo "$result" echo "$table" >> result.txt fi done done } # Ask the user for the search term read -p "Please enter the search term: " -r search_term if [ -z "$search_term" ]; then echo "No search term entered. Exiting." exit 1 fi # Call function to check for the value check_tables_for_value "$search_term"