macOS 下使用CLI 启动和停止Wireguard GUI 配置

在macOS 下我一直使用ControlPlane 这个APP 来控制某个环境下的一些系统配置,比如到家就会自动mount 我的NAS 盘,外出就会自动静音诸如此类。长期以来我一直试图用它实现在离家的环境里自动连上家里的WireGuard 来实现内部互通。但是macOS 的WireGuard 图形界面与命令行并不会联动,当你使用wg-quick 命令去启动WireGuard 通道后,图形界面并不会跟随之改变状态。昨天忽然从macOS 的Settings/VPN 页面看到了图形界面的WireGuard 连接后,忽然意识到WireGuard 图形界面其实是调用macOS 的Network Extension 框架在工作。那么接下来就简单了:

启用和中断WireGuard 连接:

/usr/sbin/scutil --nc start "WG连接名"
/usr/sbin/scutil --nc stop "WG连接名"

而对于ControlPlane 来说,由于它只能运行某个命令并且不能带参数,所以需要单独建立2个脚本文件:
start-wg.sh

#!/bin/sh
/usr/sbin/scutil --nc start "WG连接名"

stop-wg.sh

#!/bin/sh
/usr/sbin/scutil --nc stop "WG连接名"

Leave a Reply

Note: Commenter is allowed to use '@User+blank' to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write '@ABC '(exclude ') will automatically send your comment to ABC. Using '@all ' to notify all previous commenters. Be sure that the value of User should exactly match with commenter's name (case sensitive).