how to toggle web proxy from commandline

Awesome tips!

I don’t usually use proxy servers. But depending on the network environment, I need to connect proxy servers.

I had been searching for the solution for switching proxy. The more simple or easier, the better.
I tried locamatic, or other softwares. But nothing served well.

Now, that post taught me how to manipulate proxy setting from command line.
That suggests the commads below:

networksetup -setwebproxy Ethernet proxy.example.net 80 off
networksetup -setwebproxy Ethernet proxy.example.net 80 on proxyuser "p4ssw0rd"
networksetup -setwebproxystate Ethernet off

And then, I added one function to my bashrc for myself.

setPrefProxy() {
    server=$1
    port=$2
    adapter=$3
    mode=$4

    echo "s:$1 p:$2 m:$3"

    if [ ${mode} = "on" ];then
	    networksetup -setwebproxy ${adapter} ${server} ${port} off
	    networksetup -setsecurewebproxy ${adapter} ${server} ${port} off
	    networksetup -setftpproxy ${adapter} ${server} ${port} off
    else
	    networksetup -setwebproxystate ${adapter} off
	    networksetup -setsecurewebproxystate ${adapter} off
	    networksetup -setftpproxystate ${adapter} off
    fi
}

Usage:

setPrefProxy proxy.server.com 8080 Ethernet on[off]
  • You have to input password for modifying configuration.

OK. It may be life-changing.