Last active
October 7, 2020 18:24
-
-
Save madlymad/67b05f218780bc464595269ea2648af4 to your computer and use it in GitHub Desktop.
MacOS script for loading system proxy setting at bash and git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
proxyToFile() { | |
local filename="$2" | |
# break if filename not found | |
if [[ ! -f $filename ]]; then return -1; fi | |
local add="$1" | |
if [[ "$add" = "true" ]]; then | |
perl -pi -e 's/^#http/http/' $filename | |
else | |
perl -pi -e 's/^http/#http/' $filename | |
fi | |
} | |
prxyIP=$(system_profiler SPNetworkDataType | grep "HTTP Proxy Server" | awk '{print $4}' | head -1) | |
prxyPort=$(system_profiler SPNetworkDataType | grep "HTTP Proxy Port" | awk '{print $4}' | head -1) | |
if [ ! -z ${prxyIP} ] && [ ${prxyIP} != "0" ] | |
then | |
prxyCfg="http://${prxyIP}:${prxyPort}" | |
prxy="${prxyIP}:${prxyPort}" | |
export http_proxy="${prxyCfg}" | |
export HTTP_PROXY="${prxyCfg}" | |
export https_proxy="${prxyCfg}" | |
export HTTPS_PROXY="${prxyCfg}" | |
export ALL_PROXY=${prxy} | |
export all_proxy=${prxy} | |
# Set proxy at git config | |
git config --global http.proxy ${HTTP_PROXY} | |
git config --global https.proxy ${HTTPS_PROXY} | |
# curl -x <proxy_host>:<proxy_port> | |
alias curl="curl -x '${prxy}'" | |
# Uncomment proxy from Ruby config | |
proxyToFile true ~/.gemrc | |
echo "Configured proxy $HTTPS_PROXY" | |
else | |
unset http_proxy | |
unset HTTP_PROXY | |
unset https_proxy | |
unset HTTPS_PROXY | |
# Remove proxy from git config | |
git config --global --unset http.proxy | |
git config --global --unset https.proxy | |
# Comment proxy at Ruby config | |
proxyToFile false ~/.gemrc | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use it add in your
.bashrc
or.bash_profile