#!/usr/bin/env bash# Script to easily toggle Tailscale connections, including a prompt for selecting an exit node.## Set TS_EXIT_NODE (an exit node's name or Tailscale IP) to skip typing it each time,# e.g. in ~/.bashrc: export TS_EXIT_NODE="my-exit-node"EXIT_NODE="${TS_EXIT_NODE:-}"if!command-vtailscale>/dev/null2>&1;thenecho"Tailscale is not installed."exit1fi# 'tailscale status' exits non-zero when stopped or logged outiftailscalestatus>/dev/null2>&1;thenecho"Tailscale is currently: ON"echo"---------------------------"read-rp"Do you want to turn Tailscale OFF? (y/N): "response
if[["$response"=~^[Yy]$]];thenecho"Stopping Tailscale..."ifsudotailscaledown;thenecho"Tailscale is now OFF."elseecho"Failed to stop Tailscale.">&2exit1fielseecho"Leaving Tailscale ON."fiexit0fiecho"Tailscale is currently: OFF"echo"---------------------------"read-rp"Do you want to turn Tailscale ON? (y/N): "response
if[[!"$response"=~^[Yy]$]];thenecho"Tailscale remains OFF."exit0fiup_args=()read-rp"Do you want to use an exit node? (y/N): "use_exit
if[["$use_exit"=~^[Yy]$]];thenif[-z"$EXIT_NODE"];thenecho"Available exit nodes:"tailscaleexit-nodelist2>/dev/null||echo" (start Tailscale once without an exit node to list them)"read-rp"Exit node name or IP: "EXIT_NODE
fiif[-n"$EXIT_NODE"];thenup_args=(--exit-node="$EXIT_NODE"--exit-node-allow-lan-access)echo"Starting Tailscale with exit node '$EXIT_NODE'..."fifi[${#up_args[@]}-eq0]&&echo"Starting Tailscale without exit node..."ifsudotailscaleup"${up_args[@]}";thenecho"Tailscale is now ON."elseecho"Failed to start Tailscale.">&2exit1fi