Advertisement
KC9UZR

Ultimate Wake On Lan Bash Script

Jun 17th, 2025
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.59 KB | Software | 0 0
  1. #!/bin/bash
  2.  
  3. # =======================================================
  4. # ============= Ultimate Wake On Lan v1.3 ===============
  5. # =======================================================
  6. # Adds a fully automatic scan/wake mode and more output.
  7. #
  8. # USAGE:
  9. # Make it executable: chmod +x wakeup.sh
  10. # Run with sudo:      sudo ./wakeup.sh
  11.  
  12. # --- Configuration ---
  13. NETWORK="192.168.1.0/24"
  14. KNOWN_HOSTS_FILE="${HOME}/.wol_known_hosts"
  15.  
  16. # --- Color Definitions ---
  17. RED='\033[0;31m'
  18. L_RED='\033[1;31m'
  19. GREEN='\033[0;32m'
  20. L_GREEN='\033[1;32m'
  21. YELLOW='\033[0;33m'
  22. L_YELLOW='\033[1;33m'
  23. BLUE='\033[0;34m'
  24. L_BLUE='\033[1;34m'
  25. MAGENTA='\033[0;35m'
  26. L_MAGENTA='\033[1;35m'
  27. CYAN='\033[0;36m'
  28. L_CYAN='\033[1;36m'
  29. WHITE='\033[1;37m'
  30. BOLD='\033[1m'
  31. NC='\033[0m' # No Color
  32.  
  33. # --- Core Functions ---
  34.  
  35. check_tools() {
  36.   local missing_tools=0
  37.   for tool in "$@"; do
  38.     if ! command -v "$tool" &> /dev/null; then echo -e "${RED}Error: Required command '${BOLD}$tool${RED}' is not installed.${NC}"; missing_tools=1; fi
  39.   done
  40.   if [ $missing_tools -ne 0 ]; then
  41.     echo -e "\n${YELLOW}Please install the missing tools to proceed.${NC}"; echo "On Debian/Ubuntu: ${CYAN}sudo apt-get install nmap wakeonlan${NC}"; echo "On Fedora/CentOS: ${CYAN}sudo dnf install nmap wakeonlan${NC}"; exit 1
  42.   fi
  43. }
  44.  
  45. scan_network() {
  46.   local nmap_flags="$1"
  47.   echo -e "---------------------------------------------------------"
  48.   echo -e "${CYAN}Starting network scan on ${BOLD}${NETWORK}${CYAN} with flags: ${BOLD}${nmap_flags}${NC}"
  49.   echo -e "${YELLOW}Displaying nmap output live. This may take a while.${NC}"
  50.   echo -e "---------------------------------------------------------"
  51.  
  52.   local nmap_output_file=$(mktemp)
  53.   local start_time=$(date +%s)
  54.  
  55.   # Increased verbosity to -vv for more output
  56.   nmap -vv ${nmap_flags} "${NETWORK}" | tee "$nmap_output_file"
  57.  
  58.   local end_time=$(date +%s)
  59.   local duration=$((end_time - start_time))
  60.  
  61.   echo -e "---------------------------------------------------------"
  62.   echo -e "${GREEN}Parsing results...${NC}"
  63.  
  64.   local devices
  65.   devices=$(awk -v ip_color="$L_GREEN" -v mac_color="$L_CYAN" -v nc="$NC" \
  66.       '/Nmap scan report for/{ip=$5} /MAC Address:/{mac=$3; vendor=substr($0, index($0,$4)); printf "%s%s%s %s%s%s %s\n", ip_color, ip, nc, mac_color, mac, nc, vendor}' "$nmap_output_file")
  67.  
  68.   local device_count
  69.   # Use xargs to trim whitespace from wc -l output
  70.   device_count=$(echo "$devices" | wc -l | xargs)
  71.  
  72.   echo -e "${L_GREEN}${BOLD}Scan Complete.${NC} Found ${WHITE}${BOLD}${device_count}${NC} devices in ${WHITE}${BOLD}${duration}${NC} seconds."
  73.   echo -e "---------------------------------------------------------"
  74.  
  75.   rm "$nmap_output_file"
  76.   # Return the found devices for further action
  77.   echo "$devices"
  78. }
  79.  
  80. wake_device() {
  81.   local mac=$1
  82.   echo -e "\n${MAGENTA}Sending Wake-on-LAN packet to ${L_CYAN}${BOLD}${mac}${MAGENTA}...${NC}"
  83.   wakeonlan "${mac}"; echo -e "${GREEN}Packet sent.${NC}"
  84. }
  85.  
  86. # --- Menu Functions ---
  87.  
  88. action_menu() {
  89.   local devices="$1"
  90.   echo -e "${BLUE}${BOLD}--- Scan Results ---${NC}"
  91.   echo -e "${WHITE}$(echo -e "$devices" | cat -n)${NC}"
  92.   read -p "Save these devices to the known hosts list? (y/N): " save_choice
  93.   if [[ "$save_choice" =~ ^[Yy]$ ]]; then
  94.     touch "$KNOWN_HOSTS_FILE"
  95.     echo -e "$devices" | sed -r 's/\x1b\[[0-9;]*m//g' | while read -r device_line; do
  96.       local mac=$(echo "$device_line" | awk '{print $2}'); if ! grep -qF "$mac" "$KNOWN_HOSTS_FILE"; then echo "$device_line" >> "$KNOWN_HOSTS_FILE"; echo -e "${GREEN}Saved:${NC} $device_line"; fi
  97.     done
  98.   fi
  99.   PS3=$(echo -e "\n${YELLOW}Please choose an action: ${NC}")
  100.   local action_options=("Wake a Specific Device from Scan" "Wake All Devices from Scan" "Done")
  101.   select action in "${action_options[@]}"; do
  102.     case $action in
  103.       "Wake a Specific Device from Scan")
  104.         local device_options; mapfile -t device_options <<< "$devices"; device_options+=("Cancel")
  105.         PS3=$(echo -e "${YELLOW}Enter number of device to wake: ${NC}")
  106.         select choice in "${device_options[@]}"; do
  107.           if [[ "$choice" == "Cancel" ]]; then break; fi; if [[ -n "$choice" ]]; then wake_device "$(echo -e "$choice" | sed -r 's/\x1b\[[0-9;]*m//g' | awk '{print $2}')"; break; else echo -e "${RED}Invalid selection.${NC}"; fi
  108.         done; break ;;
  109.       "Wake All Devices from Scan") echo -e "$devices" | sed -r 's/\x1b\[[0-9;]*m//g' | awk '{print $2}' | while read -r mac; do wake_device "$mac"; done; break ;;
  110.       "Done") break ;;
  111.       *) echo -e "${RED}Invalid option $REPLY${NC}" ;;
  112.     esac
  113.   done
  114. }
  115.  
  116. timing_menu() {
  117.   local profile_flags="$1"
  118.   echo -e "${L_BLUE}${BOLD}--- Step 2: Choose Scan Speed ---${NC}"
  119.   PS3=$(echo -e "\n${YELLOW}Aggressive (T4) is best for fast networks. Use Polite (T2) if you see errors.${NC}\nSelect a timing template: ")
  120.   local timing_options=("Paranoid (T0)" "Sneaky (T1)" "Polite (T2)" "Normal (T3)" "Aggressive (T4)" "Insane (T5)" "Cancel")
  121.   select timing_choice in "${timing_options[@]}"; do
  122.     local timing_flag=""; case $timing_choice in "Paranoid (T0)") timing_flag="-T0";; "Sneaky (T1)") timing_flag="-T1";; "Polite (T2)") timing_flag="-T2";; "Normal (T3)") timing_flag="-T3";; "Aggressive (T4)") timing_flag="-T4";; "Insane (T5)") timing_flag="-T5";; "Cancel") return;; *) echo -e "${RED}Invalid option $REPLY${NC}"; continue;; esac
  123.     local devices; devices=$(scan_network "${profile_flags} ${timing_flag}")
  124.     if [ -z "$devices" ]; then echo -e "${YELLOW}No devices with discoverable MAC addresses were found.${NC}"; else action_menu "$devices"; fi
  125.     break
  126.   done
  127. }
  128.  
  129. scan_profile_menu() {
  130.   echo -e "${L_BLUE}${BOLD}--- Step 1: Choose Scan Profile ---${NC}"
  131.   PS3=$(echo -e "\n${YELLOW}Select what you want to find: ${NC}")
  132.   local profile_options=("Simple Host List (for WOL)" "Quick Port Scan" "Full TCP Port Scan" "Aggressive Scan (OS, Version)" "Back to Main Menu")
  133.   select opt in "${profile_options[@]}"; do
  134.     local nmap_params=""; case $opt in "Simple Host List (for WOL)") nmap_params="-sn";; "Quick Port Scan") nmap_params="-F";; "Full TCP Port Scan") nmap_params="-p-";; "Aggressive Scan (OS, Version)") nmap_params="-A";; "Back to Main Menu") return;; *) echo -e "${RED}Invalid option $REPLY${NC}"; continue;; esac
  135.     timing_menu "$nmap_params"; break
  136.   done
  137. }
  138.  
  139. automatic_wake_all() {
  140.   echo -e "${L_BLUE}${BOLD}--- Automatic Scan & Wake All ---${NC}"
  141.   # Use the fastest, most reliable scan for finding WOL-ready hosts on a LAN.
  142.   local nmap_params="-sn -T4"
  143.   local devices
  144.   devices=$(scan_network "$nmap_params")
  145.  
  146.   if [ -z "$devices" ]; then
  147.     echo -e "\n${YELLOW}No devices were found to wake up.${NC}"
  148.   else
  149.     echo -e "\n${GREEN}Waking up all discovered devices...${NC}"
  150.     # Extract just the MAC address (2nd column after stripping colors) and wake each one
  151.     echo -e "$devices" | sed -r 's/\x1b\[[0-9;]*m//g' | awk '{print $2}' | while read -r mac; do
  152.       wake_device "$mac"
  153.     done
  154.     echo -e "\n${GREEN}${BOLD}All WOL packets have been sent!${NC}"
  155.   fi
  156. }
  157.  
  158. direct_wake_menu() { # (Unchanged)
  159.   echo -e "${L_MAGENTA}${BOLD}--- Direct Wake ---${NC}"; read -p "Enter the MAC address to wake: " mac_address
  160.   if [[ "$mac_address" =~ ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$ ]]; then wake_device "$mac_address"; else echo -e "${RED}Invalid MAC address format.${NC}"; fi
  161. }
  162.  
  163. change_network_menu() { # (Unchanged)
  164.   echo -e "${L_MAGENTA}${BOLD}--- Change Network Target ---${NC}"; echo -e "Current network target is: ${L_CYAN}${NETWORK}${NC}"
  165.   read -p "Enter new network target in CIDR format (e.g., 10.0.0.0/24): " new_network
  166.   if [ -n "$new_network" ]; then NETWORK="$new_network"; echo -e "${GREEN}Network target updated to ${BOLD}${NETWORK}${NC} for this session."; else echo -e "${RED}No input provided.${NC}"; fi
  167. }
  168.  
  169. saved_hosts_menu() { # (Unchanged)
  170.   echo -e "${L_MAGENTA}${BOLD}--- Wake from Saved List ---${NC}"
  171.   if [[ ! -f "$KNOWN_HOSTS_FILE" || ! -s "$KNOWN_HOSTS_FILE" ]]; then echo -e "${YELLOW}Known hosts file is empty.${NC}"; return; fi
  172.   echo -e "${WHITE}Available saved hosts:${NC}"; awk -v ip_color="$L_GREEN" -v mac_color="$L_CYAN" -v nc="$NC" '{printf "%s%s%s %s%s%s %s\n", ip_color, $1, nc, mac_color, $2, nc, substr($0, index($0,$3))}' "$KNOWN_HOSTS_FILE" | cat -n
  173.   PS3=$(echo -e "\n${YELLOW}Choose an option for saved hosts: ${NC}"); local saved_options=("Wake a Specific Saved Host" "Wake ALL Saved Hosts" "Delete a Saved Host" "Cancel")
  174.   select opt in "${saved_options[@]}"; do
  175.     case $opt in
  176.       "Wake a Specific Saved Host"|"Delete a Saved Host") local device_options; mapfile -t device_options < "$KNOWN_HOSTS_FILE"; device_options+=("Cancel"); PS3=$(echo -e "${YELLOW}Select a host to ${opt// a Saved Host/}: ${NC}"); select choice in "${device_options[@]}"; do if [[ "$choice" == "Cancel" ]]; then break; fi; if [[ -n "$choice" ]]; then if [[ "$opt" == "Wake a Specific Saved Host" ]]; then wake_device "$(echo "$choice" | awk '{print $2}')"; else grep -vFw "$choice" "$KNOWN_HOSTS_FILE" > "$KNOWN_HOSTS_FILE.tmp" && mv "$KNOWN_HOSTS_FILE.tmp" "$KNOWN_HOSTS_FILE"; echo -e "${GREEN}Deleted entry:${NC} $choice"; fi; break; else echo -e "${RED}Invalid selection.${NC}"; fi; done; break ;;
  177.       "Wake ALL Saved Hosts") awk '{print $2}' "$KNOWN_HOSTS_FILE" | while read -r mac; do wake_device "$mac"; done; break ;;
  178.       "Cancel") break ;;
  179.       *) echo -e "${RED}Invalid option $REPLY${NC}";;
  180.     esac
  181.   done
  182. }
  183.  
  184. help_menu() {
  185.     echo -e "${L_MAGENTA}${BOLD}--- Help & Information ---${NC}"
  186.     echo -e "Welcome to the ${BOLD}Ultimate Wake On Lan${NC} script (v1.3)."
  187.     echo -e "\n${BOLD}Menu Options:${NC}"
  188.     echo -e "  ${L_RED}Automatic Scan & Wake:${NC} The express option. Immediately scans for all devices"
  189.     echo -e "                       on the network and sends a WOL packet to each one."
  190.     echo -e "  ${L_GREEN}Manual Scan Network:${NC}   Provides full control over the scan. You first choose a"
  191.     echo -e "                       Scan Profile (what to look for), then a Timing Template"
  192.     echo -e "                       (how fast to scan)."
  193.     echo -e "  ${L_CYAN}Saved & Direct Wake:${NC} Wake devices from your saved list or by entering a"
  194.     echo -e "                       MAC address manually."
  195.     echo -e "\n${BOLD}Scan Timing (-T0 to -T5):${NC}"
  196.     echo -e "  If you get ${L_RED}timeout errors${NC}, your network can't keep up. Slow down the scan!"
  197.     echo -e "  ${L_GREEN}-T4 (Aggressive):${NC} Recommended for fast, modern LANs."
  198.     echo -e "  ${L_YELLOW}-T3 (Normal):${NC}      Nmap's default behavior."
  199.     echo -e "  ${L_RED}-T2 (Polite):${NC}       Slower scan that uses less bandwidth. Try this if T4/T3 fail."
  200. }
  201.  
  202. main_menu() {
  203.   while true; do
  204.     echo -e "${L_RED}${BOLD}U${L_YELLOW}l${L_GREEN}t${L_CYAN}i${L_BLUE}m${L_MAGENTA}a${L_RED}t${L_YELLOW}e ${L_GREEN}W${L_CYAN}a${L_BLUE}k${L_MAGENTA}e ${L_RED}O${L_YELLOW}n ${L_GREEN}L${L_CYAN}a${L_BLUE}n${NC} ${WHITE}v1.3${NC}"
  205.     echo -e "${BLUE}=======================================================${NC}"
  206.     echo -e "  Network Target: ${L_CYAN}${BOLD}${NETWORK}${NC}"
  207.     echo -e "${BLUE}=======================================================${NC}"
  208.  
  209.     PS3=$(echo -e "\n${WHITE}${BOLD}Select an option: ${NC}")
  210.     local options=(
  211.       "$(echo -e "${L_RED}🚀${NC} Automatic Scan & Wake All")"
  212.       "$(echo -e "${L_GREEN}★${NC} Manual Scan Network")"
  213.       "$(echo -e "${L_CYAN}★${NC} Wake from Saved List")"
  214.       "$(echo -e "${L_BLUE}★${NC} Wake a MAC Address Directly")"
  215.       "$(echo -e "${L_MAGENTA}★${NC} Change Network Target")"
  216.       "$(echo -e "${L_YELLOW}★${NC} Help & Information")"
  217.       "$(echo -e "${WHITE}★${NC} Exit")"
  218.     )
  219.     select opt in "${options[@]}"; do
  220.       local clean_opt; clean_opt=$(echo "$opt" | sed -r 's/\x1b\[[0-9;]*m//g' | sed -r 's/🚀 |★ //g')
  221.       case "$clean_opt" in
  222.         "Automatic Scan & Wake All") automatic_wake_all; break ;;
  223.         "Manual Scan Network") scan_profile_menu; break ;;
  224.         "Wake from Saved List") saved_hosts_menu; break ;;
  225.         "Wake a MAC Address Directly") direct_wake_menu; break ;;
  226.         "Change Network Target") change_network_menu; break ;;
  227.         "Help & Information") help_menu; break ;;
  228.         "Exit") echo -e "${GREEN}Exiting. Goodbye!${NC}"; exit 0 ;;
  229.         *) echo -e "${RED}Invalid option $REPLY. Please choose a number from the list.${NC}" ;;
  230.       esac
  231.     done
  232.     echo -e "\n${YELLOW}Press any key to return to the main menu...${NC}"
  233.     read -n 1 -s -r
  234.     clear
  235.   done
  236. }
  237.  
  238. # --- Script Start ---
  239. clear
  240. check_tools "nmap" "wakeonlan"
  241. if [ "$EUID" -ne 0 ]; then echo -e "${RED}${BOLD}Root Access Required${NC}\n${YELLOW}Please run with sudo for best results.${NC}"; exit 1; fi
  242. main_menu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement