Not a biggie but just posting here in case it’s of use to someone else.
For a couple of machines I SSH into, I have them always connected to a Tailscale exit node. If I am not connected for whatever reason, I’d like to be aware of that; and since I run tmux in all my SSH sessions, I figured I could use the tmux status bar to show me the situation.
Similarly, the exit node runs in Azure and I shut them down automatically in the evening to save costs. If I happen to login at night, or when I login in the morning, I’d like to know they are offline so I start them first.
Here’s what I did. First, I created a script, let’s call it tmux.sh
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#!/usr/bin/env bash # Need this so the alias is recognized in the script shopt -s expand_aliases if [[ $(uname) == "Darwin" ]]; then alias tailscale="/Applications/Tailscale.app/Contents/MacOS/Tailscale" fi # is the node offline? if `tailscale status | egrep -q <replace>.*offline`; then echo ' #[fg=colour229,bg=colour196] offline '; else # it is, so check if we are connected if `tailscale exit-node list | grep -q '<replace>.*selected'`; then echo ' #[fg=colour40,bg=default] connected '; else echo ' #[fg=colour160,bg=default] not connected '; fi; fi |
Replace the bit marked <replace>
with the DNS suffix from you tailnet. For example, if it’s called “modest-einstein.ts.net”, put “modest-einstein”. You could put the whole DNS suffix or just a host name too, doesn’t matter. Basically we do a regex match for that name in the output of the tailscale
command.
In my case I put the DNS suffix in case I have more exit nodes from that tailnet at some point.
This script simply outputs the text “offline”, “connected”, or “not connected” along wih some tmux colour codes so it is highlighted differently. In the case of offline I want the background to stand out, so I tweak that.
The script uses the tailscale status
command to identify if the node is offline or not. And if it’s online, it uses the tailcale exit-node list
command to identify if we are connected to it or not.
Use the following snippet to change the colours if needed (thanks to this StackOverflow post):
1 |
for i in {0..255}; do printf '\x1b[38;5;%dmcolour%d\x1b[0m\n' $i $i; done |
In my .tmux.conf
, I then have a line like this:
1 |
set-option -g status-right " %H:%M #(~/path/to/tmux.sh)" |
This shows the time followed by the output of the script. You could use the following too if you don’t care about the time.
1 |
set-option -g status-right " #(~/path/to/tmux.sh)" |
That’s all. Quite simple!
Right now the node is offline so my tmux status bar looks like this:
Nice!
Not sure if I said so recently, but I do ❤️ Tailscale. 😊
Update (17th August 2024): Slight change to the alias section of the script. It wasn’t working on macOS.