2

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

ALX
  • 283
  • 1
  • 4
  • 11
  • use `networksetup -setwebproxystate Thunderbolt\ Ethernet on` to turn 'Thunderbolt Ethernet' on/off use `networksetup -setwebproxy Thunderbolt\ Ethernet localhost 8080` to set 'Thunderbolt Ethernet' host to localhost and port to 8080 use `networksetup` to see help information – igrek Jan 31 '19 at 12:17

2 Answers2

3

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/

Equinox
  • 6,483
  • 3
  • 23
  • 32
0

I wrote a little tool that does exactly that. Usage is straightforward.

  1. Compile it with xCode.
  2. Add:

    eval `./path/to/export-proxies`
    

    to your .profile, .bashrc, or .bash_profile.

It will set all commonly used proxy environment variables ($HTTP_PROXY, $http_proxy, $HttpProxy, etc.) for all protocols (HTTP, FTP, SOCKS) including all exceptions as defined in the OS X control panel.

Suggestions and improvements are also always welcome ;)

EDIT I just saw you want to set the settings instead of reading them. I haven't found a way to do this so far. But would be happy to learn otherwise...

jan.vogt
  • 1,801
  • 10
  • 27