Set system proxy with a shell script in MacOS
15 Dec 2017Have you experienced setting a proxy in your MacOS? If you have, you must be fairly clear about how painful it is. You must
- Open “System Preference”,
- Find “Network”,
- Find the right network interface, click on it,
- Click “Advanced..” in the right panel,
- Switch to “Proxies” tab,
- Check the desired proxies,
- Enter the proxy IP address and port,
- Click “OK”,
- Click “Apply”,
and finally, if you are still alive, the task is done.
But, if you are a coder, please do not do that for our title’s sake - you can save a ton of time with the networksetup
command. Among a variety of the supported operations on its manual page, -setwebproxy
option is for this case.
networksetup [-setwebproxy networkservice domain portnumber authenticated username password]
So, with the following script added in your $PATH
, you can simply set the system proxy by typing the script’s name and press enter in your favorite bash interface.
host=10.0.0.10
port=80
networksetup -setwebproxy Wi-Fi $host $port off
networksetup -setsecurewebproxy Wi-Fi $host $port off
🚀🚀🚀🚀🚀
What? You want to turn the proxy off and on in this way? Dig into the manual and see if it bites. 🍺