Skip to content

Instantly share code, notes, and snippets.

@austinsonger
Last active February 27, 2025 23:42
Show Gist options
  • Save austinsonger/752b1242fa766bbbb0d909bcdb577092 to your computer and use it in GitHub Desktop.
Save austinsonger/752b1242fa766bbbb0d909bcdb577092 to your computer and use it in GitHub Desktop.
Plex Reclaim Server
#!/bin/bash
prefFile="$(readlink -f "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Preferences.xml")"
echo "Please go to https://plex.tv/claim in order to obtain a new claim-code, then type it below"
echo -n "Claim Code: "
read PLEX_CLAIM
clientId=$(sed -n 's/.*ProcessedMachineIdentifier="\(\S*\)".*/\1/p' "${prefFile}")
loginInfo="$(curl -X POST \
-H "X-Plex-Client-Identifier: ${clientId}" \
-H "X-Plex-Product: Plex Media Server" \
-H "X-Plex-Version: 1.1" \
-H "X-Plex-Provides: server" \
-H "X-Plex-Platform: Linux" \
-H "X-Plex-Platform-Version: 1.0" \
-H "X-Plex-Device-Name: PlexMediaServer" \
-H "X-Plex-Device: Linux" \
"https://plex.tv/api/claim/exchange?token=${PLEX_CLAIM}")"
token=$(echo "$loginInfo" | sed -n 's/.*<authentication-token>\(.*\)<\/authentication-token>.*/\1/p')
if [ "$token" ]; then
echo "Token obtained successfully"
sed -i "s|PlexOnlineToken=\"[^\"]*\"|PlexOnlineToken=\"${token}\"|" "${prefFile}"
echo "Plex claim token installed successfully."
# Restart Plex Media Server
echo "Restarting Plex Media Server..."
sudo systemctl restart plexmediaserver
if [ $? -eq 0 ]; then
echo "Plex Media Server restarted successfully."
else
echo "Failed to restart Plex Media Server."
fi
else
echo "Failed to obtain token. Please check the claim code and try again."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment