-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
update-plex-ipv6-access-url.sh
executable file
·61 lines (54 loc) · 2.28 KB
/
update-plex-ipv6-access-url.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
60
61
#!/bin/bash
# Retrieve host IPv6 address and update Plex custom access url accordingly
# based on workaround posted by Pikey18 on the Plex subreddit: https://www.reddit.com/r/PleX/comments/b82opu/plex_remote_access_over_ipv6/
# IPv6 address expansion (https://stackoverflow.com/a/50208987)
# helper to convert hex to dec (portable version)
hex2dec(){
[ "$1" != "" ] && printf "%d" "$(( 0x$1 ))"
}
# expand an ipv6 address
expand_ipv6() {
ip=$1
# prepend 0 if we start with :
echo $ip | grep -qs "^:" && ip="0${ip}"
# expand ::
if echo $ip | grep -qs "::"; then
colons=$(echo $ip | sed 's/[^:]//g')
missing=$(echo ":::::::::" | sed "s/$colons//")
expanded=$(echo $missing | sed 's/:/:0/g')
ip=$(echo $ip | sed "s/::/$expanded/")
fi
blocks=$(echo $ip | grep -o "[0-9a-f]\+")
set $blocks
printf "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n" \
$(hex2dec $1) \
$(hex2dec $2) \
$(hex2dec $3) \
$(hex2dec $4) \
$(hex2dec $5) \
$(hex2dec $6) \
$(hex2dec $7) \
$(hex2dec $8)
}
# Get IPv6 address of given interface (command adapted from: https://superuser.com/a/1057290)
IPv6=`/sbin/ip -6 -o addr show dev "$1" scope global | grep inet6 | grep -v deprecated | grep -v temporary | awk -F '[ \t]+|/' '{print $4}'`
if [ -n "$IPv6" ]; then
echo "Got IPv6 address: $IPv6"
# Format IPv6 for Plex (replace : with -)
PlexFormatIPv6=`expand_ipv6 "$IPv6" | sed -e "s/:/-/g"`
if ! grep -q "customConnections\=\"https\:\/\/$PlexFormatIPv6" "$2" ; then
echo "Current IPv6 does not match config, updating config"
# Replace old IPv6 with new one
sed -i -e "s/customConnections\=\"https\:\/\/[a-fA-F0-9\-]*/customConnections\=\"https:\/\/$PlexFormatIPv6/" "$2"
# Restart Plex service, uncomment line for your server's os or add your own
# systemctl restart plexmediaserver # systemd Linux distributions (Ubuntu, Debian, ...)
# synoservice --restart pkgctl-Plex\ Media\ Server # Synology DiskStations (details: https://tech.setepontos.com/2018/03/25/control-synology-dsm-services-via-terminal-ssh/)
# synopkg start PlexMediaServer # Synology NAS DSM 7 with the official package
else
echo "Current IPv6 matches config, exiting"
fi
exit 0
else
echo "No IPv6 address found, exiting"
exit 1
fi