script add

This commit is contained in:
dodger 2023-08-15 09:39:32 +02:00
parent 456100d544
commit 6f0bf2b66a
Signed by: dodger
GPG Key ID: F6701F6CB4D1C826
2 changed files with 391 additions and 0 deletions

View File

@ -0,0 +1,14 @@
SIGNURL="https://thecompany.woffu.com/api/signs"
APIURL="https://app.woffu.com"
CURLBIN="/usr/bin/curl -s"
RANDOMIZE=500
USERNAME="jholgado@thecompany.net"
PASSWORD="thepassword"
CYPHEREDUSERNAME="the CYPHEREDUSERNAME"
SMTPHOST="mta.thecompany.com"
SMTPUSER="smtp user"
SMTPPASS="smpt host"
MAILXBIN="/usr/bin/s-nail"

377
platform9and3quarters.sh Executable file
View File

@ -0,0 +1,377 @@
#!/bin/bash -x
# ----------------------------------------------------------------------------
# title : common_functions.sh
# description : functions commonly used in our scripts
# author : Alberto Rodríguez
# author : Jorge Holgado
# date : 20190404
# version : 0.1
# bash_version : 4
# ----------------------------------------------------------------------------
#
# Necessary packages: s-nail postfix (debian&ubuntu)
# Setup your credentials con ${CONFIGFILE}
# ${CYPHEREDUSERNAME} must be spoofed from a running session on the browser, is just for testing purpouses
#
BOLD="\e[1m"
GREEN="\e[32m"
LIGHTGREEN="${BOLD}${GREEN}"
#RED="\033[1;31m"
LIGHTRED="\033[1;31m"
BLUE="\e[34m"
LIGHTBLUE="${BOLD}${BLUE}"
#YELLOW="\e[33m"
#LIGHTYELLOW="${BOLD}${YELLOW}"
#WHITE="\033[0;37m"
RESET="\033[0;00m"
##############################################
#
# CONFIG
#
##############################################
QUIETOUTPUT=false
NOW="$(date +%Y%m%d%H%M%S)"
OUTPUTFILE="$(dirname "$(readlink -f "$0")")/logs/${NOW}_outputfile_$(basename "$0" .sh).log"
CONFIGFILE="$(dirname "$(readlink -f "$0")")/$(basename "$(readlink -f "$0")" .sh).config"
SCRIPTLOG="$(dirname "$(readlink -f "$0")")/logs/${NOW}_scriptlog_$(basename "$0" .sh).log"
SCRIPTLOGERR="$(dirname "$(readlink -f "$0")")/logs/${NOW}_scriptlog_$(basename "$0" .sh).err"
AUXFILE=$(mktemp)
TMPFILE=$(mktemp)
#MANDATORYHEADERS="-H \"Accept: application/json, text/plain, */*\""
#MANDATORYHEADERS="${MANDATORYHEADERS} -H \"Content-Type: application/json;charset=UTF-8\""
## TEDDYBEAR added after sourcing config file
declare -a MANDATORYHEADERS
MANDATORYHEADERS=( 'Accept: application/json, text/plain, */*' 'Content-Type: application/json;charset=UTF-8' )
declare -a SIGNINHEADERS
SIGNINHEADERS=( 'Origin: https://voxelgroup.woffu.com' 'Host: voxelgroup.woffu.com:443' 'Expect:' )
##############################################
#
#/CONFIG
#
##############################################
##############################################
#
# FUNCTIONS
#
##############################################
print_codename()
{
echo -e "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWNNXXXXXNWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMN0xl:'.. ..,cokKWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMXxc' .. .; ,: .; .;oOWMMMMMMMMMMMMMMMMMMMMMM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MMMMX0xol:;;,,;;,;:lox0XMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
"
}
usage()
{
print_codename
echo
echo -e "$0 ${LIGHTBLUE}signin|signout|status${RESET}"
echo -e "\tsignin|signout|status is MANDATORY"
}
printmsg()
{
echo -e "$*"
}
output_log()
{
if [[ "${QUIETOUTPUT}" == true ]]; then
printmsg "$*" >> "${OUTPUTFILE}"
else
printmsg "$*" | tee -a "${OUTPUTFILE}"
fi
}
abort_message()
{
printmsg "ERROR: $*"
exit 1
}
# debug_me uses variable ${DEBUGME}
debug_me()
{
if [[ "${DEBUGME}" && ${DEBUGME} -eq 0 ]] ; then
echo -e "${LIGHTBLUE}DEBUG: ${RESET}$*"
fi
}
# build_headers has a optional parameter to build "sign_in" headers
# build_headers [extended]
build_headers()
{
local EXTENDED=$1
if [[ "${EXTENDED,,}" = "extended" ]] ; then
for ((x=0; x<${#SIGNINHEADERS[@]}; x++)) ; do
#printf "%s" "-H "${SIGNINHEADERS[${x}]}" "
echo -e "-H '${SIGNINHEADERS[${x}]}' \\"
done
fi
for ((x=0; x<${#MANDATORYHEADERS[@]}; x++)) ; do
#printf "%s" "-H "${MANDATORYHEADERS[${x}]}" "
echo -e "-H '${MANDATORYHEADERS[${x}]}' \\"
done
}
do_login()
{
local POSTDATA="grant_type=password&username=${USERNAME}&password=${PASSWORD}"
local AUXURL="${APIURL}/Token"
echo -e "${CURLBIN} $(build_headers)
-d '${POSTDATA}' \\
${AUXURL}" >"${AUXFILE}"
#cat ${AUXFILE}
TEDDYBEAR="$(bash "${AUXFILE}" | jq . | grep -E "access_token" | awk '{print $2}' | awk -F\" '{print $2}')"
if [[ "${TEDDYBEAR}" =~ .*${CYPHEREDUSERNAME}.* ]] ; then
TEDDYBEAR="Authorization: Bearer ${TEDDYBEAR}"
return 0
else
output_log "Not able to login, exitting"
mail_report
abort_message "Not able to login, exitting"
fi
}
get_userid()
{
local AUXURL="${APIURL}/api/users"
echo -e "${CURLBIN} $(build_headers)
${AUXURL}" >"${AUXFILE}"
USERID="$(bash "${AUXFILE}" | jq . | grep -E "\"UserId\"" | awk '{print $2}' | sed 's/,$//g')"
if [[ "${USERID}" =~ ^[0-9]{1,}$ ]] ; then
return 0
else
output_log "Not able obtain user id"
mail_report
abort_message "Not able obtain user id"
fi
}
# will return 0 if signed, 1 if unsigned
get_status()
{
local STATUS=""
echo -e "${CURLBIN} $(build_headers)
${SIGNURL}" >"${AUXFILE}"
STATUS=$(bash "${AUXFILE}" | jq . | grep -E "SignIn" | tail -1 | awk '{print $2}')
# if there's no signIn this day, the app return an empty JSON
# As a patch, try to signin forced...
if [[ "${STATUS}" =~ ^true,$ ]] ; then
return 0
elif [[ "${STATUS}" =~ ^false,$ || "${STATUS}" = "" ]] ; then
return 1
else
output_log "Something wrong on get_status()"
mail_report
abort_message "Something wrong on get_status()"
fi
}
toggle_status()
{
# the content-length is the length of the string '{"UserId":58470,"TimezoneOffset":-120}'
#SIGNINHEADERS=${SIGNINHEADERS}' -H "content-length: 38"'
local POSTDATA="{\"UserId\":${USERID},\"TimezoneOffset\":-120}"
CONTENTLENGTH=$(echo ${POSTDATA}|wc -c)
((CONTENTLENGTH--))
echo -e "${CURLBIN} $(build_headers extended)
-H 'content-length: ${CONTENTLENGTH}' \\
-d '${POSTDATA}' \\
${SIGNURL}" >"${AUXFILE}"
#cat ${AUXFILE}
bash "${AUXFILE}" | jq . >"${TMPFILE}"
RES=$?
return ${RES}
}
randomize_time()
{
SLEEPTIME=$(( RANDOM % RANDOMIZE ))
printmsg "${LIGHTBLUE}Sleeping ${SLEEPTIME}${RESET}"
sleep ${SLEEPTIME}
}
mail_report()
{
${MAILXBIN} -s "$(basename "$0") report ${NOW}" \
-S ssl-verify=ignore -S smtp-auth=login \
-S smtp="smtp://${SMTPHOST}:25" \
-S from="${USERNAME}" \
-S smtp-auth-user="${SMTPUSER}" \
-S smtp-auth-password="${SMTPPASS}" \
-S ssl-verify=ignore \
-a "${SCRIPTLOG}" \
-a "${SCRIPTLOGERR}" \
"${USERNAME}" < "${OUTPUTFILE}"
}
##############################################
#
#/FUNCTIONS
#
##############################################
##############################################
#
# MAIN
#
##############################################
[[ ! -d $(dirname "${OUTPUTFILE}") ]] && mkdir -p "$(dirname "${OUTPUTFILE}")"
# DETECTING if the script is run by cron
if [[ "$(tty)" = "not a tty" ]] ; then
set -x
exec > "${SCRIPTLOG}"
exec 2> "${SCRIPTLOGERR}"
else
touch "${SCRIPTLOG}"
touch "${SCRIPTLOGERR}"
fi
if [[ -f ${CONFIGFILE} ]] ; then
. "${CONFIGFILE}"
else
output_log "Configfile ${CONFIGFILE} not found"
mail_report
abort_message "Configfile ${CONFIGFILE} not found"
fi
THEACTION="$1"
if [[ ! "${THEACTION,,}" =~ ^signin|signout|status$ ]] ; then
usage
exit 0
fi
MAYTHEFORCEBEWITHYOU="$2"
if [[ "${MAYTHEFORCEBEWITHYOU}" = "Imonewiththeforce" ]] ; then
RANDOMIZE=1
fi
do_login
MANDATORYHEADERS[${#MANDATORYHEADERS[@]}]="${TEDDYBEAR}"
get_userid
get_status
RES=$?
ACTUALSTATUS=${RES}
if [[ "${THEACTION,,}" = "status" ]] ; then
if [[ ${ACTUALSTATUS} -eq 0 ]] ; then
printmsg "${LIGHTGREEN}Signed In${RESET}"
else
printmsg "${LIGHTRED}Signed Out${RESET}"
fi
exit 0
fi
# ACTUALSTATUS=0 Signed In
if [[ "${THEACTION,,}" = "signin" && ${ACTUALSTATUS} -eq 0 ]] || [[ "${THEACTION,,}" = "signout" && ${ACTUALSTATUS} -eq 1 ]] ; then
output_log "Already ${THEACTION}"
mail_report
abort_message "Already ${THEACTION}"
else
randomize_time
toggle_status
RES=$?
fi
if [[ ${RES} -eq 0 ]] ; then
printmsg "${LIGHTGREEN}Successfully changed status${RESET}"
output_log "Successfully changed status"
mail_report
else
output_log "FAILED to changed status"
mail_report
abort_message "FAILED to changed status"
fi
rm -fv "${OUTPUTFILE}"
##############################################
#
#/MAIN
#
##############################################