乐闻世界logo
搜索文章和话题

How to you toggle on and off a web proxy in os x from the command line

1个答案

1

In macOS, you can manage network settings through the command line using the networksetup command, including enabling and disabling the web proxy. Below are the steps to follow:

Enable Web Proxy

  1. Identify the Network Service Name

First, identify the exact name of the network service you want to configure (e.g., Wi-Fi, Ethernet). You can list all available network services using the following command:

bash
networksetup -listallnetworkservices
  1. Enable Web Proxy

Assuming your network service is named 'Wi-Fi', use the following command to enable the HTTP proxy:

bash
sudo networksetup -setwebproxy Wi-Fi 127.0.0.1 8080

In the above command, 127.0.0.1 is the IP address of the proxy server, and 8080 is the port number. Replace these with the actual IP address and port of your proxy server.

  1. Enable Secure Web Proxy (HTTPS Proxy)

If you also need to configure the proxy for HTTPS, use the following command:

bash
sudo networksetup -setsecurewebproxy Wi-Fi 127.0.0.1 8080

Disable Web Proxy

  1. Disable Web Proxy

To disable the HTTP proxy, run:

bash
sudo networksetup -setwebproxystate Wi-Fi off
  1. Disable Secure Web Proxy

Similarly, to disable the HTTPS proxy:

bash
sudo networksetup -setsecurewebproxystate Wi-Fi off

Example: Integrating Commands into a Script

You can integrate these commands into a shell script to quickly enable or disable the proxy as needed. For example:

bash
#!/bin/bash # Enable proxy function enable_proxy() { sudo networksetup -setwebproxy Wi-Fi 127.0.0.1 8080 sudo networksetup -setsecurewebproxy Wi-Fi 127.0.0.1 8080 echo "HTTP and HTTPS proxies are enabled." } # Disable proxy function disable_proxy() { sudo networksetup -setwebproxystate Wi-Fi off sudo networksetup -setsecurewebproxystate Wi-Fi off echo "HTTP and HTTPS proxies are disabled." } # Choose operation based on input parameter case "$1" in on) enable_proxy ;; off) disable_proxy ;; *) echo "Usage: $0 on|off" exit 1 ;; esac

Thus, you can run ./proxy_script.sh on to enable the proxy or ./proxy_script.sh off to disable it.

This approach allows you to flexibly manage the network proxy settings in macOS, accommodating various networking configuration needs.

2024年8月5日 10:00 回复

你的答案