LLI Wiki — My Digital Knowledge Base

Toggle a WireGuard tunnel on MikroTik RouterOS

Toggle, check and measure a WireGuard tunnel on RouterOS locally, over SSH and from Android.

This guide shows how to enable, disable and check a WireGuard tunnel on a MikroTik RouterOS router, locally, over SSH, and from an Android phone with Termux.

The examples assume a RouterOS script named VPN-TOGGLE that flips the tunnel state and prints the new state. This script is not shown here. The WireGuard interface is named wg-client.

Toggle the tunnel state (RouterOS)

/system/script/run VPN-TOGGLE

The script prints VPN ON or VPN OFF. Each run flips the state.

Check the state (RouterOS)

:if ([/interface/wireguard/get [find where name="wg-client"] disabled]) do={:put "VPN OFF"} else={:put "VPN ON"}

For more detail:

/interface/wireguard/print detail where name="wg-client"

In this list, R means the tunnel is running and X means it is disabled.

Over SSH

The examples assume an alias mikrotik defined in ~/.ssh/config.

ssh mikrotik '/system/script/run VPN-TOGGLE'
ssh mikrotik ':if ([/interface/wireguard/get [find where name="wg-client"] disabled]) do={:put "VPN OFF"} else={:put "VPN ON"}'

Android button (Termux)

The Termux script is placed in ~/.shortcuts/tasks/ and started from the Termux:Widget widget. It toggles the tunnel and shows the new state in a notification.

#!/data/data/com.termux/files/usr/bin/bash

STATUS=$(ssh -o BatchMode=yes mikrotik '/system/script/run VPN-TOGGLE')

termux-notification \
  --id vpn-toggle \
  --title "VPN" \
  --content "$STATUS"

BatchMode=yes stops SSH from prompting for a password, so key-based authentication must be set up.

Measure traffic

Real-time overall throughput:

/interface/monitor-traffic wg-client

Detailed flows (addresses, ports, protocols):

/tool/torch interface=wg-client

Interface counters:

/interface/print stats detail where name="wg-client"

Over SSH, the -t option is needed for live output:

ssh -t mikrotik '/interface/monitor-traffic wg-client'
ssh -t mikrotik '/tool/torch interface=wg-client'

Press Ctrl+C to quit monitor-traffic or torch.

Notes

Mastodon LLIMastodon SocialFosstodon