使用networksetup来设置代理

1. 执行以下命令来列出当前系统中的网络连接名称:

networksetup -listallnetworkservices

2. 设置http和https的 以太网 代理设置:

# 设置 HTTP 代理服务器为 192.168.1.20,端口号为 9090
networksetup -setwebproxy Ethernet 192.168.1.20 9090

# 开启 HTTP 代理
networksetup -setwebproxystate Ethernet on

# 设置 HTTPS 代理服务器为 192.168.1.20,端口号为 9090
networksetup -setsecurewebproxy Ethernet 192.168.1.20 9090

# 开启 HTTPS 代理
networksetup -setsecurewebproxystate Ethernet on

3. 设置http和https的 Wi-Fi 代理设置:

# 设置 HTTP 代理服务器为 192.168.1.20,端口号为 9090
networksetup -setwebproxy Wi-Fi 192.168.1.20 9090

# 开启 HTTP 代理
networksetup -setwebproxystate Wi-Fi on

# 设置 HTTPS 代理服务器为 192.168.1.20,端口号为 9090
networksetup -setsecurewebproxy Wi-Fi 192.168.1.20 9090

# 开启 HTTPS 代理
networksetup -setsecurewebproxystate Wi-Fi on

注: 以上参数备注

-setwebproxy  # 设置http代理
-setsecurewebproxy # 设置https代理
Ethernet #以太网的网络连接名称 (不清楚网络连接名称可以通过 networksetup -listallnetworkservices 获取)
Wi-Fi  #WiFi的网络连接名称 (不清楚网络连接名称可以通过 networksetup -listallnetworkservices 获取)
on # 开启代理
off # 关闭代理

4. 同时设置http/https/socket的代理设置:

# 设置代理服务器为 192.168.1.20,端口号为 8888
networksetup -setsocksfirewallproxy Ethernet 192.168.1.20 8888

# 开启代理
networksetup -setsocksfirewallproxystate Ethernet on

# 关闭代理
networksetup -setsocksfirewallproxystate Ethernet off

5. 查看以太网/WiFi设置的代理

networksetup -getsecurewebproxy Ethernet
#输出:
    Enabled: Yes
    Server: 192.168.1.20
    Port: 9090
    Authenticated Proxy Enabled: 0

networksetup -getsecurewebproxy Wi-Fi
#输出:
    Enabled: No
    Server: 127.0.0.1
    Port: 9090
    Authenticated Proxy Enabled: 0

6. 最后附上所有命令, 有另外需求再看文档

networksetup -help

networksetup Help Information
-------------------------------
Usage: networksetup -listnetworkserviceorder
    Display services with corresponding port and device in order they are tried for connecting
    to a network. An asterisk (*) denotes that a service is disabled.

Usage: networksetup -listallnetworkservices
    Display list of services. An asterisk (*) denotes that a network service is disabled.

Usage: networksetup -listallhardwareports
    Display list of hardware ports with corresponding device name and ethernet address.

Usage: networksetup -detectnewhardware
    Detect new network hardware and create a default network service on the hardware.

Usage: networksetup -getmacaddress <hardwareport or device name>
    Display ethernet (or Wi-Fi) address for hardwareport or device specified.

Usage: networksetup -getcomputername
    Display the computer name.

Usage: networksetup -setcomputername <name>
    Set the computer's name (if valid) to <name>.

Usage: networksetup -getinfo <networkservice>
    Display IPv4 address, IPv6 address, subnet mask,
    router address, ethernet address for <networkservice>.

Usage: networksetup -setmanual <networkservice> <ip> <subnet> <router>
    Set the <networkservice> TCP/IP configuration to manual with IP address set to ip,
    Subnet Mask set to subnet, and Router address set to router.

Usage: networksetup -setdhcp <networkservice> [clientid]
    Set the <networkservice> TCP/IP configuration to DHCP. You can set the
    DHCP client id to the optional [clientid]. Specify "Empty" for [clientid]
    to clear the DHCP client id.

Usage: networksetup -setbootp <networkservice>
    Set the <networkservice> TCP/IP configuration to BOOTP.

