1

I have the following proxy info in my bash_profile:

export http_proxy=http://work.proxy.address.com:8080
export https_proxy=$http_proxy
export no_proxy="localhost,.workURL.com"

This allows me to run stuff like Homebrew without having to switch my wifi at work (we have two wifi networks--one is proxied). The issue is if I'm at home and not connected to the proxy, I can't use Homebrew or push commits to Github unless I comment out the proxy stuff.

Is it possible to conditionally use these proxy env variables based on the network I'm connected to?

2

Since you're talking about homebrew I will assume you are using a mac. You can use airport to get your SSID and then make a conditional on that. I have the following:

export CURR_SSID="$(airport -I | awk '$1 ~ "^SSID"{print $2}' | tr -d " ")"

I'm deleting the whitespace for reasons but that isn't really necessary.

Then you could make a conditional based on that:

if [[ $CURR_SSID == 'workSSID' ]]; then
  export http_proxy=http://work.proxy.address.com:8080
  export https_proxy=$http_proxy
  export no_proxy="localhost,.workURL.com"
fi
  • Do I have to reload my shell each time I change networks? – Nxt3 Aug 16 '18 at 14:39
  • No problem, glad it helps. You would need to reload your shell or at least source your bash_profile when you change networks. I use an alias to make that more simple: alias reload='source ~/.bash_profile' – Jesse_b Aug 16 '18 at 14:42
  • I have an alias for that already--but in my case at least, I have to open a new shell after switching wifi for the proxy env to be loaded or not. Not a big deal to me. Thanks again! – Nxt3 Aug 16 '18 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.