Skip to content

Instantly share code, notes, and snippets.

@alex3305
Created August 14, 2023 11:54
Show Gist options
  • Save alex3305/8cc73ddd2c8ca6328f20235480aaa594 to your computer and use it in GitHub Desktop.
Save alex3305/8cc73ddd2c8ca6328f20235480aaa594 to your computer and use it in GitHub Desktop.
Plex Wake On Demand
[Unit]
Description=Plex Wake on Demand Service
[Service]
User=root
Type=simple
ExecStart=/bin/bash /opt/scripts/plex-wol.sh
KillMode=mixed
[Install]
WantedBy=multi-user.target
#!/bin/bash
### Plex - Wake On Demand
###
### Author Alex van den Hoogen
### Last change Febraury 21, 2019
###
# START CONFIGURATION
mac_address="00:00:00:00:00:00"
plex_server_ip="192.168.1.100"
plex_target_port=32400
banned_ips="localhost or 127.0.0.1 or 192.168.1.1"
docker_ips="172.16.0.0/12"
timeout=1800
# END CONFIGURATION
last_src=""
last_timestamp=$(date +%s)
tcpdump -C 10 -l "tcp[tcpflags] & (tcp-syn) != 0 and dst port $plex_target_port and dst host $plex_server_ip and not (src ($banned_ips) or (src net $docker_ips))" | while read line
do
parsed_line=$(echo ${line} | cut -d ' ' -f 1-3)
if [[ ${parsed_line} == *"amazonaws.com"* ]]; then
echo "$parsed_line IGNORING - PERM BAN"
else
src=$(echo ${parsed_line} | cut -f 3 -d ' ' | cut -f 1-4 -d '.')
date="$(echo $(date +'%Y-%m-%d')T$(echo ${parsed_line} | cut -f 1 -d ' '))"
timestamp="$(echo ${date} | date +%s)"
diff_timestamp=$(expr ${timestamp} - ${last_timestamp})
if [[ ${last_src} == ${src} && ${diff_timestamp} -lt ${timeout} ]]; then
echo "$parsed_line IGNORING - TEMP BAN (last wakeup: $(date -d @${last_timestamp}))"
else
last_src=${src}
last_timestamp=${timestamp}
wakeonlan ${mac_address} >/dev/null
echo "$parsed_line WAKING UP!"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment