Setting Required Proxy Variables Using Bash Shell (URL Encoding Included)

Today I come to you with a script I’ve created that helps to set the http_proxy/https_proxy/ftp_proxy/no_proxy variables and configruations for Yum, Git, and other basic commands that may make sure of these configurations.

I created this one due to the need to easily jump on any Linux Box and punch through a proxy then remove my credentials after.

This script uses strictly native Bash functionality to create a completely native Linux approach towards setting required configurations when working behind a Corporate Proxy.

The process would be as follows:

  1. Open SSH Terminal (Or Git-Bash/Cygwin)
  2. Execute this Bash Script using the “source” command
  3. Perform your Required Commands (pip/curl/yum/wget/etc)

Here is the script:

#!/bin/bash

##################################
#### Defining URL Encode     ####
#### Function for Passwords  ####
#### with Special Characters ####
#################################

rawurlencode() {
  local string="${1}"
  local strlen=${#string}
  local encoded=""
  local pos c o

  for (( pos=0 ; pos<strlen ; pos++ )); do
     c=${string:$pos:1}
     case "$c" in
        [-_.~a-zA-Z0-9] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
     esac
     encoded+="${o}"
  done
  echo "${encoded}"
}

PROXY_ADDRESS=yourCorporateProxy.com
PROXY_PORT=80
PROXY_BYPASS=localhost,127.0.0.1
ENVIRONMENT_VARIABLE_LIST="http_proxy https_proxy ftp_proxy"
GIT_ENVIRONMENT_VARIABLE_LIST="http.proxy https.proxy"

setProxy() {

	###################################
	#### Defining Proxy Parameters ####
	###################################

	echo "Enter Username: "
	read USERNAME

	echo "Enter Password: "
	read -s PASSWORD

	PASSWORD_ENCODED=$(rawurlencode $PASSWORD)

	PROXY_URL="http://$USERNAME:$PASSWORD_ENCODED@$PROXY_ADDRESS:$PROXY_PORT/"


	for ENVIRONMENT_VARIABLE in $ENVIRONMENT_VARIABLE_LIST
	do
		TEMP=$ENVIRONMENT_VARIABLE="$PROXY_URL"
		export $TEMP
	done

	export no_proxy=$PROXY_BYPASS

	###################################
	#### Setting GIT Configuration ####
	###################################

	#for ENVIRONMENT_VARIABLE in $GIT_ENVIRONMENT_VARIABLE_LIST
	#do
		#Apparently Not Needed if Above Environment Variables are set
		#git config --global $ENVIRONMENT_VARIABLE $PROXY_URL
	#done

	###################################
	#### Setting YUM Configuration ####
	###################################

	echo proxy=http://$PROXY_ADDRESS:$PROXY_PORT >> /etc/yum.conf
	echo proxy_username=$USERNAME >> /etc/yum.conf
	echo proxy_password=$PASSWORD >> /etc/yum.conf
}

unsetProxy() {
	if [[ $suffix == *"User"* ]]; then
		JAVA_ARGS+=" $prefix.$suffix=$USERNAME"
	fi
	sed -i '/proxy=/d' /etc/yum.conf
	sed -i '/proxy_username=/d' /etc/yum.conf
	sed -i '/proxy_password=/d' /etc/yum.conf

	for ENVIRONMENT_VARIABLE in $ENVIRONMENT_VARIABLE_LIST
	do
		TEMP=$ENVIRONMENT_VARIABLE=""
		export $TEMP
	done

	export no_proxy=""
	
	for ENVIRONMENT_VARIABLE in $GIT_ENVIRONMENT_VARIABLE_LIST
	do
		git config --global --unset $ENVIRONMENT_VARIABLE 
	done
}


launchJavaJar() {
	######################################################
	#### Setting JAVA Launch Parameters Configuration ####
	######################################################

	echo "Type Java JAR Path: "
	read JAVA_JAR_PATH

	JAVA_ARGS=""

	PREFIX_LIST="-Dhttp -Dhttps"
	SUFFIX_LIST="proxyHost proxyPort proxyUser proxyPassword"

	for prefix in $PREFIX_LIST
	do
		 for suffix in $SUFFIX_LIST
		 do
			if [[ $suffix == *"Host"* ]]; then
				JAVA_ARGS+=" $prefix.$suffix=$PROXY_ADDRESS"
			fi

			if [[ $suffix == *"Port"* ]]; then
				JAVA_ARGS+=" $prefix.$suffix=$PROXY_PORT"
			fi	
			
			if [[ $suffix == *"User"* ]]; then
				JAVA_ARGS+=" $prefix.$suffix=$USERNAME"
			fi

			if [[ $suffix == *"Password"* ]]; then
				JAVA_ARGS+=" $prefix.$suffix=\"$PASSWORD\""
			fi	  
		 done
	done
	
	JAVA_ARGS+=" -Djava.net.useSystemProxies=true"
	JAVA_ARGS+=" -Djdk.http.auth.tunneling.disabledSchemes=\"\""
	JAVA_ARGS+=" -Djdk.http.auth.proxying.disabledSchemes=\"\""

	java $JAVA_ARGS -jar "$JAVA_JAR_PATH"
}

# Bash Menu Script Example
choiceFunction() {
	SUCCESS="false"

	while [ $SUCCESS == "false" ]
	do
		clear
		echo 'Please enter your choice: '
		echo "1 - Set Proxy Settings (Yum, Environment, GIT)" 
		echo "2 - Disable/Remote Proxy Settings (Yum, Environment, GIT)" 
		echo "3 - Launch Java JAR with Proxy Settings" 
		echo "4 - Quit"
		read CHOICE

		case $CHOICE in
		   "1") SUCCESS="true";echo "Setting Proxy..."; unsetProxy; setProxy;;
		   "2") SUCCESS="true";echo "Disabling Proxy..."; unsetProxy;;
		   "3") SUCCESS="true";echo "Launching JAR with Proxy Settings...";unsetProxy; setProxy;launchJavaJar;;
		   "4") SUCCESS="true";echo "Exitting..."; exit;;
		   *) read -p "Invalid choice, Press any key to continue..." fakeVariableIgnoreMe;;
		esac
	done
}

choiceFunction

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s