Usage: networksetup -setmanualwithdhcprouter <networkservice> <ip> 
    Set the <networkservice> TCP/IP configuration to manual with DHCP router with IP address set
    to ip.

Usage: networksetup -getadditionalroutes <networkservice>
    Get additional IPv4 routes associated with <networkservice>
Usage: networksetup -setadditionalroutes <networkservice> [ <dest> <mask> <gateway> ]*
    Set additional IPv4 routes associated with <networkservice>
    by specifying one or more [ <dest> <mask> <gateway> ] tuples.
    Remove additional routes by specifying no arguments.
    If <gateway> is "", the route is direct to the interface
Usage: networksetup -setv4off <networkservice> 
    Turn IPv4 off on <networkservice>. 

Usage: networksetup -setv6off <networkservice> 
    Turn IPv6 off on <networkservice>. 

Usage: networksetup -setv6automatic <networkservice> 
    Set the service to get its IPv6 info automatically. 

Usage: networksetup -setv6LinkLocal <networkservice> 
    Set the service to use its IPv6 only for link local. 

Usage: networksetup -setv6manual <networkservice> <address> <prefixlength> <router>
    Set the service to get its IPv6 info manually.
    Specify <address> <prefixLength> and <router>.

Usage: networksetup -getv6additionalroutes <networkservice>
    Get additional IPv6 routes associated with <networkservice>
Usage: networksetup -setv6additionalroutes <networkservice> [ <dest> <prefixlength> <gateway> ]*
    Set additional IPv6 routes associated with <networkservice>
    by specifying one or more [ <dest> <prefixlength> <gateway> ] tuples.
    Remove additional routes by specifying no arguments.
    If <gateway> is "", the route is direct to the interface
Usage: networksetup -getdnsservers <networkservice>
    Display DNS info for <networkservice>.

Usage: networksetup -setdnsservers <networkservice> <dns1> [dns2] [...] 
    Set the <networkservice> DNS servers to <dns1> [dns2] [...]. Any number of dns servers can be
    specified. Specify "Empty" for <dns1> to clear all DNS entries.

Usage: networksetup -getsearchdomains <networkservice>
    Display Domain Name info for <networkservice>.

Usage: networksetup -setsearchdomains <networkservice> <domain1> [domain2] [...] 
    Set the <networkservice> Domain Name servers to <domain1> [domain2] [...]. Any number of Domain Name
    servers can be specified. Specify "Empty" for <domain1> to clear all Domain Name entries.

Usage: networksetup -create6to4service <newnetworkservicename> 
    Create a 6 to 4 service with name <newnetworkservicename>.

Usage: networksetup -set6to4automatic <networkservice> 
    Set the service to get its 6 to 4 info automatically. 

Usage: networksetup -set6to4manual <networkservice> <relayaddress>
    Set the service to get its 6 to 4 info manually. 
    Specify <relayaddress> for the relay address.

Usage: networksetup -getwebproxy <networkservice>
    Display Web proxy (server, port, enabled value) info for <networkservice>.

Usage: networksetup -setwebproxy <networkservice> <domain> <port number> <authenticated> <username> <password>
    Set Web proxy for <networkservice> with <domain> and <port number>. Turns proxy on. Optionally, specify <on> or <off> for <authenticated> to enable and disable authenticated proxy support. Specify <username> and <password> if you turn authenticated proxy support on.

Usage: networksetup -setwebproxystate <networkservice> <on off>
    Set Web proxy to  either <on> or <off>.

Usage: networksetup -getsecurewebproxy <networkservice>
    Display Secure Web proxy (server, port, enabled value) info for <networkservice>.

Usage: networksetup -setsecurewebproxy <networkservice> <domain> <port number> <authenticated> <username> <password>
    Set Secure Web proxy for <networkservice> with <domain> and <port number>. Turns proxy on. Optionally, specify <on> or <off> for <authenticated> to enable and disable authenticated proxy support. Specify <username> and <password> if you turn authenticated proxy support on.

Usage: networksetup -setsecurewebproxystate <networkservice> <on off>
    Set SecureWeb proxy to  either <on> or <off>.

Usage: networksetup -getsocksfirewallproxy <networkservice>
    Display SOCKS Firewall proxy (server, port, enabled value) info for <networkservice>.

