Skip to content

Commit

Permalink
fix proxy tool
Browse files Browse the repository at this point in the history
  • Loading branch information
ogrand committed Sep 22, 2023
1 parent a022aeb commit 6636d95
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 38 deletions.
13 changes: 4 additions & 9 deletions tools/bash_profile
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,11 @@ fi
#--- Set dynamic user prompt
set_proxy_prompt()
{
case "${https_proxy}" in
"http://intranet-http-proxy.internal.paas:3129") PROXY_TYPE="intranet" ;;
"http://system-internet-http-proxy.internal.paas:3128") PROXY_TYPE="internet" ;;
"") PROXY_TYPE="" ;;
esac

if [ "${PROXY_TYPE}" = "" ] ; then
echo ""
else
if [ ! -z "${PROXY_TYPE}" ] ; then
echo "${PROXY_SYMBOL}${PROXY_TYPE} proxy "
else
unset PROXY_TYPE PROXY_HOST PROXY_PORT http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY
echo ""
fi
}

Expand Down
5 changes: 4 additions & 1 deletion tools/get-vm-info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ vm_host="$(govc vm.info ${vm_name} | awk '/Host/ {print $2}')"
datastore="$(echo "${vm_info}" | jq -r '.Config.DatastoreUrl[].Name')"
power_state="$(echo "${vm_info}" | jq -r '.Summary.Runtime.PowerState')"
vm_uptime="$(echo "${vm_info}" | jq -r '.Summary.QuickStats.UptimeSeconds')"
vm_uptime="$(printf "%d days %d hours %d mn\n" $((vm_uptime/86400)) $((vm_uptime%86400/3600)) $((vm_uptime%3600/60)))"
vm_uptime_days="$(expr ${vm_uptime} / 86400)"
vm_uptime_hours="$(expr ${vm_uptime} % 86400 / 3600)"
vm_uptime_mn="$(expr ${vm_uptime} % 3600 / 60)"
vm_uptime="$(printf "%d days %d hours %d mn\n" ${vm_uptime_days} ${vm_uptime_hours} ${vm_uptime_mn})"
nb_cpus="$(echo "${vm_info}" | jq -r '.Summary.Config.NumCpu')"
memory_size="$(echo "${vm_info}" | jq -r '.Summary.Config.MemorySizeMB')"
nb_diks="$(echo "${vm_info}" | jq -r '.Summary.Config.NumVirtualDisks')"
Expand Down
90 changes: 62 additions & 28 deletions tools/proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,97 @@
#===========================================================================
# Set/unset internet/intranet proxy
# Parameters :
# --intranet, -a : Set intranet proxy
# --internet, -e : Set internet proxy
# --switch, -s : Set switch proxy
# -a, --intranet : Set intranet proxy
# -i, --internet : Set internet proxy
# -s, --switch : Set switch proxy
# -c, --corporate : Set corporate proxy
#===========================================================================

#--- Check scripts options
usage() {
printf "\n%bUSAGE:" "${RED}"
printf "\n proxy [OPTIONS]\n\nOPTIONS:"
printf "\n %-20s %s" "--intranet, -a " "Set intranet proxy"
printf "\n %-20s %s" "--internet, -e" "Set internet proxy"
printf "\n %-20s %s" "--switch, -s" "Set switch proxy"
printf "\n %-20s %s" "-a, --intranet" "Set intranet proxy"
printf "\n %-20s %s" "-i, --internet" "Set internet proxy"
printf "\n %-20s %s" "-s, --switch" "Set switch proxy"
printf "\n %-20s %s" "-c, --corporate" "Set corporate proxy"
printf "%b\n\n" "${STD}"
flagError=1
}

flagError=0
PROXY=""
proxyStatus=`env | grep -i "http_proxy"`
#--- Get a parameter in credhub
getCredhubValue() {
value=$(credhub g -n $2 | grep 'value:' | awk '{print $2}')
if [ "${value}" = "" ] ; then
printf "\n\n%bERROR : \"$2\" credhub value unknown.%b\n\n" "${RED}" "${STD}" ; flagError=1
else
eval "$1=${value}"
fi
}

#--- Log to credhub
logToCredhub() {
flag=$(credhub f > /dev/null 2>&1)
if [ $? != 0 ] ; then
printf "\n%bLDAP user and password :%b\n" "${REVERSE}${GREEN}" "${STD}"
printf "username: " ; read LDAP_USER
credhub login --server=https://credhub.internal.paas:8844 -u ${LDAP_USER}
if [ $? != 0 ] ; then
printf "\n%bERROR : LDAP authentication failed with \"${LDAP_USER}\" account.%b\n\n" "${RED}" "${STD}" ; flagError=1
fi
fi
}

flagError=0 ; PROXY=""
NO_PROXY_INTERNAL="127.0.0.1,localhost,169.254.0.0/16,172.17.11.0/24,192.168.0.0/16,.internal.paas"

case "$1" in
"") PROXY_TYPE="" ;;

"-a"|"--intranet")
PROXY_TYPE="intranet"
PROXY="http://intranet-http-proxy.internal.paas:3129"
NO_PROXY="127.0.0.1,localhost,169.254.0.0/16,172.17.11.0/24,192.168.0.0/16,.internal.paas" ;;
PROXY_HOST="intranet-http-proxy.internal.paas"
PROXY_PORT="3129" ;;

"-e"|"--internet")
"-i"|"--internet"|"")
PROXY_TYPE="internet"
PROXY="http://system-internet-http-proxy.internal.paas:3128"
NO_PROXY="127.0.0.1,localhost,169.254.0.0/16,172.17.11.0/24,192.168.0.0/16,.internal.paas,${INTRANET_DOMAINS}" ;;
PROXY_HOST="system-internet-http-proxy.internal.paas"
PROXY_PORT="3128" ;;

"-s"|"--switch")
PROXY_TYPE="switch"
PROXY="http://switch-http-proxy.internal.paas:3127"
NO_PROXY="127.0.0.1,localhost,169.254.0.0/16,172.17.11.0/24,192.168.0.0/16,.internal.paas" ;;
PROXY_HOST="switch-http-proxy.internal.paas"
PROXY_PORT="3127" ;;

"") if [ "${proxyStatus}" = "" ] ; then
PROXY_TYPE="internet"
PROXY="http://system-internet-http-proxy.internal.paas:3128"
NO_PROXY="127.0.0.1,localhost,169.254.0.0/16,192.168.0.0/16,172.17.11.0/24,.internal.paas,${INTRANET_DOMAINS}"
fi ;;
"-c"|"--corporate")
PROXY_TYPE="corporate"
logToCredhub
if [ ${flagError} = 0 ] ; then
getCredhubValue "PROXY_HOST" "/secrets/multi_region_region_1_corporate_internet_proxy_host"
getCredhubValue "PROXY_PORT" "/secrets/multi_region_region_1_corporate_internet_proxy_port"
fi ;;

*) usage ;;
esac

if [ ${flagError} = 0 ] ; then
if [ "${proxyStatus}" = "" ] ; then
printf "\n%bSet \"${PROXY_TYPE}\" proxy%b\n\n" "${REVERSE}${YELLOW}" "${STD}"
if [ "${PROXY_TYPE}" = "" ] ; then
printf "\n%bUnset proxy...%b\n" "${REVERSE}${YELLOW}" "${STD}"
unset PROXY_TYPE PROXY_HOST PROXY_PORT http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY
else
printf "\n%bSet \"${PROXY_TYPE}\" proxy%b\n" "${REVERSE}${YELLOW}" "${STD}"
PROXY="http://${PROXY_HOST}:${PROXY_PORT}"
export PROXY_TYPE
export http_proxy=${PROXY}
export HTTP_PROXY=${PROXY}
export https_proxy=${PROXY}
export HTTPS_PROXY=${PROXY}
export NO_PROXY="${NO_PROXY_INTERNAL},${INTRANET_DOMAINS}"
export no_proxy=${NO_PROXY}
export NO_PROXY
else
printf "\n%bUnset proxy...%b\n\n" "${REVERSE}${YELLOW}" "${STD}"
unset PROXY_TYPE http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY
fi
set_prompt

#--- Don't set prompt if NO_PROMPT var is not empty
if [ ! "${NO_PROMPT}" ] ; then
set_prompt
fi
fi

0 comments on commit 6636d95

Please sign in to comment.