Skip to content

Instantly share code, notes, and snippets.

@richardevcom
Created October 13, 2024 03:25
Show Gist options
  • Save richardevcom/a003fc03c4a2fff8a2107d104c38c700 to your computer and use it in GitHub Desktop.
Save richardevcom/a003fc03c4a2fff8a2107d104c38c700 to your computer and use it in GitHub Desktop.
πŸ›œ airmon-monit.sh - Toggle WiFi interface between monitor and managed mode.
# -----------------------------------------------------------------------------
# monit - Toggle WiFi interface between monitor and managed mode
#
# This script allows you to toggle your WiFi interface between monitor mode
# and managed mode based on the provided arguments.
#
# Usage:
# ./alfa-monit.sh -i <wifi_interface> [-e | --enable | -d | --disable | -h | --help]
#
# Options:
# -i, --interface <wifi_interface> Specify the WiFi interface to toggle.
# -e, --enable Enable monitor mode on the specified interface.
# -d, --disable Disable monitor mode (set to managed mode) on the specified interface.
# -h, --help Display this help message.
#
# Requirements:
# - This script must be run as root.
# - The `airmon-ng` (``aircrack-ng`` package), `ip`, `iw`, and `systemctl` commands must be available.
#
# Author: richardevcom
# URL: https://www.github.com/richardevcom/
# Version: 1.0.2
# Built: 2023-10-04
# -----------------------------------------------------------------------------
#!/bin/bash
# Initialize variables
WIFI_INTERFACE=""
MONITOR_INTERFACE=""
ENABLE_MONITOR=false
DISABLE_MONITOR=false
# Constants for colors
WHITE="\e[38;2;255;255;255m"
LIGHT_BLUE="\e[38;2;238;245;251m"
BLUE="\e[38;2;221;235;247m"
DARK_BLUE="\e[38;2;204;225;243m"
DARKER_BLUE="\e[38;2;187;215;239m"
EVEN_DARKER_BLUE="\e[38;2;170;205;235m"
DARKEST_BLUE="\e[38;2;153;195;231m"
SKY_BLUE="\e[38;2;135;206;235m"
RED="\e[38;2;255;105;97m"
ORANGE="\e[38;2;250;200;152m"
GREEN="\e[38;2;119;221;119m"
RESET="\e[0m"
# Function to display the banner
display_banner() {
echo -e "\n${WHITE} β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ "
echo -e "${LIGHT_BLUE} β–‘β–‘β–‘ β–‘β–‘β–ˆβ–ˆβ–ˆ "
echo -e "${BLUE} β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ "
echo -e "${DARK_BLUE}β–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–ˆβ–ˆβ–ˆβ–‘ "
echo -e "${DARKER_BLUE} β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ "
echo -e "${EVEN_DARKER_BLUE} β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆ"
echo -e "${DARKEST_BLUE} β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ "
echo -e "${SKY_BLUE}β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ "
echo -e "${RESET}"
echo -e "${DARKEST_BLUE}@richardevcom | v1.0.0"
echo -e "${DARKEST_BLUE}Toggle WiFi interface between monitor & managed mode."
echo -e "${RESET}"
}
# Function to display usage
usage() {
echo -e "${DARKEST_BLUE}[i] Usage: $0 -i | --interface <wifi_interface> [-e | --enable | -d | --disable | -h | --help]"
echo -e "${RESET}"
exit 1
}
# Function to display usage
usage_help() {
echo -e "${WHITE}Usage:"
echo -e "${DARKEST_BLUE} $0 -i <wifi_interface> [-e | --enable | -d | --disable | -h | --help]"
echo -e ""
echo -e "${WHITE}Options:"
echo -e "${DARKEST_BLUE} -i, --interface <wifi_interface> Specify the WiFi interface to toggle."
echo -e "${DARKEST_BLUE} -e, --enable Enable monitor mode on the specified interface."
echo -e "${DARKEST_BLUE} -d, --disable Disable monitor mode (set to managed mode) on the specified interface."
echo -e "${DARKEST_BLUE} -h, --help Display this help message."
echo -e ""
echo -e "${WHITE}Requirements:${RESET}"
echo -e "${DARKEST_BLUE} - This script must be run as root."
echo -e "${DARKEST_BLUE} - The \`airmon-ng\` (\`aircrack-ng\` package), \`ip\`, \`iw\`, and \`systemctl\` commands must be available."
echo -e ""
echo -e "${WHITE}Author: ${DARKEST_BLUE}richardevcom"
echo -e "${WHITE}URL: ${DARKEST_BLUE}https://www.github.com/richardevcom/"
echo -e "${WHITE}Version: ${DARKEST_BLUE}1.0.2"
echo -e "${WHITE}Built: ${DARKEST_BLUE}2024-10-04"
echo -e "${RESET}"
exit 1
}
# Function to check if script is run as root
sudo_check() {
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}[-] This script requires to be run as root!${RESET}"
exit 1
fi
}
# Function to enable monitor mode
enable_monitor_mode() {
echo -e "${DARKEST_BLUE}[i] Killing interfering processes...${RESET}"
sudo airmon-ng check kill || echo -e "${RED}[-] Error: Failed to kill interfering processes${RESET}"
sudo systemctl disable avahi-daemon || echo -e "${RED}[-] Error: Failed to disable avahi-daemon${RESET}"
sudo systemctl stop avahi-daemon || echo -e "${RED}[-] Error: Failed to stop avahi-daemon${RESET}"
echo -e "${GREEN}[i] Interfering processes killed!${RESET}"
echo -e "${DARKEST_BLUE}[i] Toggle $WIFI_INTERFACE in monitor mode...${RESET}"
sudo ip link set "$WIFI_INTERFACE" down || echo -e "${RED}[-] Error: Failed to bring down $WIFI_INTERFACE${RESET}"
sudo airmon-ng start "$WIFI_INTERFACE" || echo -e "${RED}[-] Error: Failed to set $WIFI_INTERFACE to monitor mode${RESET}"
MONITOR_INTERFACE=$(iw dev | awk '$1=="Interface"{print $2}' | grep -E "^${WIFI_INTERFACE}mon[0-9]*$|^${WIFI_INTERFACE}$")
sudo ip link set "$MONITOR_INTERFACE" up || echo -e "${RED}[-] Error: Failed to bring up $MONITOR_INTERFACE${RESET}"
echo -e "${GREEN}[i] $MONITOR_INTERFACE set to monitor mode!${RESET}"
echo -e "${DARKEST_BLUE}[i] Re-enabling Network Manager...${RESET}"
sudo systemctl restart NetworkManager || echo -e "${RED}[-] Error: Failed to restart Network Manager${RESET}"
echo -e "${GREEN}[i] Network Manager restarted!${RESET}"
}
# Function to disable monitor mode
disable_monitor_mode() {
echo -e "${DARKEST_BLUE}[i] Toggle $WIFI_INTERFACE in managed mode...${RESET}"
sudo ip link set "$WIFI_INTERFACE" down || echo -e "${RED}[-] Error: Failed to bring down $WIFI_INTERFACE${RESET}"
sudo airmon-ng stop "$MONITOR_INTERFACE" || echo -e "${RED}[-] Error: Failed to stop monitor mode on $MONITOR_INTERFACE${RESET}"
sudo ip link set "$WIFI_INTERFACE" up || echo -e "${RED}[-] Error: Failed to bring up $WIFI_INTERFACE${RESET}"
echo -e "${GREEN}[i] $WIFI_INTERFACE set to managed mode!${RESET}"
echo -e "${DARKEST_BLUE}[i] Re-enabling avahi-daemon...${RESET}"
sudo systemctl enable avahi-daemon || echo -e "${RED}[-] Error: Failed to enable avahi-daemon${RESET}"
sudo systemctl start avahi-daemon || echo -e "${RED}[-] Error: Failed to start avahi-daemon${RESET}"
echo -e "${GREEN}[i] avahi-daemon re-enabled!${RESET}"
echo -e "${DARKEST_BLUE}[i] Re-enabling Network Manager...${RESET}"
sudo systemctl restart NetworkManager || echo -e "${RED}[-] Error: Failed to restart Network Manager${RESET}"
echo -e "${GREEN}[i] Network Manager restarted!${RESET}"
}
# Function to check if required commands are available
check_dependencies() {
sudo_check
local dependencies=("aircrack-ng" "ip" "iw" "systemctl")
local missing_dependencies=()
for cmd in "${dependencies[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
missing_dependencies+=("$cmd")
fi
done
if [ ${#missing_dependencies[@]} -ne 0 ]; then
echo -e "${ORANGE}[!] Warning: The following required packages & commands are missing: ${missing_dependencies[*]}${RESET}"
read -p "Do you want to install the missing packages? (y/n): " choice
case "$choice" in
y|Y )
for pkg in "${missing_dependencies[@]}"; do
sudo apt-get install -y "$pkg" || {
echo -e "${RED}[-] Error: Failed to install $pkg${RESET}"
exit 1
}
done
;;
* )
echo -e "${RED}[-] Error: Required packages are not installed. Exiting...${RESET}"
exit 1
;;
esac
fi
echo -e ""
}
display_banner
check_dependencies
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-i|--interface)
WIFI_INTERFACE="$2"
shift 2
;;
-e|--enable)
if [ "$DISABLE_MONITOR" = true ]; then
echo -e "${RED}[-] Error: Cannot use -e|--enable and -d|--disable together!${RESET}"
usage
fi
sudo_check
ENABLE_MONITOR=true
shift
;;
-d|--disable)
if [ "$ENABLE_MONITOR" = true ]; then
echo -e "${RED}[-] Error: Cannot use -e|--enable and -d|--disable together!${RESET}"
usage
fi
sudo_check
DISABLE_MONITOR=true
shift
;;
-h|--help)
usage_help
;;
*)
echo -e "${RED}[-] Error: Invalid argument $1${RESET}"
usage
;;
esac
done
# Validate required arguments
if [ -z "$WIFI_INTERFACE" ]; then
echo -e "${RED}[-] Error: No WiFi interface provided: -i <wifi_interface>!${RESET}"
usage
fi
# Default to enable monitor mode if neither is specified
if [ "$ENABLE_MONITOR" = false ] && [ "$DISABLE_MONITOR" = false ]; then
echo -e "${RED}[-] Error: Warning: No toggle mode provided!${RESET}"
usage
fi
# Execute the appropriate action
if [ "$ENABLE_MONITOR" = true ]; then
enable_monitor_mode
elif [ "$DISABLE_MONITOR" = true ]; then
disable_monitor_mode
fi
@richardevcom
Copy link
Author

After buying Alfa AWUS036ACH WiFi adapter and going through aircrack-ng/rtl8812au driver installation on my Ubuntu 23.04 machine, it was even more painful to call all those monitor mode toggling commands.. so I wrote this script to do it automatically.

This script allows you to toggle your WiFi interface between monitor mode and managed mode based on the provided arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment