Skip to content

Instantly share code, notes, and snippets.

@ryunp
Last active August 29, 2015 14:26
Show Gist options
  • Save ryunp/f057caedcb4af56d06b6 to your computer and use it in GitHub Desktop.
Save ryunp/f057caedcb4af56d06b6 to your computer and use it in GitHub Desktop.
+=====================+
| Results of WoL.sh: |
+=====================+
ryan@ryPi2 ~ $ ./wol.sh
Format Error
Usage: ./wol.sh IP4|MAC
ryan@ryPi2 ~ $ ./wol.sh 192.168
Format Error
Usage: ./wol.sh IP4|MAC
ryan@ryPi2 ~ $ ./wol.sh 192.168.1.
Format Error
Usage: ./wol.sh IP4|MAC
ryan@ryPi2 ~ $ ./wol.sh 192.168.1.1
Sending WOL packet to 192.168.1.1 (e8:94:f6:62:32:b2)
Sending magic packet to 255.255.255.255:9 with e8:94:f6:62:32:b2
ryan@ryPi2 ~ $ ./wol.sh 192.168.1.100
192.168.1.100 Not Found On Network
ryan@ryPi2 ~ $ ./wol.sh 192.168.1.106
Sending WOL packet to 192.168.1.106 (00:23:69:6f:ec:c1)
Sending magic packet to 255.255.255.255:9 with 00:23:69:6f:ec:c1
ryan@ryPi2 ~ $ ./wol.sh 192.168.1.41
Sending WOL packet to 192.168.1.41 (e8:4e:06:18:9f:66)
Sending magic packet to 255.255.255.255:9 with e8:4e:06:18:9f:66
ryan@ryPi2 ~ $ arp
Address HWtype HWaddress Flags Mask Iface
192.168.1.80 ether 00:23:15:71:f5:e8 C wlan0
192.168.1.20 ether b8:27:eb:ae:40:c5 C wlan0
192.168.1.41 ether e8:4e:06:18:9f:66 C wlan0
192.168.1.106 ether 00:23:69:6f:ec:c1 C wlan0
192.168.1.1 ether e8:94:f6:62:32:b2 C wlan0
ryan@ryPi2 ~ $ ./wol.sh e8:4e:06:18:9f:66
Sending WOL packet to 192.168.1.41 (e8:4e:06:18:9f:66)
Sending magic packet to 255.255.255.255:9 with e8:4e:06:18:9f:66
+=========+
| WoL.sh: |
+=========+
#!/bin/bash
# Program accepts 1 parameter, IP4 or MAC
# We know one, set the other in main()
#
# I had to make use of some global cariable access
# throughout the functions as to not have seriously
# shit-tastic reference passing. So it may get speghetti.
##############################
# Entry point
##############################
# @param1 Assumed IP or MAC address
main() {
if is_MAC_address "$1"; then
if valid_MAC "$1"; then
if ! MAC_to_IP4 "$1"; then
error "$MAC $ERROR_NOTFOUND"
fi
fi
elif is_IP4_address "$1"; then
if valid_IP4 "$1"; then
if ! IP4_to_MAC "$1"; then
error "$IP4 $ERROR_NOTFOUND"
fi
fi
else
usage
error "$ERROR_FORMAT"
fi
echo "Sending WOL packet to $IP4 ($MAC)"
wakeonlan "$MAC"
}
##############################
# IP4 Methods
##############################
# @param1 Unknown address
is_MAC_address() {
if [ "$1" != "${1/:/}" ]; then
return 0
fi
return 1
}
# @param1 MAC Address
valid_MAC() {
# Break into an array of octets
IFS=':' MAC_ARRAY=($1)
# Ensure Length
if [ ${#MAC_ARRAY[@]} -eq 6 ]; then
# Ensure valid range for octets
for octet in ${MAC_ARRAY[@]}; do
octet=$(printf "%d" "0x$octet")
if [ $? -ne 0 ]; then
usage
error $ERROR_FORMAT
fi
if [ $octet -lt 0 ] || [ $octet -gt 255 ]; then
error "$octet $ERROR_RANGE"
fi
done
else
usage
error "$ERROR_FORMAT"
fi
return 0
}
# @param1 (hopefully) valid IP4 Address
IP4_to_MAC() {
MAC=$(arp | grep -w "$1" | awk '{print($3)}')
if [ $MAC ];then
return 0
fi
return 1
}
##############################
# IP4 Methods
##############################
# @param1 Unknown address
is_IP4_address() {
if [ "$1" != "${1/./}" ]; then
return 0
fi
return 1
}
# @param1 IP4 Address
valid_IP4() {
# Break into an array of octets
IFS='.' IP4_ARRAY=($1)
# Ensure 24 bits
if [ ${#IP4_ARRAY[@]} -eq 4 ]; then
# Ensure valid range for octets
for octet in ${IP4_ARRAY[@]}; do
if [ $octet -lt 0 ] || [ $octet -gt 255 ]; then
error "$octet $ERROR_RANGE"
fi
done
else
usage
error "$ERROR_FORMAT"
fi
return 0
}
# @param1 (hopefully) Valid MAC Address
MAC_to_IP4() {
IP4=$(arp | grep -w "$1" | awk '{print($1)}')
if [ $IP4 ];then
return 0
fi
return 1
}
##############################
# Misc methods
##############################
error() {
echo -e "$@"
exit 1
}
usage() {
echo "Usage: $0 IP4|MAC"
}
##############################
# Globals
##############################
IP4=$1
MAC=$1
ERROR_RANGE="Not In Range"
ERROR_FORMAT="Format Error"
ERROR_NOTFOUND="Not Found On Network"
# Call main after functions have been defined
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment