Skip to content

Instantly share code, notes, and snippets.

@alexargo
Last active October 22, 2022 04:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexargo/4657760 to your computer and use it in GitHub Desktop.
Save alexargo/4657760 to your computer and use it in GitHub Desktop.
Script to set your proxy information based on the current default service on Mac OS X... usefull to configure the http_proxy/https_proxy variables when you have a mac running on a corporate network. - Updated for Mountain Lion
#!/bin/bash
#take a configurable service from first argument
if [ $# = 1 ]
then
SERVICE_NAME=$1
fi
#otherwise get the active one
if [ $# = 0 ]
then
#find service for default route
SERVICE_GUID=`printf "open\nget State:/Network/Global/IPv4\nd.show" | \
scutil | grep "PrimaryService" | awk '{print $3}'`
#find service name for guid
SERVICE_NAME=`printf "open\nget Setup:/Network/Service/$SERVICE_GUID\nd.show" |\
scutil | grep "UserDefinedName" | awk -F': ' '{print $2}'`
fi
echo "Copying http proxy configuration for $SERVICE_NAME to http_proxy"
#setup http proxy
PROXY_HOSTNAME=`networksetup -getwebproxy "$SERVICE_NAME" | awk {'print $2'} | awk 'FNR == 2 {print}' | cut -d' ' -f2`
# PROXY_HOSTNAME=`eval $PROXY_HOSTNAME_COMMAND`
PROXY_PORT=`networksetup -getwebproxy "$SERVICE_NAME" | awk {'print $2'} | awk 'FNR == 3 {print}' | cut -d' ' -f2`
# PROXY_PORT=`eval $PROXY_PORT_COMMAND`
PROXY_USER=`security find-internet-password -s "$PROXY_HOSTNAME" | grep "acct" | cut -d '"' -f 4`
PROXY_PASS=`security 2>&1 >/dev/null find-internet-password -gs "$PROXY_HOSTNAME" | cut -d '"' -f 2`
export http_proxy="http://$PROXY_USER:$PROXY_PASS@$PROXY_HOSTNAME:$PROXY_PORT/";
echo "Copying https proxy configuration for $SERVICE_NAME to https_proxy"
#setup https proxy
SECURE_PROXY_HOSTNAME=`networksetup -getsecurewebproxy "$SERVICE_NAME" | awk {'print $2'} | awk 'FNR == 2 {print}' | cut -d' ' -f2`
# SECURE_PROXY_HOSTNAME=`eval $SECURE_PROXY_HOSTNAME_COMMAND`
SECURE_PROXY_PORT=`networksetup -getsecurewebproxy "$SERVICE_NAME" | awk {'print $2'} | awk 'FNR == 3 {print}' | cut -d' ' -f2`
# SECURE_PROXY_PORT=`eval $SECURE_PROXY_PORT_COMMAND`
SECURE_PROXY_USER=`security find-internet-password -s "$SECURE_PROXY_HOSTNAME" | grep "acct" | cut -d '"' -f 4`
SECURE_PROXY_PASS=`security 2>&1 >/dev/null find-internet-password -gs "$SECURE_PROXY_HOSTNAME" | cut -d '"' -f 2`
export https_proxy="https://$SECURE_PROXY_USER:$SECURE_PROXY_PASS@$SECURE_PROXY_HOSTNAME:$SECURE_PROXY_PORT/";
#Only print these out to debug.... passwords in logs are bad
#echo $http_proxy
#echo $https_proxy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment