├── LICENSE.md ├── README.md └── UserCredentialReset.sh /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2022, ChuckPa. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to use the Software solely for personal use only. The Software may not be modified, merged, published, distributed, sublicensed, and/or used in other form without the express written consent of the author. 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # User Credential Reset 3 | 4 | 5 | [![GitHub issues](https://img.shields.io/github/issues/ChuckPa/UserCredentialReset.svg?style=flat)](https://github.com/ChuckPa/UserCredentialReset/issues) 6 | [![Release](https://img.shields.io/github/release/ChuckPa/UserCredentialReset.svg?style=flat)](https://github.com/ChuckPa/UserCredentialReset/releases/latest) 7 | [![Download latest release](https://img.shields.io/github/downloads/ChuckPa/UserCredentialReset/latest/total.svg)](https://github.com/ChuckPa/UserCredentialReset/releases/latest) 8 | [![Download total](https://img.shields.io/github/downloads/ChuckPa/UserCredentialReset/total.svg)](https://github.com/ChuckPa/UserCredentialReset/releases) 9 | [![master](https://img.shields.io/badge/master-stable-green.svg?maxAge=2592000)]('') 10 | ![Maintenance](https://img.shields.io/badge/Maintained-Yes-green.svg) 11 | 12 | User Credential Reset and Server Reclaim Utility 13 | - for Plex Media Server 14 | 15 | 16 | # Introduction 17 | 18 | This utility script assists and semi-automates recovery of your Plex Media Server's credentials and account binding (claiming) 19 | after a hard password reset or account change. 20 | 21 | It runs on the Linux command line with 'root' privilege level so it may edit Plex's files 22 | 23 | 24 | # Currently supported platforms (more can be added) 25 | 26 | 1. Linux (workstation & server) 27 | 2. Synology (DSM 6 and DSM 7) 28 | 3. QNAP (QTS and QuTS) 29 | 4. ASUSTOR 30 | 5. Netgear ReadyNAS 31 | 6. Western Digital (OS5 models – PR,DL, and Ultra) 32 | 7. Non-standard Linux installations, including Docker, when path to Preferences.xml is known. 33 | 8. Linux Snap *restrictions apply 34 | 9. Plex,inc and LinuxServerio docker images (HOTIO and BinHex partially supported) 35 | 36 | # Prerequisites 37 | 38 | 1. A Plex Media Server account at https://app.plex.tv 39 | 2. A valid server Preferences.xml containing the MachineID and ProcessedMachineID fields. 40 | 3. 'curl' (C-url) utility for your host. 41 | 4. 'tar' or 'zip' to extract the utility script from the protective wrapper. 42 | 5. Plex Media Server stopped. 43 | 44 | # Where to place the utility's tar file (Recommendations) 45 | ``` 46 | Vendor | Shared folder name | directory 47 | -------------------+---------------------+------------------------------------------ 48 | ASUSTOR | Public | /volume1/Public 49 | Netgear (ReadyNAS) | "your_choice" | "/data/your_choice" 50 | Synology (DSM 6) | Plex | /volume1/Plex (change volume as required) 51 | Synology (DSM 7) | PlexMediaServer | /volume1/PlexMediaServer (change volume as required) 52 | QNAP (QTS/QuTS) | Public | /share/Public 53 | Western Digital | Public | /mnt/HD/HD_a2/Public (Does not support 'MyCloudHome' series) 54 | Docker | N/A | N/A 55 | Linux (wkstn/svr) | N/A | N/A 56 | Linux (SNAP) | N/A | N/A 57 | ``` 58 | 59 | (recommend using Public shared folder) 60 | Manual Path specification (which includes containers and custom) (See below) 61 | 62 | # Restrictions 63 | 64 | SNAP packages, due to their non-integration with the package managers (RPM & DPKG), can find themselves installed 65 | on a system where another PMS instance already exists. 66 | 67 | In consideration of this, presence of a SNAP package is only tested *after* the mainstream package has been confirmed not to exist. 68 | If both DPKG/RPM package and a snap package are installed concurrently, the DPKG/RPM instance will be given priority in all cases. 69 | 70 | # Download and extraction 71 | 72 | 1. If you have a Linux or MacOS computer, the shell script `UserCredentialReset.sh` can be downloaded directly. 73 | 2. If you have a Windows computer, you must be very careful not to damage the `.sh` file because Windows 'newline' character is different than Linux. 74 | 3. If you want the `tar.gz` (compressed tar) or `zip` file, download appropriately. 75 | 4. Extraction is either: 76 | `tar xf UserCredentialReset.tar.gz` 77 | -or- 78 | `unzip UserCredentialReset.zip` 79 | 80 | 5. In both cases, you'll end up with subdirectory `UserCredentialReset-main`. 81 | 6. You will find `UserCredentialReset.sh` there. 82 | 83 | 84 | 85 | 86 | 87 | # How to use this tool. 88 | 89 | ### Installing the script 90 | 91 | 1. Place the tar file, or just the script, (upload) on the host. (See recommended locations above) 92 | 93 | 2. Open a terminal window or SSH session to the host and sign in. 94 | (Windows users can use Putty utility) 95 | 96 | 3. Elevate command line privileges to 'root' (`sudo sh`) 97 | 98 | 4. Extract the utility from the tar file (if appropriate) 99 | ``` 100 | cd DIRECTORY_FROM_ABOVE 101 | tar xf ./UserCredentialReset.tar.gz 102 | ls UserCredentialReset.* 103 | ``` 104 | You will see file `UserCredentialReset.sh` 105 | 106 | 5. Make it executable 107 | ``` 108 | chmod +x UserCredentialReset.sh 109 | ``` 110 | 111 | ### This script can detect most configurations and therefore stop/start PMS automatically as part of the claiming process. If it can stop & start plex for you, it will do so. If it can't then it will make certain PMS is stopped before proceeding. 112 | 113 | 114 | 6. Invoke the utility `./UserCredentialReset.sh` 115 | -- Confirms the host platform type (so it knows what to do) 116 | -- Confirm you have sufficient user privilege and PMS is stopped 117 | -- Prompts for a "Plex Claim Token" (unless provided on the invocation command line) 118 | 119 | 7. Open a browser tab to: https://plex.tv/claim 120 | 121 | 8. COPY the given token 122 | 123 | 9. WITHIN the next 4 Minutes, PASTE the token on the utility's command line 124 | (The token expires so we need be quick) 125 | 126 | 10. Hit Enter and it will immediately complete the task. (5-20'ish seconds) 127 | -- Stops PMS (if supported for your configuration) 128 | -- Obtains new credentials for your server 129 | -- Writes new credentials in Preferences.xml 130 | -- Starts PMS (if supported for your configuration) 131 | 132 | 11. It will print out your Plex username and email (confirmation which account PMS is claimed). 133 | 134 | 12. Utility exits. 135 | 136 | 13. Start PMS (if needed). It will be back to normal. 137 | 138 | 139 | **PLEASE** don't hesitate to ask if questions or issues. 140 | 141 | 142 | # Special Usage cases 143 | 144 | If your PMS configuration is not using one of the above predefined configurations, 145 | You may specify the path to the Preferences.xml file using the `-p` option. 146 | 147 | Using `-p "/path/to/Preferences.xml"` bypasses host type checking. 148 | 149 | However, it does confirm PMS is stopped and sufficient "root/admin" privilege is active. 150 | 151 | Example: 152 | ``` 153 | [/tmp] # ./UserCredentialReset.sh -p "/mnt/docker/Plex/Library/Application Support/Plex Media Server/Preferences.xml" 154 | ``` 155 | 156 | 157 | # How it looks in use (as seen on a QNAP) 158 | 159 | ``` 160 | [~] # chmod +x UserCredentialReset.sh 161 | [~] # ./UserCredentialReset.sh claim-TE89fVWHtVjqXBPgG8z5 162 | 163 | Plex Media Server user credential reset and reclaim tool (QNAP) 164 | 165 | 166 | This utility will reset the server's credentials. 167 | It will next reclaim the server for you using a Plex Claim token you provide from https://plex.tv/claim 168 | 169 | Using given claim token: 'claim-TE89fVWHtVjqXBPgG8z5' 170 | 171 | Stopping PMS 172 | Clearing Preferences.xml 173 | Getting new credentials from Plex.tv 174 | Claim completed without errors. 175 | Username: ChuckPA 176 | Email: ChuckIsCrazy@loonies.com 177 | 178 | Starting PMS 179 | Complete. 180 | [~] # 181 | ``` 182 | 183 | ## Conclusion 184 | 185 | When you start PMS, it will already be claimed for you. 186 | It will never know it was reset. 187 | -------------------------------------------------------------------------------- /UserCredentialReset.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Plex credential reset and claim for Plex Media Server 4 | # 5 | # Author: ChuckPa 6 | # Version: 1.0.9.0 7 | # 8 | # Set Prefs 9 | SetPref() 10 | { 11 | # Add ONLY if it doesn't already exist 12 | if ! grep "$1" "$Preferences" 1>/dev/null 2>/dev/null ; then 13 | sed -i "s;/>; $1=\""$2"\"/>;" "$Preferences" 14 | fi 15 | } 16 | 17 | # Determine which host we are running on and set variables 18 | HostConfig() { 19 | 20 | #### NOT YET 21 | # # Docker 22 | # if [ -d "/config/Library/Application Support/Plex Media Server" ]; then 23 | # AppSuppDir="/config/Library/Application Support" 24 | # Preferences="$AppSuppDir/Plex Media Server/Preferences.xml" 25 | # HostType="Docker Container" 26 | # return 0 27 | ###### 28 | # ASUSTOR 29 | if [ -f /etc/nas.conf ] && grep ASUSTOR /etc/nas.conf >/dev/null && \ 30 | [ -d "/volume1/Plex/Library/Plex Media Server" ]; then 31 | 32 | # Where are things 33 | AppSuppDir="/volume1/Plex/Library" 34 | Preferences="$AppSuppDir/Plex Media Server/Preferences.xml" 35 | HostType="ASUSTOR" 36 | return 0 37 | 38 | # Synology (DSM 7) 39 | elif [ -d /var/packages/PlexMediaServer ] && \ 40 | [ -d "/var/packages/PlexMediaServer/shares/PlexMediaServer/AppData/Plex Media Server" ]; then 41 | 42 | # Where is the data 43 | AppSuppDir="/var/packages/PlexMediaServer/shares/PlexMediaServer/AppData" 44 | Preferences="$AppSuppDir/Plex Media Server/Preferences.xml" 45 | HostType="Synology (DSM 7)" 46 | StartCommand="/usr/syno/bin/synopkg start plexmediaserver" 47 | StopCommand="/usr/syno/bin/synopkg stop plexmediaserver" 48 | return 0 49 | 50 | # Synology (DSM 6) 51 | elif [ -d "/var/packages/Plex Media Server" ] && \ 52 | [ -f "/usr/syno/sbin/synoshare" ]; then 53 | 54 | # Get shared folder path 55 | PlexShare="$(synoshare --get Plex | grep Path | awk -F\[ '{print $2}' | awk -F\] '{print $1}')" 56 | 57 | # Where is the data 58 | AppSuppDir="$PlexShare/Library/Application Support" 59 | Preferences="$AppSuppDir/Plex Media Server/Preferences.xml" 60 | HostType="Synology (DSM 6)" 61 | StopCommand="synopkg stop 'PlexMediaServer'" 62 | StartCommand="synopkg start 'PlexMediaServer'" 63 | return 0 64 | 65 | # QNAP (QTS & QuTS) 66 | elif [ -f /etc/config/qpkg.conf ]; then 67 | 68 | # Where is the software 69 | PKGDIR="$(getcfg -f /etc/config/qpkg.conf PlexMediaServer Install_path)" 70 | 71 | # Where is the data 72 | AppSuppDir="$PKGDIR/Library" 73 | Preferences="$AppSuppDir/Plex Media Server/Preferences.xml" 74 | HostType="QNAP" 75 | StartCommand="/etc/init.d/plex.sh start" 76 | StopCommand="/etc/init.d/plex.sh stop" 77 | return 0 78 | 79 | # Standard configuration Linux host 80 | elif [ -f /etc/os-release ] && \ 81 | [ -d /usr/lib/plexmediaserver ] && \ 82 | [ -d /var/lib/plexmediaserver ]; then 83 | 84 | # Where is the data 85 | AppSuppDir="/var/lib/plexmediaserver/Library/Application Support" 86 | 87 | # Find the metadata dir if customized 88 | if [ -e /etc/systemd/system/plexmediaserver.service.d ]; then 89 | 90 | # Glob up all 'conf files' found 91 | NewSuppDir="$(cd /etc/systemd/system/plexmediaserver.service.d ; \ 92 | cat override.conf local.conf *.conf 2>/dev/null | grep "APPLICATION_SUPPORT_DIR" | head -1)" 93 | 94 | if [ "$NewSuppDir" != "" ]; then 95 | NewSuppDir="$(echo $NewSuppDir | sed -e 's/[^.]*SUPPORT_DIR=//' | tr -d \")" 96 | if [ -d "$NewSuppDir" ]; then 97 | AppSuppDir="$NewSuppDir" 98 | else 99 | echo "Given application support directory override specified does not exist: '$NewSuppDir'". Ignoring. 100 | fi 101 | fi 102 | fi 103 | 104 | Preferences="$AppSuppDir/Plex Media Server/Preferences.xml" 105 | HostType="$(grep ^PRETTY_NAME= /etc/os-release | sed -e 's/PRETTY_NAME=//' | sed -e 's/"//g')" 106 | StartCommand="systemctl start plexmediaserver" 107 | StopCommand="systemctl stop plexmediaserver" 108 | return 0 109 | 110 | # Netgear ReadyNAS 111 | elif [ -e /etc/os-release ] && [ "$(cat /etc/os-release | grep ReadyNASOS)" != "" ]; then 112 | 113 | # Find PMS 114 | if [ "$(echo /apps/plexmediaserver*)" != "/apps/plexmediaserver*" ]; then 115 | 116 | PKGDIR="$(echo /apps/plexmediaserver*)" 117 | 118 | # Where is the code 119 | AppSuppDir="$PKGDIR/MediaLibrary" 120 | Preferences="$AppSuppDir/Plex Media Server/Preferences.xml" 121 | HostType="Netgear ReadyNAS" 122 | StartCommand="systemctl start fvapp-plexmediaserver" 123 | StopCommand="systemctl stop fvapp-plexmediaserver" 124 | return 0 125 | fi 126 | 127 | # Western Digital (watch for semi-broken configurations with multiple drives) 128 | elif [ -f /etc/system.conf ] && grep "Western Digital Corp" /etc/system.conf >/dev/null; then 129 | 130 | AppSuppDir="$(echo /mnt/HD/HD*/Nas_Prog/plex_conf)" 131 | for i in $AppSuppDir 132 | do 133 | if [ -f "$i/Plex Media Server/Preferences.xml" ];then 134 | AppSuppDir="$i" 135 | Preferences="$i/Plex Media Server/Preferences.xml" 136 | HostType="Western Digital" 137 | return 0 138 | fi 139 | done 140 | echo "ERROR: Host is Western Digital but Preferences.xml not found." 141 | 142 | # look for SNAP (low usage) 143 | elif [ -f "/snap/plexmediaserver/current/Plex Media Server" ] && \ 144 | [ -f "/var/snap/plexmediaserver/Library/Application Support/Plex Media Server/Preferences.xml" ]; then 145 | 146 | AppSuppDir="/var/snap/plexmediaserver/Library/Application Support" 147 | Preferences="/var/snap/plexmediaserver/Library/Application Support/Plex Media Server/Preferences.xml" 148 | HostType="Snap (Linux)" 149 | StartCommand="snap start plexmediaserver" 150 | StopCommand="snap stop plexmediaserver" 151 | return 0 152 | 153 | # Containers: 154 | # - Docker cgroup v1 & v2 155 | # - Podman (libpod) 156 | elif [ "$(grep docker /proc/1/cgroup | wc -l)" -gt 0 ] || [ "$(grep 0::/ /proc/1/cgroup)" = "0::/" ] || 157 | [ "$(grep libpod /proc/1/cgroup | wc -l)" -gt 0 ]; then 158 | 159 | # HOTIO Plex image structure is non-standard (contains symlink which breaks detection) 160 | if [ -d "/app/usr/lib/plexmediaserver" ] && [ -d "/config/Plug-in Support" ]; then 161 | 162 | AppSuppDir="/config" 163 | Preferences="$AppSuppDir/Plex Media Server/Preferences.xml" 164 | HostType="HOTIO" 165 | return 0 166 | 167 | # Docker (All main image variants except binhex and hotio) 168 | elif [ -d "/config/Library/Application Support" ]; then 169 | 170 | AppSuppDir="/config/Library/Application Support" 171 | Preferences="$AppSuppDir/Plex Media Server/Preferences.xml" 172 | HostType="Docker" 173 | StopCommand="/plex_service.sh -d" 174 | StartCommand="/plex_service.sh -u" 175 | 176 | return 0 177 | 178 | # BINHEX Plex image 179 | elif [ -d "/config/Plex Media Server" ]; then 180 | 181 | AppSuppDir="/config" 182 | Preferences="$AppSuppDir/Plex Media Server/Preferences.xml" 183 | HostType="BINHEX" 184 | return 0 185 | fi 186 | fi 187 | 188 | # Unknown / currently unsupported host 189 | return 1 190 | } 191 | 192 | ############################################### Begin here #################################################### 193 | 194 | # Initialize 195 | Manual=0 196 | CustomPreferences="" 197 | ClaimToken="" 198 | Preferences="" 199 | StopCommand="" 200 | StartCommand="" 201 | PRINTF="echo -n" 202 | [ -e "/usr/bin/printf" ] && PRINTF="printf %s" 203 | 204 | 205 | # Check username 206 | if [ "$(id -u $(whoami))" -ne 0 ]; then 207 | echo "ERROR: This tool can only be run as the root/admin (or sudo root/admin) user" 208 | exit 1 209 | fi 210 | 211 | # Use any given command line options 212 | while [ "$1" != "" ] 213 | do 214 | 215 | # Manual path to Preferences.xml given 216 | if [ "$1" = "-p" ]; then 217 | 218 | # -p (preferences path) option 219 | if [ -f "$2" ]; then 220 | 221 | if grep ProcessedMachineIdentifier "$2" >/dev/null 2>/dev/null; then 222 | 223 | # Use this path. It appears to be a Preferences.xml file 224 | CustomPreferences="$2" 225 | shift 226 | shift 227 | else 228 | echo "File '$2' does not appear to be a minimally valid Plex Preferences.xml file. Cannot use. Exiting." 229 | exit 1 230 | fi 231 | else 232 | echo "ERROR: Cannot access given Preferences file '$2'." 233 | echo "Exiting." 234 | exit 1 235 | fi 236 | 237 | HostType="User-Defined" 238 | Manual=1 239 | 240 | # User supplied claim token on command line ? 241 | elif [ "$(echo $1 | grep 'claim-')" != "" ]; then 242 | 243 | ClaimToken="$1" 244 | shift 245 | 246 | # Unrecognizd item on command line 247 | else 248 | echo "Error: Unrecognized command line item '$1'- ignored." 249 | shift 250 | fi 251 | done 252 | 253 | # Get our config if not manual 254 | if [ "$CustomPreferences" = "" ]; then 255 | 256 | if ! HostConfig; then 257 | echo " " 258 | echo "Unrecognized host type. Cannot continue." 259 | exit 1 260 | fi 261 | else 262 | Preferences="$CustomPreferences" 263 | fi 264 | 265 | echo " " 266 | echo " Plex Media Server user credential reset and reclaim tool ($HostType)" 267 | echo " " 268 | echo "This utility will reset the server's credentials." 269 | echo "It will next reclaim the server for you using a Plex Claim token you provide from https://plex.tv/claim" 270 | echo " " 271 | 272 | # Make sure curl exists 273 | if ! command -v curl > /dev/null; then 274 | echo " " 275 | echo "This utility requires the 'curl' command which is not found." 276 | echo "Please install 'curl' or add it to 'path' if already installed." 277 | exit 1 278 | fi 279 | 280 | if [ ! -f "$Preferences" ]; then 281 | echo "ERROR: Cannot find Preferences file at '$Preferences'. Exiting" 282 | exit 1 283 | fi 284 | 285 | # Annotate custom preferences usage 286 | if [ "$CustomPreferences" != "" ]; then 287 | echo "Using given Preferences path: '$CustomPreferences'" 288 | fi 289 | 290 | # Make certain PMS is stoppable or stopped 291 | if [ "$StopCommand" = "" ] || [ $Manual -eq 1 ]; then \ 292 | if [ $(ps -ef | grep 'Plex Media Server' | grep -v Preferences | grep -v grep | wc -l) -gt 0 ]; then 293 | echo "ERROR: PMS is running. Please stop PMS and try again" 294 | exit 1 295 | fi 296 | fi 297 | 298 | # Get owner UID:GID of Preferences.xml (sed mucks with it on some machines) 299 | Owner="$(stat -c '%u:%g' "$Preferences")" 300 | Permissions="$(stat -c '%a' "$Preferences")" 301 | 302 | # Ask for claim token 303 | if [ "$ClaimToken" != "" ]; then 304 | echo "Using given claim token: '$ClaimToken'" 305 | echo " " 306 | else 307 | while [ "$ClaimToken" = "" ] 308 | do 309 | $PRINTF "Please enter Plex Claim Token copied from http://plex.tv/claim : " 310 | read ClaimToken 311 | 312 | if [ "$(echo $ClaimToken | grep '^claim-' )" = "" ]; then 313 | 314 | # Not recognized claim token 315 | echo "Token not recognized. Token should be 'claim-xxxxxx' form" 316 | ClaimToken="" 317 | fi 318 | done 319 | fi 320 | 321 | # Get existing ClientID (ProcessedMachineID) for use below 322 | ClientId="$(cat "$Preferences" | \ 323 | tail -1 | \ 324 | sed -e 's/.*ProcessedMachineIdentifier="//' | \ 325 | sed -e 's/".*//' )" 326 | 327 | # Stop Plex 328 | if [ "$StopCommand" != "" ]; then 329 | echo "Stopping PMS" 330 | $StopCommand 331 | Result=$? 332 | if [ $Result -ne 0 ]; then 333 | echo "Unable to stop Plex. Error code $Result." 334 | echo "Aborting." 335 | exit 1 336 | fi 337 | fi 338 | 339 | # Give 3 seconds to stop 340 | sleep 3 341 | 342 | # Make certain PMS is stopped 343 | if [ $(ps -ef | grep 'Plex Media Server' | grep -v Preferences | grep -v grep | wc -l) -gt 0 ]; then 344 | echo "ERROR: PMS is still running. Please stop PMS and try again" 345 | exit 1 346 | fi 347 | 348 | # Clear Preferences.xml 349 | echo "Clearing Preferences.xml" 350 | sed -i 's/ PlexOnlineToken="[^"]*"//' "$Preferences" 351 | sed -i 's/ PlexOnlineUsername="[^"]*"//' "$Preferences" 352 | sed -i 's/ PlexOnlineMail="[^"]*"//' "$Preferences" 353 | sed -i 's/ PlexOnlineHome="[^"]*"//' "$Preferences" 354 | sed -i 's/ secureConnections="[012]"//' "$Preferences" 355 | sed -i 's/ AcceptedEULA="[01]"//' "$Preferences" 356 | 357 | # Get Credentials 358 | echo "Getting new credentials from Plex.tv" 359 | LoginInfo="$(curl -X POST -s \ 360 | -H "X-Plex-Client-Identifier: ${ClientId}" \ 361 | -H "X-Plex-Product: Plex Media Server"\ 362 | -H "X-Plex-Version: 1.1" \ 363 | -H "X-Plex-Provides: server" \ 364 | -H "X-Plex-Platform: Linux" \ 365 | -H "X-Plex-Platform-Version: $HostType $(uname -r)" \ 366 | -H "X-Plex-Device-Name: PlexMediaServer" \ 367 | -H "X-Plex-Device: $HostType" \ 368 | "https://plex.tv/api/claim/exchange?token=${ClaimToken}")" 369 | 370 | # If errors, redo claim sequence 371 | Result=$? 372 | if [ $Result -ne 0 ]; then 373 | echo "ERROR: Could not get credentials from plex.tv (Error: $Result)" 374 | exit 1 375 | fi 376 | 377 | # Extract values 378 | Username="$(echo "$LoginInfo" | sed -n 's/.*\(.*\)<\/username>.*/\1/p')" 379 | Email="$(echo "$LoginInfo" | sed -n 's/.*\(.*\)<\/email>.*/\1/p')" 380 | Token="$(echo "$LoginInfo" | sed -n 's/.*\(.*\)<\/authentication-token>.*/\1/p')" 381 | 382 | # Make certain we got valid data 383 | if [ "$Username" = "" ] || \ 384 | [ "$Email" = "" ] || \ 385 | [ "$Token" = "" ]; then 386 | 387 | echo Incomplete credentials from Plex.tv 388 | echo " Username: '$Username'" 389 | echo " Email: '$Email'" 390 | echo " Token: '$Token'" 391 | echo "" 392 | echo "Cannot continue" 393 | echo "" 394 | echo "Server credentials are cleared but server has not been reclaimed. Claim manually" 395 | exit 1 396 | 397 | fi 398 | 399 | # Write info to Preferences and continue to start 400 | SetPref PlexOnlineUsername "$Username" 401 | SetPref PlexOnlineMail "$Email" 402 | SetPref PlexOnlineToken "$Token" 403 | SetPref AcceptedEULA "1" 404 | SetPref PublishServerOnPlexOnlineKey "1" 405 | 406 | # We made it 407 | echo "Claim completed without errors." 408 | echo " Username: $Username" 409 | echo " Email: $Email" 410 | echo " " 411 | 412 | # Set the ownership (back) to what it was and guarantee read/write 413 | chown $Owner "$Preferences" 414 | chmod $Permissions "$Preferences" 415 | 416 | # Remove existing certificate and let PMS pull fresh 417 | # (harmless if cert unchanged but required if password change) 418 | CertDir="$(dirname "$Preferences")/Cache" 419 | rm -f "$CertDir"/*.p12 420 | 421 | if [ "$StartCommand" != "" ]; then 422 | echo "Starting PMS" 423 | $StartCommand 424 | echo "Complete." 425 | else 426 | echo "Complete. You may restart PMS." 427 | fi 428 | --------------------------------------------------------------------------------