So, here in Los Angeles, we’ve been having a gaggle of rolling blackouts. Thankfully, I have a set of UPS’s which prevent any damage being done. However, there’s one thing I’ve been having trouble finding on the forums.
Are there any bash scripts that can be run, in terminal (Mac), that can send a message to users currently connected to my Plex server?
My Cyperpower UPS can run a bash script prior to shutdown. Which is great, because I can receive email alerts from my system, if there’s a power outage. But I’d like to go a step further.
What I’d like to blast out, is an annoying message stating, “Due to a power outage, this PLEX Media Server will shut down in 30 sec. Apologies, for any inconvenience.”
Sample url to send, and nope, not my real access token https://PMS_IP:32400/status/sessions/terminate?sessionId=2W6y3xeptpdoYqBxeEvA&reason=Lost+Power&X-Plex-Client-Identifier=3oqnywstl4gyupfnsd8vrh98&X-Plex-Token=MY_PLEX_TOKEN
I’m hitting a few roadblocks. Easily found the Plex Token, but nothing I try can give a session ID. Do I have to get a new token and/or session ID for every stream/streamer? I’m pretty sure I can create a script to do that. I’m just unsure how to get the session IDs, properly. Running a version of your command just gives an output of:
[1] 49383
-bash: X-Plex-Token=xxxPLEXxxTOKENxxx: command not found
PLEX-Mac-mini:local admin$ <html><head><title>Unauthorized</title></head><body><h1>401 Unauthorized</h1></body></html>
[1]+ Done curl -X GET 127.0.0.1:32400/status/sessions?X-Plex-Client-Identifier=alexvanhaaff
I’m also guessing that your Plex-Client-Indentifier would be the individual User Accounts, or would it be the PlexPass account name?
Is this something that might require another sort of authentication? Like a PIN?
And 4 the fun of it, I created a bash script
You’ll need to customize first 4 lines
#!/bin/sh
PMS_IP='192.168.1.14'
TOKEN='YOUR_TOKEN'
MSG='Power+off+now'
CLIENT_IDENT='SomeRandomStringWithoutSpace'
#Start by getting the active sessions
sessionURL="http://$PMS_IP:32400/status/sessions?X-Plex-Client-Identifier=$CLIENT_IDENT&X-Plex-Token=$TOKEN"
response=$(curl -i -k -L -s $sessionURL)
sessions=$(printf %s "$response"| grep '^<Session*'| awk -F= '$1=="id"{print $2}' RS=' '| cut -d '"' -f 2)
# Active sessions id's now stored in sessions variable, so convert to an array
set -f # avoid globbing (expansion of *).
array=(${sessions//:/ })
for i in "${!array[@]}"
do
echo "Need to kill session: ${array[i]}"
killURL="http://$PMS_IP:32400/status/sessions/terminate?sessionId=${array[i]}&reason=$MSG&X-Plex-Client-Identifier=$CLIENT_IDENT&X-Plex-Token=$TOKEN"
# Kill it
response=$(curl -i -k -L -s $killURL)
# Get response
http_status=$(echo "$response" | grep HTTP | awk '{print $2}')
if [ $http_status -eq "200" ]
then
echo "Succes with killing of stream ${array[i]}"
else
echo "Something went wrong here"
fi
done
I think he tried to use his token, but the unescaped & got eaten and interpreted by the shell. The bash output [1] 49383 means a job got spawned into the background, and the -bash : X-Plex-... detritus means bash tried to execute the rest of the URI as a command.
I think wrapping the argument in some nice protective quotes will do it:
curl -X GET “127.0.0.1:32400/status/sessions?X-Plex-Client-Identifier=alexvanhaaff&X-Plex-Token=<INSERT_TOKEN_HERE>”