From 2317caf74e0090eee0c76d01eb74599a4ff8dfd9 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Tue, 14 Mar 2023 14:13:16 -0400 Subject: [PATCH] test --- README.md | 1 + claimpms.sh | 246 ++++++++++++++++++++++++++++++++++++++++++++++++ plex-freshen.sh | 2 +- plex-sqlite.sh | 15 +++ plex-token.sh | 2 +- plex-tools.sh | 6 +- 6 files changed, 268 insertions(+), 4 deletions(-) create mode 100644 claimpms.sh create mode 100644 plex-sqlite.sh diff --git a/README.md b/README.md index fb58a42..729e319 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,4 @@ Credits: ChuckPa: for the database management utility, as well as a function for easily getting host information jlim0930: for token reading from Preferences.xml and some logic for cleaning up libraries and refreshing metadata Werner Beroux: For getting token from api +dane22: claimpms script diff --git a/claimpms.sh b/claimpms.sh new file mode 100644 index 0000000..52decc4 --- /dev/null +++ b/claimpms.sh @@ -0,0 +1,246 @@ +#!/bin/sh +#************************************************************************ +# Script to claim a Plex Media Server +# Will prompt for: +# * plex.tv username +# * plex.tv password +# * IP Address of your unclaimed Plex Media Server +# +# Made by dane22, a Plex community member +# Mark Walker/ZiGGiMoN, a Plex hobbyist +# +# Version: 1.1.0.0 +# +# Home: https://github.com/ukdtom/ClaimIt +#************************************************************************ + +#************************************************************************ +# Functions +#************************************************************************ + +function ComparePwd() +# Compare password entered +{ + if [ $passvar == $passvar2 ] + then + return 0 + else + echo "Password mismatch" + exit 1 # terminate and indicate error + fi +} + +function ValidateIP() +# Check if IP has valid format +{ + if [ "`echo $ippms | awk -F'.' 'NF==4 && $1 > 0 && $1<256 && $2<256 && $3<256 && $4<256 && !/\.\./'`" == "$ippms" ] + then + if echo "$ippms" | grep -Eq '(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)' >/dev/null; then + return 0 + else + echo "The IP address entered is not in Private Address Space" + echo "Either '127.0.0.1' or an address in private address space is needed to claim a server" + echo "See: https://github.com/ukdtom/ClaimIt/wiki/IP-Address-requirement" + exit 1 + fi + return 0 + else + echo "IP is not valid" + exit 1 # terminate and indicate error + fi +} + +function GetClaimToken() +#***************************************** +#* Get claim token from plex.tv +#* Needs the following params: +#* 1 Param: X-Plex-Token +#* 2 Param: X-Plex-Client-Identifier +#***************************************** +{ + url="https://plex.tv/api/claim/token?X-Plex-Token=$1&X-Plex-Client-Identifier=ClaimIt-$2" + local response=$(curl -i -k -L -s $url) + # Grap the token + local claimtoken=$(printf %s "$response" | awk -F= '$1=="token"{print $2}' RS=' '| cut -d '"' -f 2) + # grap the return code + local http_status=$(echo "$response" | grep HTTP | awk '{print $2}') + if [ -z "$http_status" ]; + then + exit 1 + else + if [ $http_status -eq "200" ] + then + echo "$claimtoken" + exit 0 + else + exit 1 + fi + fi +} + +function GetLoginToken() +#***************************************** +#* Get user auth token from plex.tv +#* Needs the following params: +#* 1 Param: UserName +#* 2 Param: Password +#* 3 Param: X-Plex-Client-Identifier +#***************************************** +{ + url="https://plex.tv/api/v2/users/signin" + local response=$(curl -X POST -i -k -L -s $url --data-urlencode "login=$1" --data-urlencode "password=$2" --data-urlencode "X-Plex-Client-Identifier=ClaimIt-$3") + # Grap the token + local UserToken=$(printf %s "$response" | awk -F= '$1=="authToken"{print $2}' RS=' '| cut -d '"' -f 2) + # grap the return code + local http_status=$(echo "$response" | grep HTTP | awk '{print $2}') + if [ -z "$http_status" ]; + then + exit 1 + else + if [ "$http_status" -lt "202" ] + then + echo "$UserToken" + else + exit 1 + fi + fi +} + +function GetClientIdentifier() +# Get PMS machineIdentifier +{ + url="http://$ippms:32400/identity" + content=$(curl -i -k -L -s $url) + local machineIdentifier=$(printf %s "$content" | awk -F= '$1=="machineIdentifier"{print $2}' RS=' '| cut -d '"' -f 2) + local http_status=$(echo "$content" | grep HTTP | awk '{print $2}') + if [ -z "$http_status" ]; + then + exit 1 + else + if [ $http_status -eq "200" ] + then + if [ -z "$machineIdentifier" ]; + then + exit 1 + else + echo "$machineIdentifier" + fi + else + exit 1 + fi + fi +} + +function Claimit() +#***************************************** +#* Claim a PMS Server +#* Needs the following params: +#* 1 Param: $ippms +#* 2 Param: $XPlexClaimtoken +#* 3 Param: $XPlexClientIdentifier +#* 4 Param: $UserToken +#***************************************** +{ + url="http://$1:32400/myplex/claim?token=$2&X-Plex-Client-Identifier=ClaimIt-$3&X-Plex-Token=$4" + content=$(curl -X POST -i -k -L -s $url) + local http_status=$(echo "$content" | grep HTTP | awk '{print $2}') + if [ -z "$http_status" ]; + then + echo "Fatal error happened" + echo "HTTP returned Status $http_status" + exit 1 + else + if [ $http_status -eq "200" ]; + then + echo "Claiming server ok $http_status" + echo "" + echo "" + echo "Please close your browser, reopen, and browse to http://$1:32400/web" + exit 0 + else + echo "Fatal error happened" + echo "HTTP returned Status $http_status" + exit 1 + fi + fi +} + +#************************************************************************ +#* Main +#************************************************************************ +clear + +echo "************************************************************************" +echo "* Script to claim a Plex Media Server" +echo "* Will prompt for" +echo "* * plex.tv username" +echo "* * plex.tv password" +echo "* * IP Address of your unclaimed Plex Media Server" +echo "*" +echo "*" +echo "* Made by dane22, a Plex community member" +echo "* And Mark Walker/ZiGGiMoN, a Plex hobbyist" +echo "*" +echo "* Version 1.1.0.0" +echo "*" +echo "* To see the manual, please visit https://github.com/ukdtom/ClaimIt/wiki" +echo "************************************************************************" + +read -p 'plex.tv Username: ' uservar +echo '' +read -sp 'plex.tv Password: ' passvar +echo '' +read -sp 'plex.tv Password Repeated: ' passvar2 +echo '' +read -p 'IP Address of PMS server: ' ippms + +# Compare pwd entered +echo "Comparing entered passwords" +ComparePwd +echo "Comparing entered passwords ok" + +echo "Validating IP address" +if ! CheckIPValidity=$(ValidateIP); +then + echo "******** ERROR ********" + echo "The IP address entered is not in Private Address Space" + echo "Either '127.0.0.1' or an address in private address space is needed to claim a server" + echo "See: https://github.com/ukdtom/ClaimIt/wiki/IP-Address-requirement" + exit 1 +fi + +echo "Getting PMS Server Identifier" +if ! XPlexClientIdentifier=$(GetClientIdentifier); +then + echo "******** ERROR ********" + echo "We failed to get the Plex Media Server ID" + echo "Please check server is up and running, as well as the IP address entered" + exit 1 +fi +echo "Getting PMS Server Identifier ok" + +# Get UserToken from plex.tv +echo "Getting User Token from plex.tv" +if ! UserToken=$(GetLoginToken "$uservar" "$passvar" "$XPlexClientIdentifier"); +then + echo "******** ERROR ********" + echo "We failed to authenticate towards plex.tv" + echo "Please check username and password, as well as network access" + exit 1 +fi +echo "Getting User Token from plex.tv ok" + +# Get the Claim token +echo "Getting PMS Claim Token" +if ! XPlexClaimtoken=$(GetClaimToken "$UserToken" "$XPlexClientIdentifier"); +then + echo "******** ERROR ********" + echo "We failed to get a claim token from plex.tv" + echo "Please check username and password, as well as network access" + exit 1 +fi +echo "Getting PMS Claim Token ok" + +# Claiming server +echo "Claiming server" +Claimit "$ippms" "$XPlexClaimtoken" "$XPlexClientIdentifier" "$UserToken" diff --git a/plex-freshen.sh b/plex-freshen.sh index 4c33b33..32b2664 100644 --- a/plex-freshen.sh +++ b/plex-freshen.sh @@ -32,7 +32,7 @@ update_from_api() { curl -X PUT http://127.0.0.1:32400/library/clean/bundles?X-Plex-Token=${LOCAL_PLEX_TOKEN} } -case "$2" in +case "$1" in cli) update_from_cli;; ""|api) update_from_api;; *) echo "'$1' is not a valid plex-tools freshen command";; diff --git a/plex-sqlite.sh b/plex-sqlite.sh new file mode 100644 index 0000000..bf76b1a --- /dev/null +++ b/plex-sqlite.sh @@ -0,0 +1,15 @@ +#!/bin/bash + + +PLEX_SQLITE_RUN() { + # ChuckPa already wrote a great function for getting host info + source ./HostConfig.sh + + echo "Getting Host Information" + HostConfig + + PLEX_SQLITE ${@} + fi +} + +PLEX_SQLITE_RUN diff --git a/plex-token.sh b/plex-token.sh index 1cac2fa..42edd27 100644 --- a/plex-token.sh +++ b/plex-token.sh @@ -55,7 +55,7 @@ token_from_api() { echo $X_PLEX_TOKEN } -case "$2" in +case "$1" in api) token_from_api;; ""|xml) token_from_Pref_XML;; *) echo "'$1' is not a valid plex-tools token command";; diff --git a/plex-tools.sh b/plex-tools.sh index f9c105f..c5dafcb 100644 --- a/plex-tools.sh +++ b/plex-tools.sh @@ -3,8 +3,10 @@ echo "Welcome to plex-tools wrapper utility!" case "$1" in - token) source ./plex-token.sh;; - freshen) source ./plex-freshen.sh;; + token) source ./plex-token.sh ${@:2};; + freshen) source ./plex-freshen.sh ${@:2};; + claim) source ./claimpms.sh;; + sqlite) source ./plex-sqlite.sh;; dbrepair) source ./DBRepair.sh stop auto start exit;; start) source ./DBRepair.sh start exit;; stop) source ./DBRepair.sh stop exit;;