macOS 上手动启动/关闭 GlobalProtect

GlobalProtect 在 macOS 上默认会开机自启。如果只是偶尔需要 VPN,可以写一个小脚本,平时关闭它,需要时再手动启动。

安装脚本

/usr/local/bin 下新建一个命令:

1
sudo vim /usr/local/bin/globalprotect

写入下面内容:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/zsh

set -e

agent_glob="/Library/LaunchAgents/com.paloaltonetworks.gp.pangp*.plist"
daemon="/Library/LaunchDaemons/com.paloaltonetworks.gp.pangpsd.plist"
daemon_label="com.paloaltonetworks.gp.pangpsd"

usage() {
echo "-l launch"
echo "-k kill"
echo "-as launch and set autostart"
echo "-uas kill and unset autostart"
echo "-ds disable system daemon"
echo "-h help"
}

quit_ui() {
osascript -e 'tell application "GlobalProtect" to quit' 2>/dev/null || true
sleep 1
pkill -x GlobalProtect 2>/dev/null || true
}

unload_daemon() {
sudo launchctl unload -w "${daemon}" 2>/dev/null || true
sudo launchctl disable "system/${daemon_label}"
sudo pkill -x PanGPS 2>/dev/null || true
sudo pkill -x PanGpHip 2>/dev/null || true
}

load_ui() {
launchctl enable "gui/$(id -u)/com.paloaltonetworks.gp.pangpa" 2>/dev/null || true
launchctl enable "gui/$(id -u)/com.paloaltonetworks.gp.pangps" 2>/dev/null || true
launchctl load -w ${~agent_glob} 2>/dev/null || true
}

case "${1:-}" in
-h|--help)
usage
;;
-l)
quit_ui
load_ui
;;
-k)
quit_ui
launchctl unload ${~agent_glob} 2>/dev/null || true
;;
-as)
quit_ui
load_ui
;;
-uas)
quit_ui
launchctl unload -w ${~agent_glob} 2>/dev/null || true
launchctl disable "gui/$(id -u)/com.paloaltonetworks.gp.pangpa" 2>/dev/null || true
launchctl disable "gui/$(id -u)/com.paloaltonetworks.gp.pangps" 2>/dev/null || true
;;
-ds)
unload_daemon
;;
"")
usage
exit 0
;;
*)
usage
exit 1
;;
esac

然后赋予执行权限:

1
sudo chmod +x /usr/local/bin/globalprotect

使用

启动 GlobalProtect:

1
globalprotect -l

关闭 GlobalProtect,并取消开机自启:

1
globalprotect -uas

查看帮助:

1
globalprotect -h

一般只需要用 -l-uas。如果机器上还有 system daemon 残留,可以手动执行一次:

1
globalprotect -ds