Skip to content

Instantly share code, notes, and snippets.

@DanielChuDC
Last active April 7, 2024 16:40
Show Gist options
  • Save DanielChuDC/175f58df22959fdb8e2649bf48cbac5d to your computer and use it in GitHub Desktop.
Save DanielChuDC/175f58df22959fdb8e2649bf48cbac5d to your computer and use it in GitHub Desktop.
This Bash script is designed to search for specific system tools ("scp", "ssh") within specified directories and generate a report containing the file paths and line numbers where these tools are found.
#!/bin/bash
target_dirs="/opt"
target_tools=(
"scp"
"ssh"
)
target_output="report.txt"
# Array to track scanned directories
declare -a scanned_dirs
function is_utf8() {
# Use `file` command to check if a file is UTF-8 encoded
file -i "$1" | grep -q "charset=utf-8"
}
function print_file_content() {
local file="$1"
local tool="$2"
echo "File: $file"
echo "Content:"
grep -n "$tool" "$file" | sed 's/^/ /' # Indent lines for better readability
echo ""
}
function search_files() {
local directory="$1"
local scanned_dirs_ref="$2[@]" # Reference to the scanned_dirs array
# Print the current directory being scanned
echo "Directory: $directory"
# Iterate over files and directories in the directory
for item in "$directory"/*; do
if [ -f "$item" ]; then
for tool in "${target_tools[@]}"; do
if grep -q "$tool" "$item"; then
echo "Match found in file: $item"
print_file_content "$item" "$tool"
break # No need to check for other tools once a match is found
fi
done
elif [ -d "$item" ]; then
scanned_dirs+=("$item") # Add the directory to the scanned_dirs array
echo "Subdirectory: $item"
# Recursively search files in subdirectories
search_files "$item" "scanned_dirs"
fi
done
}
# function save_report() {
# local report="$1"
# local output_file="$2"
# if [ -n "$output_file" ]; then
# echo -e "$report" >> "$output_file"
# echo "Report updated and saved to $output_file"
# else
# echo -e "$report"
# fi
# }
function save_report() {
local report="$1"
local output_file="$2"
if [ -n "$output_file" ]; then
echo -e "$report" >> "$output_file"
echo "Report updated and saved to $output_file"
fi
# Always print the report to the terminal
echo -e "$report"
}
echo "Checking for system tools in target directories:"
echo ""
# Main loop to process target directories
for directory in $target_dirs; do
# search_files "$directory" "scanned_dirs"
report=$(search_files "$directory" "scanned_dirs")
save_report "$report" "$target_output"
done
# # Output scanned directories
# echo "Scanned directories:"
# for scanned_dir in "${scanned_dirs[@]}"; do
# echo "$scanned_dir"
# done
@DanielChuDC
Copy link
Author

Key Features and Functionalities:

Target Directories and Tools:

Predefined target directories (target_dirs) where the script searches for system tools.
Predefined list of system tools (target_tools) that the script searches for within the target directories.
Search Functionality:

The script recursively searches through the target directories to find files containing the specified system tools.
It uses the grep command to search for occurrences of the tools in each file.
File Content Analysis:

When a tool is found in a file, the script prints the file path along with the line number(s) where the tool is found.
Output Options:

The script allows saving the generated report to a predefined output file (target_output) or printing it to the standard output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment