
#!/bin/sh
# File	: sent_wol.sh
# By	: MaartenDeBoer.nl, 230121
# Subject	: Script to sent a WakeOnLAN for a host
# Pre-req: 
# - WoL NEED to be enabled at (remote) server
#   $ sudo apt install ethtool
#   `/sbin/ethtool -s eth0 wol g`
#   Best to in in /etc/rc.local:
#
# - wakeonlan need to be installed on this server:
#   $ sudo apt install wakeonlan
#
#(0.2),230121	: Added case
#(0.3),230121	: Added usage of etc-file
PGM=`basename $0|cut -d\. -f1`
VER="0.3"
CSV="${HOME}/etc/${PGM}.csv"

if [ "${1}" != "" ]; then
  if [ ! -f ${CSV} ]; then
    echo "`date` NO CSV-file (${CSV}) found."
    exit 3
  fi
  HOST=`grep "^${1};" ${CSV}|awk -F\; '{print $1}'`
  MAC_ADDR=`grep "^${1};" ${CSV}|awk -F\; '{print $2}'`
  IP_ADDR=`grep "^${1};" ${CSV}|awk -F\; '{print $3}'`
  if [ "${HOST}" != "" ] && [ "${HOST}" != "" ] && [ "${HOST}" != "" ]; then
    echo "`date` ${PGM} v${VER} for MAC: ${MAC_ADDR} (${IP_ADDR})."
    /usr/bin/wakeonlan ${MAC_ADDR}
  else
    echo "`date` NO <hostname> , <MAC-address> and/or <IP-address> found in ${CSV}"
    exit 2
  fi

else
  echo "`date` NO <hostname> given: USAGE: ${PGM} <hostname>"
  exit 1
fi


exit 0

