0%

Mac网络快捷指令-实现快速开启关闭网络Proxy

networksetup -setwebproxystate 'Thunderbolt Ethernet' on

背景:

想快速切换网络代理的状态(on 或 off)

确定方案

https://support.apple.com/zh-cn/guide/remote-desktop/apdd0c5a2d5/mac

从官方帮助手册看到networksetup命令,通过networksetup -help获取命令帮助手册

可以看到:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查看网络代理状态
networksetup -getwebproxy <networkservice>
networksetup -getsecurewebproxy <networkservice>

# 查看网络接口
networksetup -listallnetworkservices

# 设置网络代理:http 和 https
networksetup -setwebproxy <networkservice> <domain> <port number> <authenticated> <username> <password>
networksetup -setsecurewebproxy <networkservice> <domain> <port number> <authenticated> <username> <password>

# 设置网络代理状态
networksetup -setwebproxystate <networkservice> <on off>
networksetup -setsecurewebproxystate <networkservice> <on off>

do it

获取网络接口

1
2
3
4
5
6
7
8
networksetup -listallnetworkservices

An asterisk (*) denotes that a network service is disabled.
Wi-Fi
Thunderbolt Bridge
Thunderbolt Ethernet
iPhone USB
Bluetooth PAN

配置代理信息

1
2
3
4
5
# 建议是在图形界面-网络管理中配置
# 以下是命令行方式

networksetup -setwebproxy 'Thunderbolt Ethernet' 172.16.242.47 3128 username password
networksetup -setsecurewebproxy 'Thunderbolt Ethernet' 172.16.242.47 3128 username password

开启代理

1
2
networksetup -setwebproxystate 'Thunderbolt Ethernet' on
networksetup -setsecurewebproxystate 'Thunderbolt Ethernet' on

查看状态

1
2
3
4
5
6
7
8
9
10
11
12
13
networksetup -getsecurewebproxy 'Thunderbolt Ethernet'

Enabled: Yes
Server: 172.16.242.47
Port: 3128
Authenticated Proxy Enabled: 0

networksetup -getwebproxy 'Thunderbolt Ethernet'

Enabled: Yes
Server: 172.16.242.47
Port: 3128
Authenticated Proxy Enabled: 0

shuttle实现快捷切换

https://github.com/fitztrev/shuttle

.shuttle.json 的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
"_comments": [
"Valid terminals include: 'Terminal.app' or 'iTerm'",
"In the editor value change 'default' to 'nano', 'vi', or another terminal based editor.",
"Hosts will also be read from your ~/.ssh/config or /etc/ssh_config file, if available",
"For more information on how to configure, please see http://fitztrev.github.io/shuttle/"
],
"editor": "default",
"launch_at_login": false,
"terminal": "iTerm.app",
"iTerm_version": "nightly",
"default_theme": "Homebrew",
"open_in": "new",
"show_ssh_config_hosts": false,
"ssh_config_ignore_hosts": [],
"ssh_config_ignore_keywords": [],
"hosts": [
{
"Proxy": [
{
"cmd": "networksetup -setwebproxystate 'Thunderbolt Ethernet' on && networksetup -setsecurewebproxystate 'Thunderbolt Ethernet' on",
"name": "开启代理"
},
{
"cmd": "networksetup -setwebproxystate 'Thunderbolt Ethernet' off && networksetup -setsecurewebproxystate 'Thunderbolt Ethernet' off",
"name": "关闭代理"
},
{
"cmd": "networksetup -getwebproxy 'Thunderbolt Ethernet' && networksetup -getsecurewebproxy 'Thunderbolt Ethernet'",
"name": "获取当前状态"
}
]
}
]
}