Skip to content

Instantly share code, notes, and snippets.

@asamofal
Last active November 14, 2018 13:34
Show Gist options
  • Save asamofal/c32b95fb4bc7d4709e3045f203931572 to your computer and use it in GitHub Desktop.
Save asamofal/c32b95fb4bc7d4709e3045f203931572 to your computer and use it in GitHub Desktop.
#!/bin/bash
##################################################
# auto wakeonlan v. 1.4
# Anton Samofal, 2018
##################################################
# set MACs for wakeup
declare -A macToWake=(
[Anton_Sam]=b0:6e:bf:c4:66:65
[Evgeny_Yar]=44:8a:5b:8d:ff:2b
[Dima_Sob]=c8:60:00:cf:e4:95
[Sasha_Kan]=60:a4:4c:a9:65:76
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# immediately open popup (param "wakeup")
action=$1
# design
txtbld=$(tput bold)
red=${txtbld}$(tput setaf 1)
green=${txtbld}$(tput setaf 2)
blue=${txtbld}$(tput setaf 4)
reset=$(tput sgr0)
toend=$(tput hpa $(tput cols))$(tput cub 6)
fileName=$(basename $0)
autostartPath="$HOME/.config/autostart"
# get ethernet inerface name (from default gateway)
intName=$(ip -o -4 route show to default | awk '{print $5}')
# get current MAC
currMac=$(cat /sys/class/net/$intName/address)
# get current netmask
netMask=$(ip -o -f inet addr show $intName | awk '{print $6}')
desktopEntry="
[Desktop Entry]
Type=Application
Exec=/usr/local/bin/$fileName wakeup
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=PowerOn"
wolService="
[Unit]
Description='Enable WOL'
[Service]
Type=oneshot
ExecStart=/sbin/ethtool -s $intName wol g
[Install]
WantedBy=multi-user.target"
# check root permission
if [[ -z "$action" && "$(whoami)" != 'root' ]]; then
echo $"${red}You should run this script as super-user."
echo -n "${reset}"
exit 1;
fi
function install() {
if [[ ! -e "/usr/local/bin/$fileName" ]]; then
# check ethtool package & install
if ! which ethtool > /dev/null; then
echo "${blue}ethtool does not exist. Installing..."
echo -n "${reset}"
apt-get install ethtool -y > /dev/null
fi
# check wakeonlan package & install
if ! which wakeonlan > /dev/null; then
echo "${blue}wakeonlan does not exist. Installing..."
echo -n "${reset}"
apt-get install wakeonlan -y > /dev/null
fi
# copy itself
cp "$(readlink -f $0)" "/usr/local/bin/$fileName"
chmod +x "/usr/local/bin/$fileName"
# create service to enable WOL on startup
if which ethtool > /dev/null; then
if [ ! -e "/etc/systemd/system/wol.service" ]; then
touch /etc/systemd/system/wol.service
echo "$wolService" > /etc/systemd/system/wol.service
systemctl enable wol.service &> /dev/null
systemctl start wol.service
fi
fi
# run at startup
if [ ! -e "$autostartPath/.desktop" ]; then
touch "$autostartPath/.desktop"
chmod +x "$autostartPath/.desktop"
echo "$desktopEntry" > "$autostartPath/.desktop"
else
if ! grep "wakeonlan" "$autostartPath/.desktop" > /dev/null
then
echo "$desktopEntry" >> "$autostartPath/.desktop"
fi
fi
echo "${green}$fileName successfully added to startup!${toend}[OK]"
echo -n "${reset}"
else
echo "${green}$fileName is already installed!"
echo -n "${reset}"
fi
}
function uninstall() {
if [[ -e "/usr/local/bin/$fileName" ]]; then
rm "/usr/local/bin/$fileName"
# if [[ -e "$autostartPath/.desktop" ]]; then
# grep -v "$(grep -C 4 "$fileName" "$autostartPath/.desktop")" "$autostartPath/.desktop" > "$autostartPath/desktop_tmp"
# rm "$autostartPath/.desktop"
# cat "$autostartPath/desktop_tmp" > "$autostartPath/.desktop" && chmod +x "$autostartPath/.desktop"
# rm "$autostartPath/desktop_tmp"
# fi
echo "${green}$fileName successfully removed!${toend}[OK]"
echo -n "${reset}"
else
echo "${red}$fileName not installed!${toend}[fail]"
echo -n "${reset}"
fi
if [[ -e "/etc/systemd/system/wol.service" ]]; then
systemctl disable wol.service &> /dev/null
systemctl stop wol.service
rm /etc/systemd/system/wol.service > /dev/null
fi
}
function wakeup() {
# prepare string for popup
for key in "${!macToWake[@]}"
do
if [[ "${macToWake[$key]}" != "$currMac" ]]; then
unit+="TRUE $key ${macToWake[$key]} "
fi
done
# show popup
popup=$(zenity --list \
--width=290 \
--height=300 \
--title "Power ON" \
--text "Select computers to wake up:" \
--print-column=3 \
--separator=" " \
--checklist \
--column "" \
--column "Name" \
--column "MAC" \
$unit 2> /dev/null)
# prepare final array
macToWakeSelected=($popup)
# get ethernet inerface name (from default gateway)
#intName=$(ip -o -4 route show to default | awk '{print $5}')
# WAKE UP!
for mac in "${macToWakeSelected[@]}"
do
wakeonlan -i "$netMask" $mac > /dev/null
wakeonlan -i "$netMask" $mac > /dev/null
echo "${blue}Sending magic packet to $mac${toend}${green}[OK]"
echo -n "${reset}"
done
}
# immediately open popup
if [[ "$action" == "wakeup" ]]; then
wakeup
exit 1;
fi
clear
echo "===============================================
Welcome to ${txtbld}$fileName${reset} script!
==============================================="
PS3='Please select option, type number and press Enter: '
COLUMNS=12
select opt in 'Install' 'Uninstall script' 'WakeUp!' 'Exit'
do
if [ "$opt" == 'Install' ]; then
install
break
elif [ "$opt" == 'Uninstall script' ]; then
uninstall
break
elif [ "$opt" == 'WakeUp!' ]; then
wakeup
break
elif [ "$opt" == 'Exit' ]; then
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment