Wrote a Mac quick setup, open and close shell command for Web Proxy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.