Reason (Pain point)
Each time you set up a Web proxy on your Mac, you'll need to click on " System Preferences-network-advanced-Agent ", set up the Web Proxy (HTTP) and secure Web Proxy (HTTPS) separately, and then " good-Apply " when you're done. This is true if you want to turn off Web proxy, but only after you point to the Proxy Settings panel, simply uncheck the Web Proxy and secure Web proxy.
Personally, this kind of operation is very cumbersome, for a person who often need to change the Web proxy, this is a catastrophic repeat operation. So this time I learned some Linux shell script programming knowledge, wrote two shell commands to set up and switch the Web Proxy.
Set up and open a shell script for the Web Proxy
#!/bin/bash#export path=/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbinclearnetworkservice=$ (Networksetup- listallnetworkservices | Head-n 2 | Tail-n 1) #获取Mac当前使用的网络名称echo Open Web proxy for NetworkService: $networkserviceif ["$" = ""]; Then sudo networksetup-setwebproxy $networkservice $ #设置Web http proxy sudo networksetup- Setsecurewebproxy $networkservice $ #设置Web HTTPS proxy Fisudo networksetup-setwebproxystate $networkservice on #打开Web http proxy sudo networksetup-setsecurewebproxystate $networkservice on #打开Web HTTPS proxy echo done
How to use this script
Save the above script as a file, say Proxyon, and then move the Proxyon file to a bin directory, such as:
/usr/local/bin/
Then we can quickly set up and open the Web proxy through this command at the terminal:
sudo proxyon #仅打开Web代理sudo proxyon 115.231.175.68 8081 #设置并打开Web代理
To close the shell script for the Web Proxy
#!/bin/bash#export path=/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbinclearnetworkservice=$ (Networksetup- listallnetworkservices | Head-n 2 | Tail-n 1) #获取Mac当前使用的网络名称echo Close Web proxy for NetworkService: $networkservicesudo Networksetup- Setwebproxystate $networkservice off #关闭Web http proxy sudo networksetup-setsecurewebproxystate $networkservice off #关闭Web HTTPS proxy echo done
How to use this script
With Proxyon, we save the above script as a file, for example, called Proxyoff, and then move the Proxyff file to a bin directory, such as:
/usr/local/bin/
Then we can quickly close the Web proxy with this command at the terminal:
sudo proxyoff #关闭Web代理
Next time write a script that automatically gets the network proxy available on the Internet.
Wrote a Mac quick setup, open and close shell command for Web Proxy