shallowbit
8/21/2019 - 5:34 PM

Wake on LAN Utility

Wake on LAN Utility

#!/bin/bash

# sudo apt install bash-completion
# sudo cp etc-bash_completion.d-wol.bash /etc/bash_completion.d/wol.bash

_wol() {
    local _cur="${COMP_WORDS[COMP_CWORD]}"
    COMPREPLY=( $(compgen -W "$(wol --list)" -- "$_cur") )
}

complete -F _wol wol
#!/bin/bash

# sudo apt install wakeonlan
# sudo cp wol /usr/local/bin/wol
# sudo chmod 0755 /usr/local/bin/wol

if ! which wakeonlan &> /dev/null; then
    echo Please install 'wakeonlan'
    exit 1
fi

CONFIG_FILE="/path/to/wol.conf"
#CONFIG_FILE="/usr/local/etc/wol.conf"

declare -A settings
param=()

while read line; do
    _e=( ${line/\#*/} )
    _name="${_e[0]}"
    _mac="${_e[1]}"
    if [ "$_name" != "" -a "$_mac" != "" ]; then
        settings["${_name}"]="${_mac}"
    fi
done < "$CONFIG_FILE"

usage () {
    cat << EOL
Usage: ${0##*/} [--list|--all] [ MAC Address | IP Address | Target name ]...

Options:
    --list:     show Target names
    --all:      send magic packet to all target
    -h, --help: show this message
EOL
}

show_target_list () {
    local max_len=0 len
    for name in ${!settings[@]}; do
        len=$((${#name} + 1))
        if [ ${#name} -gt $max_len ]; then
            max_len=$len
        fi
    done

    for name in ${!settings[@]}; do
        printf "%-${max_len}s %s\n" "$name" "${settings[${name}]}"
    done
}

if [[ -z "$1" ]]; then
    show_target_list
else
    for _opt in "$@"; do
        case "$_opt" in
            '--list' )
                show_target_list
                exit 0
                ;;
            '--all' )
                param=(${!settings[@]})
                break
                ;;
            '-h'|'--help' )
                usage
                exit 0
                ;;
            *)
                param+=("$1")
                shift
                ;;
        esac
    done
fi

for e in ${param[@]}; do
    if [ "$(echo "${settings[${e}]}")" != "" ]; then
        wakeonlan "${settings[${e}]}"
    else
        wakeonlan "${e}"
    fi
done
[Unit]
Description=Wake on LAN enabler

[Service]
Type=oneshot
ExecStart=/sbin/ethtool -s eth0 wol g
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

# sudo cp wol-enabler.service /etc/systemd/system/wol-enabler.service
# sudo systemctl daemon-reload
# sudo systemctl enable wol-enabler.service
# name         MAC-address
http-server    00:11:22:aa:bb:cc
nas-server     99:88:77:ff:ee:dd
pc1            00:00:00:00:00:00