Sample Script for Wake-on-LAN
Reminder:
Do not forget to give permissions before running the scripts.
chmod +x <name>.shOriginally Designed by Me (Old Version)
#!/usr/bin/env bash
declare -a DEVICE=("xx:xx:xx:xx:xx:xx"
"xx:xx:xx:xx:xx:xx"
"xx:xx:xx:xx:xx:xx")
declare -i COUNTER=0
for a in "${DEVICE[@]}"
do
if sudo wake "$a" ; then
echo "Waked: $a"
let COUNTER++
else
echo "Failed To Send Out Magic Packet"
fi
done
if ((COUNTER == ${#DEVICE[@]})) ; then
echo "Operation Successful"
else
echo "Operation Failed"
fiExplanation
declare -a DEVICE
Declares an array called DEVICE.
"xx:xx:xx:xx:xx:xx"
Replace the value inside the double quotation marks "" with the devices’ mac addresses.
And you can wake as many devices as you want.
wake
The command that sends “Wake on LAN frames to hosts on a local Ethernet network”.
Check Out: wake(8) – FreeBSD Manual Pages
Execution
Go to the path of the script (or export the path and source ~/.bashrc), and type:
chmod +x wake.sh
./wake.shAdvanced Version
Prerequisites:
Network Mapper (Nmap) needs to be installed on the machine
Manually Execute (After Setting Up ALL the Scrips Below):mac-search.shwake.sh
Structure ($PATH):
- data
- found-mac.txt
- mac-backup.txt
- mac-vendor.txt
- nmap-orig-output.txt
- nmap-output.txt
- default.sh
- mac-search.sh
- wake.sh
default.sh
It is always nice to have a default setting.
Script Function:
Create a directory called data to store data files if it does not exist.
Action:
Replace mac-vendor with your own desired mac vendors (Leave it empty if you do not know it, mac-search.sh will find it for you and you can add it later on).... means you can do it as much as you want (do not forget to remove it).
#!/usr/bin/env bash
# Make directory if it does not exist
mkdir -p data
# (Optional) Remove the mac-vendor and mac-backup file to make a fresh one
rm -f data/mac-vendor.txt
rm -f data/mac-backup.txt
# Create a Backup File for MAC Addresses
touch data/mac-backup.txt
# Default MAC Vendors
echo "mac-vendor" >> data/mac-vendor.txt
echo "mac-vendor" >> data/mac-vendor.txt
...mac-search.sh
Script Function:
Get the information scanned with Nmap and store them in a file called nmap-orig-output.txt.
Get the LAN IP address, MAC address, and MAC vendor from nmap-orig-output.txt and store them in a file called nmap-output.txt.
Action:
Replace xxx.xxx.xxx with your own LAN IP address, the * means all.
Most routers use 192.168.0.* OR 192.168.1.*.
#!/usr/bin/env bash
./default.sh
# -sP: Ping Scan
# -oN: Normal Output that will Stores inside the Given File Name
# tr -d '()': Remove Parentheses
sudo nmap -sP -oN data/nmap-orig-output.txt xxx.xxx.xxx.* | \
awk '/Nmap scan report for/{printf $5;}/MAC Address:/{print " " substr($0, index($0,$3))}' | \
tr -d '()' | sort -t . -k 4,4n > data/nmap-output.txt
cat data/nmap-output.txt
# Order:
# LAN IP Address MAC Address MAC Vendor
# *Space BetweenReference for Using the AWK Filter: https://unix.stackexchange.com/questions/505726/nmap-output-in-csv-format-sorted-by-ip-address
wake.sh (New Version)
Script Function:
Inside data directory, create a text file called host to store the hostnames, a host per line.
Get MAC Address from nmap-output.txt by filtering the hostnames inside found-mac.txt.
#!/usr/bin/env bash
# Get MAC Vendors from the mac-vendor.txt (Inputted by user)
mapfile -t VENDOR < data/mac-vendor.txt
# Get MAC Address from nmap-output.txt by
# filtering the MAC Vendors inside found-mac.txt.
for a in "${VENDOR[@]}"
do
awk -v vendor="$a" '
$0 ~ vendor {print $2}
' data/nmap-output.txt >> data/found-mac.txt
done
# For Backup Purpose
cat data/found-mac.txt >> data/mac-backup.txt
awk '!_[$0]++' data/mac-backup.txt > data/found-mac.txt
cat data/found-mac.txt > data/mac-backup.txt
# Convert text file (line-by-line) to an array called "DEVICE"
mapfile -t DEVICE < data/found-mac.txt
for a in "${DEVICE[@]}"
do
sudo wake "$a"
echo "Waked: $a"
doneYou can also create a default text file that stores the MAC addresses manually. (In case something messed up)
For example:
vim data/mac-backup.txtThen:
xx:xx:xx:xx:xx:xx
xx:xx:xx:xx:xx:xx
...Save it and we are finished.
Execution
chmod +x mac-search.sh
chmod +x wake.sh
./mac-search.sh
./wake.sh
Be First to Comment