cp-tls-inspection-applicati.../cron-example-with-diff.sh

103 lines
2.7 KiB
Bash
Raw Permalink Normal View History

2021-03-10 19:15:52 +02:00
#!/usr/bin/env bash
2021-03-11 00:20:24 +02:00
DEBUG="0"
2021-03-10 19:15:52 +02:00
DRY_RUN="0"
2021-03-11 00:20:24 +02:00
CLEANUP_AFTER="1"
REGEX_FLAG_ENABLED="0"
2021-03-10 19:15:52 +02:00
APP_NAME="$1"
2021-03-11 21:28:18 +02:00
URL="https://gist.githubusercontent.com/elico/249034a199d17ce52524f47fad49964f/raw/94d4c90614a9eddfb5d0b8c01f3d636ddb3909da/010-GeneralTLSInspectionBypass"
2021-03-10 19:15:52 +02:00
if [ -z "${APP_NAME}" ];then
echo "Missing App Name"
exit 1
fi
if [ "$2" == "check" ];then
DRY_RUN="1"
echo "Running in dry run mode" >&2
fi
if [ ! -z "$3" ];then
URL="$3"
fi
TMP_DOWNLOAD_FILE=$(mktemp)
wget "${URL}" -O ${TMP_DOWNLOAD_FILE}
RES=$?
if [ "${RES}" -gt "0" ];then
echo "Error Downloading file from URL: \"${URL}\""
rm -v "${TMP_DOWNLOAD_FILE}"
exit ${RES}
fi
TMP_CLISH_UPDATE_FILE=$( mktemp )
TMP_CURRENT_CONFIG_FILE=$( mktemp )
TMP_CLISH_TRANSACTION_FILE=$( mktemp )
clish -c "show configuration"|egrep "^set application application-name \"${APP_NAME}\"" > ${TMP_CURRENT_CONFIG_FILE}
2021-03-11 00:20:24 +02:00
CURRENT_APP_CONTENT=$( cat ${TMP_CURRENT_CONFIG_FILE}| awk '{print $7}' )
while IFS= read -r line
do
2021-03-10 19:15:52 +02:00
if [ "${DEBUG}" -gt "0" ];then
echo -n "DEBUG LEVEL 1: Working on regex: " >&2
2021-03-11 00:20:24 +02:00
echo "${line}" >&2
fi
echo "${CURRENT_APP_CONTENT}"| grep -x -F "${line}" >/dev/null
RES=$?
if [ "${RES}" -gt "0" ];then
if [ "${REGEX_FLAG_ENABLED}" -eq "1" ];then
echo "set application application-name \"${APP_NAME}\" regex-url true add url \"${line}\"" >> ${TMP_CLISH_UPDATE_FILE}
else
echo "set application application-name \"${APP_NAME}\" add url \"${line}\"" >> ${TMP_CLISH_UPDATE_FILE}
fi
2021-03-10 19:15:52 +02:00
fi
2021-03-11 00:20:24 +02:00
2021-03-10 19:15:52 +02:00
done < ${TMP_DOWNLOAD_FILE}
DIFF=$(diff "${TMP_CURRENT_CONFIG_FILE}" "${TMP_CLISH_UPDATE_FILE}" )
2021-03-11 00:20:24 +02:00
if [ "${DEBUG}" -gt "0" ];then
echo "DIFF Size: $(echo "${DIFF}"|wc -l)"
echo "${DIFF}"
fi
2021-03-10 19:15:52 +02:00
DELETE_OBJECTS=$(echo "${DIFF}" |egrep "^-set " |awk '{print $7}')
for object in ${DELETE_OBJECTS}; do
echo "set application application-name \"${APP_NAME}\" remove url ${object}" >> ${TMP_CLISH_TRANSACTION_FILE}
done
echo "${DIFF}" |egrep "^\+set " |sed -e "s@^\+set @set @g" >> ${TMP_CLISH_TRANSACTION_FILE}
cat "${TMP_CLISH_TRANSACTION_FILE}"
if [ "${DRY_RUN}" -eq "0" ];then
clish -f "${TMP_CLISH_TRANSACTION_FILE}"
fi
echo "Finished Transaction"
echo "Cleaning up files ..."
2021-03-11 00:20:24 +02:00
if [ "${CLEANUP_AFTER}" -eq "1" ];then
rm -v "${TMP_DOWNLOAD_FILE}"
rm -v "${TMP_CLISH_UPDATE_FILE}"
rm -v "${TMP_CURRENT_CONFIG_FILE}"
rm -v "${TMP_CLISH_TRANSACTION_FILE}"
else
echo "Don't forget to cleanup the files:"
echo "${TMP_DOWNLOAD_FILE}"
echo "${TMP_CLISH_UPDATE_FILE}"
echo "${TMP_CURRENT_CONFIG_FILE}"
echo "${TMP_CLISH_TRANSACTION_FILE}"
fi