Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created August 15, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhyland87/27204943538a0bc864191863a3261b2b to your computer and use it in GitHub Desktop.
Save jhyland87/27204943538a0bc864191863a3261b2b to your computer and use it in GitHub Desktop.
Parses the grep output and generates CSV file content (sent to STDOUT)
#!/usr/local/bin/bash
declare -A CFG
#CFG['ignore.filetype']
# Grep for DNS and IP records
# grep -IERnHis --exclude-dir={.git,.gitmodules} '(192\.168\.78\.[0-9]{1,3}|10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|\.(mywebsite|abcstaging)\.com)' liberty-{cron,magento,misc,models,portal,reports,services,ship{,-{client,server}},sweet,ups,warehouse} > ip-dns-references.txt
# Grep for just IP
# grep -IERnHis --exclude-dir={.git,.gitmodules} '(192\.168\.78\.[0-9]{1,3}|10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})' liberty-{cron,magento,misc,models,portal,reports,services,ship{,-{client,server}},sweet,ups,warehouse} > ip-references.txt
#grep -IERnHis --exclude-dir={.git,.gitmodules} '(192\.168\.78\.[0-9]{1,3}|10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|[a-zA-Z0-9\.-]+\.(mywebsite|abcstaging)\.com)' liberty-{cron,magento,misc,models,portal,reports,services,ship{,-{client,server}},sweet,ups,warehouse} > ip-dns-references.txt
printf "%s,%s,%s,%s,%s,%s,%s,%s,%s\n" "Project" "File" "Path" "Line" "Record Type" "DNS Count" "IP Count" "Records" "Code"
grepresultline=0
while read grepline; do
grepresultline=$((${grepresultline}+1))
[[ -n ${refs} ]] && unset refs
[[ -n ${counts} ]] && unset counts
declare -a refs
declare -A counts=([ip]=0 [dns]=0)
ip_match=$( echo "${grepline}" | grep -oE '(192\.168\.78\.[0-9]{1,3}|10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})' )
ip_res=$?
if [[ ${ip_res} -eq 0 ]]; then
recordtype="ip"
for ip in ${ip_match}; do
refs+=( "${ip}" )
counts[ip]=$((${counts[ip]}+1))
done
fi
dns_match=$( echo "${grepline}" | grep -oE '[a-zA-Z0-9\.-]+\.(mywebsite|abcstaging)\.com' )
dns_res=$?
if [[ ${dns_res} -eq 0 ]]; then
if [[ -n ${recordtype} ]]; then
recordtype="ip/dns"
else
recordtype="ip"
fi
for dns in ${dns_match}; do
refs+=( "${dns}" )
counts[dns]=$((${counts[dns]}+1))
done
fi
function join_by { local IFS="$1"; shift; echo "$*"; }
location=$(echo "${grepline}" | cut -d: -f 1)
file=$(basename "${location}")
path=$(dirname "${location}")
project=$(echo "${grepline}" | cut -d'/' -f1)
line=$(echo "${grepline}" | cut -d: -f 2)
code=$(echo "${grepline}" | cut -d: -f 3 | sed -E -e 's/^[[:space:]]*//g')
code_csv=$(echo "${code}" | sed -E -e 's/"/""/g' -e 's/^/"/' -e 's/$/"/')
records_str=$(join_by ';' "${refs[@]}")
records_str=$(echo "${records_str}" | sed 's/;/; /g')
printf "%s,%s,%s,%s,%s,%s,%s,%s,%s\n" \
"${project}" \
"${file}" \
"${path}" \
"${line}" \
"${recordtype}" \
"${counts[dns]}" \
"${counts[ip]}" \
"${records_str}" \
"${code_csv}"
echo -e "\tGREP LINE: ${grepline}" >&2
done < ip-dns-references.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment