Skip to content

Instantly share code, notes, and snippets.

@vviikk
Forked from kamermanpr/proxyscript.sh
Created May 27, 2016 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vviikk/83eb84be51f592a47837ed8dc2342680 to your computer and use it in GitHub Desktop.
Save vviikk/83eb84be51f592a47837ed8dc2342680 to your computer and use it in GitHub Desktop.
Bash script to toggle proxy server settings for the terminal, git, and R based on network location setting (OSX)
#!/bin/bash
# By Peter Kamerman (peter.kamerman@gmail.com)
##########
# FUNCTION
##########
# Script to toggle the proxy configurations of the terminal, git, and R
# for use behind a proxy server (e.g., at work) and no proxy (e.g., home).
########################
# DISCLAIMER and LICENSE
########################
# I cobbled this script together from responses to questions on setting-up
# the bash terminal, git, and R to work through a proxy server, which I found
# on various blog sites and stackoverflow comments. I have no experience with
# programming bash scripts, so the code probably does not meet the standards
# of those with more skill. Feel free to use and change the script as you
# see fit.
# -----------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# Peter Kamerman wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you
# think this stuff is worth it, you can buy me a beer in return.
# (Poul-Henning Kamp)
# -----------------------------------------------------------------------
##############
# REQUIREMENTS
##############
# OSX (only tested on OSX 10.11 - El Capitan)
#######
# SETUP
#######
# 1. Clone the script to your chosen directory on your hard-drive.
# 2. Open using TextEdit.app or similar, and change the following settings:
# - set 'NAME' to the relevant system location where you require a proxy
# - set 'YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD@YOUR.PROXY.SERVER:8080'
# 3. Save and change and '.sh' to '.command' to make it executable.
#####
# USE
#####
# 1. Quit the terminal and any applications using R or git.
# 2. Change your network location to the desired location.
# 3. Double-click the script, and then close the terminal when it has run.
# 4. The script should detect the location, and set the proxy as appropriate.
########
# SCRIPT
########
# set 'NAME; to the relevant system location where you require a proxy
# set 'YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD@YOUR.PROXY.SERVER:8080'
# according to your proxy settings. If no username/password is required,
# delete: 'YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD@'
# In some cases all lines with 'http(s).proxy...' or 'http(s)_proxy...' may need to be duplicated,
# but with the 'http(s).proxy...' and 'http(s)_proxy...' parts in UPPER-CASE.
value=$( networksetup -getcurrentlocation | grep -c "NAME" )
if [ $value -eq 1 ]
then
export http_proxy=http://YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD@YOUR.PROXY.SERVER:8080
export https_proxy=https://YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD@YOUR.PROXY.SERVER:8080
export no_proxy=localhost,127.0.0.0/8,*.local
git config --global http.proxy http://YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD@YOUR.PROXY.SERVER:8080
git config --global https.proxy https://YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD@YOUR.PROXY.SERVER:8080
{
echo 'http_proxy=http://YOUR.PROXY.SERVER:8080'
echo 'http_proxy_user=YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD'
echo 'https_proxy=https://pYOUR.PROXY.SERVER:8080'
echo 'https_proxy_user=YOUR_PROXY_USERNAME:YOUR_PROXY_PASSWORD'
} > /Users/USERNAME/.Renviron
else
unset http_proxy
unset https_proxy
git config --global --unset http.proxy
git config --global --unset https.proxy
{
echo ''
echo ''
echo ''
echo ''
} > /Users/USERNAME/.Renviron
fi
# Set shell close setting (Terminal > Preferences > Profiles > Shell)
# to: 'When the shell exits: > Close if the shell exited cleanly'
quit terminal
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment