-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_tailscale_hosts.sh
executable file
·59 lines (45 loc) · 1.57 KB
/
sync_tailscale_hosts.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
# Define the hosts file
HOSTS_FILE="/etc/hosts"
# Define the start and end markers
START_MARKER="# Tailscale Start"
END_MARKER="# Tailscale End"
# Get the Tailscale status in JSON format
TAILSCALE_STATUS=$(tailscale status --json)
# Parse the Self and Peer Tailscale IPs and DNS hostnames using jq
TAILSCALE_ENTRIES=$(echo "$TAILSCALE_STATUS" | jq -r '
(.Peer[] | "\(.TailscaleIPs[])\t\(.DNSName)") ,
(.Self | "\(.TailscaleIPs[])\t\(.DNSName)")
')
# Remove the tailnet suffix from the DNS names and replace with '-ts' or '-ts6' based on IP type
TAILSCALE_ENTRIES=$(echo "$TAILSCALE_ENTRIES" | awk '
{
if ($1 ~ /^fd7a/) {
sub(/\..*\.ts\.net\.$/, ".ts6", $2);
} else {
sub(/\..*\.ts\.net\.$/, ".ts4", $2);
}
print $1, $2;
}')
# Create a temporary file for the updated hosts file
TEMP_FILE=$(mktemp)
# Copy everything before the start marker into the temp file
awk -v start_marker="$START_MARKER" '{
if ($0 == start_marker) { exit }
print
}' "$HOSTS_FILE" > "$TEMP_FILE"
# Add the start marker
echo "$START_MARKER" >> "$TEMP_FILE"
# Add the Tailscale entries
echo "$TAILSCALE_ENTRIES" >> "$TEMP_FILE"
# Add the end marker
echo "$END_MARKER" >> "$TEMP_FILE"
# Copy everything after the end marker into the temp file
awk -v end_marker="$END_MARKER" 'found { print }
$0 == end_marker { found = 1 }
' "$HOSTS_FILE" >> "$TEMP_FILE"
# Replace the original hosts file with the updated temp file
sudo mv "$TEMP_FILE" "$HOSTS_FILE"
# Ensure proper permissions
sudo chmod 644 "$HOSTS_FILE"
echo "$(date): Hosts file updated with Tailscale IPs."