1
0
mirror of https://github.com/pi-hole/pi-hole synced 2024-12-23 07:18:07 +00:00

parse contents of each file in each dir, several log_writes

This commit is contained in:
Jacob Salmela 2017-05-27 00:04:42 -05:00
parent 7ec169ab10
commit 36907edd50
No known key found for this signature in database
GPG Key ID: 1962FF1A5046135E

View File

@ -66,7 +66,7 @@ make_temporary_log() {
}
log_write() {
# echo arguments to both the log and the console"
# echo arguments to both the log and the console
echo -e "${@}" | tee -a /proc/$$/fd/3
}
@ -76,13 +76,14 @@ copy_to_debug_log() {
}
echo_succes_or_fail() {
# Set the first argument passed to this function as a named variable for better readability
local message="${1}"
# If the command was successful (a zero),
if [[ $? -eq 0 ]]; then
# Set the first argument passed to this function as a named variable for better readability
local message="${1}"
# show success
log_write "${TICK} ${message}"
else
local message="${1}"
# Otherwise, show a error
log_write "${CROSS} ${message}"
fi
@ -173,7 +174,7 @@ check_core_version() {
log_write "${INFO} Branch: ${COL_YELLOW}${PI_HOLE_BRANCH:-Detached}${COL_NC} (${COL_CYAN}${FAQ_CHECKOUT_COMMAND}${COL_NC})"
fi
# echo the current commit
log_write " ${INFO} Commit: ${PI_HOLE_COMMIT}"
log_write "${INFO} Commit: ${PI_HOLE_COMMIT}\n"
# If git status failed,
else
# Return an error message
@ -222,7 +223,7 @@ check_web_version() {
log_write "${INFO} Branch: ${COL_YELLOW}${WEB_BRANCH:-Detached}${COL_NC} (${COL_CYAN}${FAQ_CHECKOUT_COMMAND}${COL_NC})"
fi
# echo the current commit
log_write " ${INFO} Commit: ${WEB_COMMIT}"
log_write "${INFO} Commit: ${WEB_COMMIT}\n"
# If git status failed,
else
# Return an error message
@ -374,12 +375,13 @@ detect_ip_addresses() {
log_write "${TICK} IPv${protocol} on ${PIHOLE_INTERFACE}"
# Since there may be more than one IP address, store them in an array
for i in "${!ip_addr_list[@]}"; do
# For each one in the list, print it out using the iterator as a numbered list
log_write " [$i] ${ip_addr_list[$i]}"
# For each one in the list, print it out
log_write "${ip_addr_list[$i]}"
done
log_write ""
else
# If there are no IPs detected, explain that the protocol is not configured
log_write " ${CROSS} ${COL_LIGHT_RED}No IPv${protocol} found on ${PIHOLE_INTERFACE}${COL_NC}"
log_write "${CROSS} ${COL_LIGHT_RED}No IPv${protocol} found on ${PIHOLE_INTERFACE}${COL_NC}\n"
return 1
fi
}
@ -414,13 +416,13 @@ ping_gateway() {
# If pinging the gateway is not successful,
if ! ping_cmd="$(${cmd} -q -c 3 -W 3 -n ${gateway} -I ${PIHOLE_INTERFACE} | tail -n 3)"; then
# let the user know
log_write " ${CROSS} ${COL_LIGHT_RED}Gateway did not respond.${COL_NC}"
log_write "${CROSS} ${COL_LIGHT_RED}Gateway did not respond.${COL_NC}\n"
# and return an error code
return 1
# Otherwise,
else
# show a success
log_write " ${TICK} ${COL_LIGHT_GREEN}Gateway responded.${COL_NC}"
log_write "${TICK} ${COL_LIGHT_GREEN}Gateway responded.${COL_NC}\n"
# and return a success code
return 0
fi
@ -446,11 +448,11 @@ ping_internet() {
# Try to ping the address 3 times
if ! ping_inet="$(${cmd} -q -W 3 -c 3 -n ${public_address} -I ${PIHOLE_INTERFACE} | tail -n 3)"; then
# if it's unsuccessful, show an error
log_write " ${CROSS} ${COL_LIGHT_RED}Cannot reach the Internet.${COL_NC}"
log_write "${CROSS} ${COL_LIGHT_RED}Cannot reach the Internet.${COL_NC}\n"
return 1
else
# Otherwise, show success
log_write " ${TICK} ${COL_LIGHT_GREEN}Query responded.${COL_NC}"
log_write "${TICK} ${COL_LIGHT_GREEN}Query responded.${COL_NC}\n"
return 0
fi
}
@ -590,7 +592,7 @@ dig_at() {
# First, do a dig on localhost to see if Pi-hole can use itself to block a domain
if local_dig=$(dig +tries=1 +time=2 -"${protocol}" "${random_url}" @${local_address} +short "${record_type}"); then
# If it can, show sucess
log_write " ${TICK} ${COL_LIGHT_GREEN}${random_url} is ${local_dig}${COL_NC} via localhost (${local_address})"
log_write "${TICK} ${random_url} ${COL_LIGHT_GREEN}is ${local_dig}${COL_NC} via localhost (${local_address})"
else
# Otherwise, show a failure
log_write "${CROSS} ${COL_LIGHT_RED}Failed to resolve${COL_NC} ${random_url} ${COL_LIGHT_RED}via localhost${COL_NC} (${local_address})"
@ -604,7 +606,7 @@ dig_at() {
# If Pi-hole can dig itself from it's IP (not the loopback address)
if pihole_dig=$(dig +tries=1 +time=2 -"${protocol}" "${random_url}" @${pihole_address} +short "${record_type}"); then
# show a success
log_write " ${TICK} ${COL_LIGHT_GREEN}${random_url} is ${pihole_dig}${COL_NC} via Pi-hole (${pihole_address})"
log_write "${TICK} ${random_url} ${COL_LIGHT_GREEN}is ${pihole_dig}${COL_NC} via Pi-hole (${pihole_address})"
else
# Othewise, show a failure
log_write "${CROSS} ${COL_LIGHT_RED}Failed to resolve${COL_NC} ${random_url} ${COL_LIGHT_RED}via Pi-hole${COL_NC} (${pihole_address})"
@ -614,7 +616,7 @@ dig_at() {
# We are using the static remote_url here instead of a random one because we know it works with IPv4 and IPv6
if remote_dig=$(dig +tries=1 +time=2 -"${protocol}" "${remote_url}" @${remote_address} +short "${record_type}" | head -n1); then
# If successful, the real IP of the domain will be returned instead of Pi-hole's IP
log_write " ${TICK} ${COL_LIGHT_GREEN}${remote_url} is ${remote_dig}${COL_NC} via a remote, public DNS server (${remote_address})"
log_write "${TICK} ${remote_url} ${COL_LIGHT_GREEN}is ${remote_dig}${COL_NC} via a remote, public DNS server (${remote_address})"
else
# Otherwise, show an error
log_write "${CROSS} ${COL_LIGHT_RED}Failed to resolve${COL_NC} ${remote_url} ${COL_LIGHT_RED}via a remote, public DNS server${COL_NC} (${remote_address})"
@ -643,6 +645,17 @@ process_status(){
done
}
make_array_from_file() {
local filename="${1}"
if [[ -d "${filename}" ]]; then
:
else
while IFS= read -r line;do
file_content+=("${line}")
done < "${filename}"
fi
}
parse_file() {
# Set the first argument passed to this function as a named variable for better readability
local filename="${1}"
@ -655,8 +668,8 @@ parse_file() {
local file_lines
# For each line in the file,
for file_lines in "${file_info[@]}"; do
# display the information with the ${INFO} icon
log_write " ${INFO} ${file_lines}"
# Display the file's content
log_write " ${file_lines}" | grep -v "#" | sed '/^$/d'
done
# Set the IFS back to what it was
IFS="$OLD_IFS"
@ -701,10 +714,10 @@ dir_check() {
for filename in "${directory}"; do
# check if exists first; if it does,
file_exists "${filename}" && \
# show a success message
echo_succes_or_fail "Files detected" || \
# do nothing
: || \
# Otherwise, show an error
echo_succes_or_fail "${COL_LIGHT_RED}irectory does not exist.${COL_NC}"
echo_succes_or_fail "${COL_LIGHT_RED}directory does not exist.${COL_NC}"
done
}
@ -715,9 +728,19 @@ list_files_in_dir() {
files_found=( $(ls "${dir_to_parse}") )
# For each file in the arry,
for each_file in "${files_found[@]}"; do
if [[ -d "${each_file}" ]]; then
:
else
# display the information with the ${INFO} icon
# Also print the permissions and the user/group
log_write " ${INFO} $(ls -ld ${dir_to_parse}/${each_file})"
log_write "\n${COL_LIGHT_GREEN}$(ls -ld ${dir_to_parse}/${each_file})${COL_NC}"
# Otherwise, parse the file's content
make_array_from_file "${dir_to_parse}/${each_file}"
for each_line in "${file_content[@]}"; do
log_write " ${each_line}"
done
fi
file_content=()
done
}
@ -761,8 +784,7 @@ analyze_gravity_list() {
echo_current_diagnostic "Gravity list"
# It's helpful to know how big a user's gravity file is
gravity_length=$(grep -c ^ "${GRAVITYFILE}") && \
log_write " ${INFO} ${GRAVITYFILE} is ${gravity_length} lines long.";
parse_file ${GRAVITYFILE} || \
log_write "${INFO} ${GRAVITYFILE} is ${gravity_length} lines long." || \
# If the previous command failed, something is wrong with the file
log_write "${CROSS} ${COL_LIGHT_RED}${GRAVITYFILE} not found!${COL_NC}"
}
@ -791,12 +813,11 @@ upload_to_tricorder() {
# Let the user know debugging is complete
echo ""
log_write "${TICK} Finished debugging!"
log_write "${TICK} ${COL_LIGHT_GREEN}** Finished debugging! **${COL_NC}\n"
# Provide information on what they should do with their token
log_write " * The debug log can be uploaded to tricorder.pi-hole.net for sharing with developers only."
log_write " * For more information, see: ${COL_CYAN}https://pi-hole.net/2016/11/07/crack-our-medical-tricorder-win-a-raspberry-pi-3/${COL_NC}"
log_write ""
log_write " * If available, we'll use openssl to upload the log, otherwise it will fall back to netcat."
# If pihole -d is running automatically (usually throught the dashboard)
if [[ "${AUTOMATED}" ]]; then
@ -819,7 +840,7 @@ upload_to_tricorder() {
# If they say yes, run our function for uploading the log
[yY][eE][sS]|[yY]) tricorder_use_nc_or_ssl;;
# If they choose no, just exit out of the script
*) log_write " * Log will ${COL_LIGHT_GREE}NOT${COL_NC} be uploaded to tricorder.";exit;
*) log_write " * Log will ${COL_LIGHT_GREEN}NOT${COL_NC} be uploaded to tricorder.";exit;
esac
fi
# Check if tricorder.pi-hole.net is reachable and provide token
@ -829,19 +850,15 @@ upload_to_tricorder() {
log_write "${COL_LIGHT_PURPLE}***********************************${COL_NC}"
log_write "${TICK} Your debug token is: ${COL_LIGHT_GREEN}${tricorder_token}${COL_NC}"
log_write "${COL_LIGHT_PURPLE}***********************************${COL_NC}"
log_write ""
log_write " Provide this token to the Pi-hole team for assistance:"
echo ""
log_write " https://discourse.pi-hole.net"
echo ""
log_write " Your log will self-destruct after 48 hours."
log_write " * Provide this token to the Pi-hole team for assistance:"
log_write " * ${COL_CYAN}https://discourse.pi-hole.net${COL_NC}"
log_write " * Your log will self-destruct after ${COL_LIGHT_RED}48 hours${COL_NC}."
else
log_write " ${CROSS} There was an error uploading your debug log."
log_write " Please try again or contact the Pi-hole team for assistance."
log_write "${CROSS} ${COL_LIGHT_RED}There was an error uploading your debug log.${COL_NC}"
log_write " * Please try again or contact the Pi-hole team for assistance."
fi
echo ""
log_write " A local copy of the debug log can be found at : /var/log/pihole_debug.log"
echo ""
log_write " * A local copy of the debug log can be found at : ${COL_CYAN}${DEBUG_LOG}${COL_NC}\n"
}
# Run through all the functions we made