Usage: networksetup -setsocksfirewallproxy <networkservice> <domain> <port number> <authenticated> <username> <password>
    Set SOCKS Firewall proxy for <networkservice> with <domain> and <port number>. Turns proxy on. Optionally, specify <on> or <off> for <authenticated> to enable and disable authenticated proxy support. Specify <username> and <password> if you turn authenticated proxy support on.

Usage: networksetup -setsocksfirewallproxystate <networkservice> <on off>
    Set SOCKS Firewall proxy to  either <on> or <off>.

Usage: networksetup -getproxybypassdomains <networkservice>
    Display Bypass Domain Names for <networkservice>.

Usage: networksetup -setproxybypassdomains <networkservice> <domain1> [domain2] [...]
    Set the Bypass Domain Name Servers for <networkservice> to <domain1> [domain2] [...]. Any number of
    Domain Name servers can be specified. Specify "Empty" for <domain1> to clear all
    Domain Name entries.

Usage: networksetup -getproxyautodiscovery <networkservice>
    Display whether Proxy Auto Discover is on or off for <network service>.

Usage: networksetup -setproxyautodiscovery <networkservice> <on off>
    Set Proxy Auto Discovery to either <on> or <off>.

Usage: networksetup -setautoproxyurl <networkservice> <url>
    Set proxy auto-config to url for <networkservice> and enable it.

Usage: networksetup -getautoproxyurl <networkservice>
    Display proxy auto-config (url, enabled) info for <networkservice>.

Usage: networksetup -setautoproxystate <networkservice> <on off>
    Set proxy auto-config to either <on> or <off>.

Usage: networksetup -getairportnetwork <device name>
    Display current Wi-Fi Network for <device name>.

Usage: networksetup -setairportnetwork <device name> <network> [password]
    Set Wi-Fi Network to <network> for <device name>.
    If a password is included, it gets stored in the keychain.

Usage: networksetup -getairportpower <device name>
    Display whether Wi-Fi power is on or off for <device name>.

Usage: networksetup -setairportpower <device name> <on off>
    Set Wi-Fi power for <device name> to either <on> or <off>.

Usage: networksetup -listpreferredwirelessnetworks <device name>
    List the preferred wireless networks for <device name>.

Usage: networksetup -addpreferredwirelessnetworkatindex <device name> <network> <index> <security type> [password]
    Add wireless network named <network> to preferred list for <device name> at <index>.
    For security type, use OPEN for none, WPA for WPA Personal, WPAE for WPA Enterprise, 
    WPA2 for WPA2 Personal, WPA2E for WPA2 Enterprise, WEP for plain WEP, and 8021XWEP for 802.1X WEP.
    If a password is included, it gets stored in the keychain.

Usage: networksetup -removepreferredwirelessnetwork <device name> <network>
    Remove <network> from the preferred wireless network list for <device name>.

Usage: networksetup -removeallpreferredwirelessnetworks <device name>
    Remove all networks from the preferred wireless network list for <device name>.

Usage: networksetup -getnetworkserviceenabled <networkservice>
    Display whether a service is on or off (enabled or disabled).

Usage: networksetup -setnetworkserviceenabled <networkservice> <on off>
    Set <networkservice> to either <on> or <off> (enabled or disabled).

Usage: networksetup -createnetworkservice <newnetworkservicename> <hardwareport>
    Create a service named <networkservice> on port <hardwareport>. The new service will be enabled by default.

Usage: networksetup -renamenetworkservice <networkservice> <newnetworkservicename>
    Rename <networkservice> to <newnetworkservicename>.

Usage: networksetup -duplicatenetworkservice <networkservice> <newnetworkservicename>
    Duplicate <networkservice> and name it with <newnetworkservicename>.

Usage: networksetup -removenetworkservice <networkservice>
    Remove the service named <networkservice>. Will fail if this is the only service on the hardware port that <networkservice> is on.

Usage: networksetup -ordernetworkservices <service1> <service2> <service3> <...>
    Order the services in order specified. Use "-listnetworkserviceorder" to view service order.
    Note: use quotes around service names which contain spaces (ie. "Built-in Ethernet").

