Skip to content

Commit 0024090

Browse files
committedAug 16, 2024
[script] Tailscale Switch: Make it switch account without logout
Update script tailscale-switch.sh to switch to another Tailscale account without logging out of the current account. With two logged-in Tailscale accounts, the updated script allows switching between the two accounts without any re-authentication (as far as tested for the duration of ~17 hours after login to my accounts). The script uses `tailscale switch --list` to list the available accounts. It takes the first not-connected account, and switches to this account with `tailscale switch ID`. ```console $ tailscale switch --list ID Tailnet Account e163 tailnet-a account-a@tailnet-a.example 2b13 tailnet-b account-b@tailnet-b.example ``` ```console $ tailscale switch 2b13 Switching to account "2b13" Success. ```
1 parent c50cc84 commit 0024090

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed
 

‎commands/apps/tailscale/tailscale-switch.sh

+8-9
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ else
2929
exit 1
3030
fi
3131

32-
$ts logout
33-
$ts up
34-
35-
if command -v jq &> /dev/null; then
36-
account=$($ts status --json | jq -r '.User[(.Self.UserID | tostring)].LoginName')
37-
echo "Connected as $account"
38-
else
39-
echo "Connected"
40-
fi
32+
"${ts}" switch --list | # List all Tailscale accounts "<ID> <Tailnet name> <User account name>"
33+
grep -vF -e 'ID' -e '*' | # Omit the header line and the current account marked with "*"
34+
awk '{ print $1 }' | # Print only the account ID of not-connected accounts
35+
head -n1 | # Print only the first not-connected account
36+
xargs -r -n1 "${ts}" switch # Switch to the selected account
37+
38+
tailnet="$("${ts}" switch --list | awk '/\*/ { print $2 }')"
39+
echo "Switched to ${tailnet}"

0 commit comments

Comments
 (0)
Please sign in to comment.