Search code examples
terminalosx-yosemite

How would I configure a proxy in OSX terminal


How would I add and activate a proxy for my computer via terminal (without going into System Preferences.) Can I do that with scutil --proxy somehow? I'm running on OSX Yosemite with a MacBook Air


Solution

  • Method 1

    We can use awk to parse the output of scutil and extract the proxy configuration. The following snippet does the trick:

    $ export http_proxy=`scutil --proxy | awk '\
    /HTTPEnable/ { enabled = $3; } \
    /HTTPProxy/ { server = $3; } \
    /HTTPPort/ { port = $3; } \
    END { if (enabled == "1") { print "http://" server ":" port; } }'`
    $ export HTTP_PROXY="${http_proxy}"
    

    This script looks for HTTPEnable, HTTPProxy, and HTTPPort in the output of scutil. If the proxy is enabled, the script prints out the proxy URL and sets it as the http_proxy environment variable. If the proxy is not enabled, the script sets http_proxy to an empty string. The final line sets the HTTP_PROXY environment variable as well since some command-line applications use that instead.

    Placing this snippet in your .bash_profile ensures that your proxy will stay configured automatically while switching between wired and wireless networks.

    Method 2

    You could try creating a bash login script that uses one of the following uses of "networksetup" to list the current proxy, and then parse out the server address and apply it to the current terminal session:

    networksetup -getftpproxy <servicename>
    networksetup -getwebproxy <servicename>
    networksetup -getsecurewebproxy <servicename>
    networksetup -getstreamingproxy <servicename>
    networksetup -getgopherproxy <servicename>
    networksetup -getsocksfirewallproxy <servicename>
    

    There may be other uses of the networksetup tool that can give you the specific proxy you are using, just look up "man networksetup" to see all the details and uses.

    Method 3 Terminal does not use proxy settings configured in the network preferences pane because it doesn't do any connection. Terminal just let you fire commands which will use the network in different ways.

    export http_proxy="username:password@proxyserver:port"
    

    reference: https://dmorgan.info/posts/mac-network-proxy-terminal/