Usage: networksetup -setMTUAndMediaAutomatically <hardwareport or device name>
    Set hardwareport or device specified back to automatically setting the MTU and Media.

Usage: networksetup -getMTU <hardwareport or device name>
    Get the MTU value for hardwareport or device specified.

Usage: networksetup -setMTU <hardwareport or device name> <value>
    Set MTU for hardwareport or device specified.

Usage: networksetup -listvalidMTUrange <hardwareport or device name>
    List the valid MTU range for hardwareport or device specified.

Usage: networksetup -getmedia <hardwareport or device name>
    Show both the current setting for media and the active media on hardwareport or device specified.

Usage: networksetup -setmedia <hardwareport or device name> <subtype> [option1] [option2] [...]
    Set media for hardwareport or device specified to subtype. Specify optional [option1] and additional options depending on subtype. Any number of valid options can be specified.

Usage: networksetup -listvalidmedia <hardwareport or device name>
    List valid media options for hardwareport or device name. Enumerates available subtypes and options per subtype.

Usage: networksetup -createVLAN <VLAN name> <device name> <tag>
    Create a VLAN with name <VLAN name> over device <device name> with unique tag <tag>. A default network service will be created over the VLAN.

Usage: networksetup -deleteVLAN <VLAN name> <device name> <tag>
    Delete the VLAN with name <VLAN name> over the parent device <device name> with unique tag <tag>. If there are network services running over the VLAN they will be deleted.

Usage: networksetup -listVLANs
    List the VLANs that have been created.

Usage: networksetup -listdevicesthatsupportVLAN
    List the devices that support VLANs.

Usage: networksetup -isBondSupported <device name ie., en0>
    Return YES if the specified device can be added to a bond. NO if it cannot.

Usage: networksetup -createBond <user defined name> <device name 1> <device name 2> <...>
    Create a new bond and give it the user defined name. Add the specified devices, if any, to the bond.

Usage: networksetup -deleteBond <bond name ie., bond0>
    Delete the bond with the specified device-name.

Usage: networksetup -addDeviceToBond <device name> <bond name> 
    Add the specified device to the specified bond.

Usage: networksetup -removeDeviceFromBond <device name> <bond name>
    Remove the specified device from the specified bond

Usage: networksetup -listBonds
    List all of the bonds.

Usage: networksetup -showBondStatus <bond name ie., bond0>
    Display the status of the specified bond.

Usage: networksetup -listpppoeservices
    List all of the PPPoE services in the current set.

Usage: networksetup -showpppoestatus <service name ie., MyPPPoEService>
    Display the status of the specified PPPoE service.

Usage: networksetup -createpppoeservice <device name ie., en0> <service name> <account name> <password> [pppoe service name]
    Create a PPPoE service on the specified device with the service name specified.
    The "pppoe service name" is optional and may not be supported by the service provider.

Usage: networksetup -deletepppoeservice <service name>
    Delete the PPPoE service.

Usage: networksetup -setpppoeaccountname <service name> <account name>
    Sets the account name for the specified service.

Usage: networksetup -setpppoepassword <service name> <password>
    Sets the password stored in the keychain for the specified service.

Usage: networksetup -connectpppoeservice <service name>
    Connect the PPPoE service.

Usage: networksetup -disconnectpppoeservice <service name>
    Disconnect the PPPoE service.

Usage: networksetup -getcurrentlocation
    Display the name of the current location.

Usage: networksetup -listlocations
    List all of the locations.

Usage: networksetup -createlocation <location name> [populate]
    Create a new network location with the spcified name.
    If the optional term "populate" is included, the location will be populated with the default services.

Usage: networksetup -deletelocation <location name>
    Delete the location.

Usage: networksetup -switchtolocation <location name>
    Make the specified location the current location.

Usage: networksetup -version
    Display version of networksetup tool.

Usage: networksetup -help
    Display these help listings.

Usage: networksetup -printcommands
    Displays a quick listing of commands (without explanations).

Any command that takes a password, will accept - to indicate the password should be read from stdin.

The networksetup tool requires at least admin privileges to change network settings. If the "Require an administrator password to access system-wide preferences" option is selected in System Preferences > Security & Privacy, then root privileges are required to change network settings.
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容