Wi-Fi Control on macOS
Control and manage Wi-Fi settings across your MacFleet devices using advanced command-line tools and centralized wireless policies. This tutorial covers Wi-Fi power management, connection control, security enforcement, and enterprise-grade wireless network management with comprehensive monitoring and compliance capabilities.
Understanding macOS Wi-Fi Management
macOS provides several command-line tools for Wi-Fi management:
networksetup
- Primary tool for network configuration and controlairport
- Low-level wireless interface management utilityifconfig
- Network interface configuration tool- System Preferences - GUI equivalent for wireless settings
Enterprise Wi-Fi control requires careful consideration of security policies, power management, and compliance requirements.
Basic Wi-Fi Control Commands
Turn Off Wi-Fi
#!/bin/bash
# Basic Wi-Fi disable command
turn_off_wifi_basic() {
# Get the airport interface name
local AIRPORT=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}')
if [[ -n "$AIRPORT" ]]; then
networksetup -setairportpower "$AIRPORT" off
if [[ $? -eq 0 ]]; then
echo "✅ Wi-Fi turned off successfully"
return 0
else
echo "❌ Failed to turn off Wi-Fi"
return 1
fi
else
echo "❌ Wi-Fi interface not found"
return 1
fi
}
# Example usage
turn_off_wifi_basic
Turn On Wi-Fi
#!/bin/bash
# Turn on Wi-Fi with verification
turn_on_wifi() {
local interface_name="$1"
# Auto-detect interface if not provided
if [[ -z "$interface_name" ]]; then
interface_name=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}')
fi
if [[ -z "$interface_name" ]]; then
echo "❌ Wi-Fi interface not found"
return 1
fi
echo "Turning on Wi-Fi interface: $interface_name"
networksetup -setairportpower "$interface_name" on
# Wait for interface to come up
sleep 3
# Verify Wi-Fi is enabled
local wifi_status
wifi_status=$(networksetup -getairportpower "$interface_name" | grep "On")
if [[ -n "$wifi_status" ]]; then
echo "✅ Wi-Fi turned on successfully"
return 0
else
echo "❌ Failed to turn on Wi-Fi"
return 1
fi
}
# Example usage
# turn_on_wifi "en0"
Check Wi-Fi Status
#!/bin/bash
# Check current Wi-Fi status with detailed information
check_wifi_status() {
echo "=== Wi-Fi Status Report ==="
echo "Generated: $(date)"
echo "=========================="
echo ""
# Get all Wi-Fi interfaces
local wifi_interfaces
wifi_interfaces=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}')
if [[ -z "$wifi_interfaces" ]]; then
echo "❌ No Wi-Fi interfaces found"
return 1
fi
for interface in $wifi_interfaces; do
echo "Interface: $interface"
echo "-----------------"
# Power status
local power_status
power_status=$(networksetup -getairportpower "$interface")
echo "Power Status: $power_status"
# Connection status
if echo "$power_status" | grep -q "On"; then
# Get current network
local current_network
current_network=$(networksetup -getairportnetwork "$interface" | cut -d' ' -f4-)
echo "Current Network: $current_network"
# Get IP address if connected
local ip_address
ip_address=$(ifconfig "$interface" | grep "inet " | awk '{print $2}')
if [[ -n "$ip_address" ]]; then
echo "IP Address: $ip_address"
else
echo "IP Address: Not assigned"
fi
# Signal strength (if available)
if command -v airport >/dev/null 2>&1; then
local signal_info
signal_info=$(airport -I | grep "agrCtlRSSI" | awk '{print $2}')
if [[ -n "$signal_info" ]]; then
echo "Signal Strength: $signal_info dBm"
fi
fi
else
echo "Wi-Fi is disabled"
fi
echo ""
done
}
# Execute status check
check_wifi_status
Advanced Wi-Fi Management
Wi-Fi Interface Discovery
#!/bin/bash
# Discover and analyze Wi-Fi interfaces
discover_wifi_interfaces() {
echo "=== Wi-Fi Interface Discovery ==="
echo ""
# List all network hardware
echo "1. ALL NETWORK HARDWARE:"
echo "------------------------"
networksetup -listallhardwareports
echo ""
# Focus on Wi-Fi interfaces
echo "2. WI-FI INTERFACES DETECTED:"
echo "----------------------------"
local wifi_count=0
while IFS= read -r line; do
if echo "$line" | grep -q "Wi-Fi"; then
echo "Hardware Port: $line"
# Get the next line which contains device name
read -r device_line
echo "Device: $device_line"
# Extract device name
local device_name
device_name=$(echo "$device_line" | awk '{print $2}')
# Get MAC address
local mac_address
mac_address=$(ifconfig "$device_name" 2>/dev/null | grep "ether" | awk '{print $2}')
if [[ -n "$mac_address" ]]; then
echo "MAC Address: $mac_address"
fi
# Get current status
local power_status
power_status=$(networksetup -getairportpower "$device_name" 2>/dev/null)
echo "Power Status: $power_status"
echo ""
((wifi_count++))
fi
done < <(networksetup -listallhardwareports)
echo "Total Wi-Fi interfaces found: $wifi_count"
if [[ $wifi_count -eq 0 ]]; then
echo "⚠️ No Wi-Fi interfaces detected"
return 1
fi
return 0
}
# Execute interface discovery
discover_wifi_interfaces
Network Scanning and Analysis
#!/bin/bash
# Scan for available Wi-Fi networks
scan_wifi_networks() {
local interface_name="$1"
local scan_type="${2:-basic}" # basic, detailed, security
# Auto-detect interface if not provided
if [[ -z "$interface_name" ]]; then
interface_name=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}')
fi
if [[ -z "$interface_name" ]]; then
echo "❌ Wi-Fi interface not found"
return 1
fi
echo "=== Wi-Fi Network Scan ==="
echo "Interface: $interface_name"
echo "Scan Type: $scan_type"
echo "========================="
echo ""
# Ensure Wi-Fi is on for scanning
local power_status
power_status=$(networksetup -getairportpower "$interface_name")
if echo "$power_status" | grep -q "Off"; then
echo "Enabling Wi-Fi for scanning..."
networksetup -setairportpower "$interface_name" on
sleep 3
fi
case "$scan_type" in
"basic")
# Basic network list
echo "Available Networks:"
echo "-------------------"
if command -v airport >/dev/null 2>&1; then
airport -s
else
echo "airport command not available, using networksetup scan"
# Note: networksetup doesn't have direct scan command, using airport utility
fi
;;
"detailed")
# Detailed network information
echo "Detailed Network Analysis:"
echo "-------------------------"
if command -v airport >/dev/null 2>&1; then
airport -s | while IFS= read -r line; do
if [[ -n "$line" && ! "$line" =~ ^[[:space:]]*$ ]]; then
echo "Network: $line"
# Extract SSID and signal strength
local ssid signal
ssid=$(echo "$line" | awk '{print $1}')
signal=$(echo "$line" | awk '{print $3}')
if [[ -n "$signal" ]]; then
if [[ "$signal" -gt -50 ]]; then
echo " Signal Quality: Excellent ($signal dBm)"
elif [[ "$signal" -gt -70 ]]; then
echo " Signal Quality: Good ($signal dBm)"
elif [[ "$signal" -gt -80 ]]; then
echo " Signal Quality: Fair ($signal dBm)"
else
echo " Signal Quality: Poor ($signal dBm)"
fi
fi
echo ""
fi
done
fi
;;
"security")
# Security analysis of networks
echo "Security Analysis:"
echo "-----------------"
if command -v airport >/dev/null 2>&1; then
airport -s | grep -E "(WPA|WEP|NONE)" | while IFS= read -r line; do
local ssid security
ssid=$(echo "$line" | awk '{print $1}')
security=$(echo "$line" | grep -o -E "(WPA2|WPA3|WPA|WEP|NONE)")
case "$security" in
"WPA3")
echo "✅ $ssid: Secure (WPA3)"
;;
"WPA2")
echo "✅ $ssid: Secure (WPA2)"
;;
"WPA")
echo "⚠️ $ssid: Moderately Secure (WPA)"
;;
"WEP")
echo "❌ $ssid: Insecure (WEP - Deprecated)"
;;
"NONE"|*)
echo "❌ $ssid: Open Network (No Security)"
;;
esac
done
fi
;;
esac
}
# Example usage
# scan_wifi_networks "en0" "detailed"
Enterprise Wi-Fi Control System
#!/bin/bash
# MacFleet Enterprise Wi-Fi Control System
# Comprehensive wireless management, security enforcement, and compliance monitoring
# Configuration
LOG_FILE="/var/log/macfleet_wifi_control.log"
CONFIG_FILE="/etc/macfleet/wifi_config.conf"
BACKUP_DIR="/var/backups/macfleet/wifi"
POLICY_DIR="/etc/macfleet/wifi_policies"
# Create directory structure
setup_directories() {
mkdir -p "$(dirname "$LOG_FILE")" "$BACKUP_DIR" "$POLICY_DIR" "$(dirname "$CONFIG_FILE")"
touch "$LOG_FILE"
# Set appropriate permissions
chmod 755 "$BACKUP_DIR" "$POLICY_DIR"
}
# Logging function
log_action() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Enterprise Wi-Fi policy enforcement
enforce_wifi_policy() {
local policy_name="$1"
local policy_file="$POLICY_DIR/${policy_name}.policy"
if [[ ! -f "$policy_file" ]]; then
log_action "ERROR: Wi-Fi policy not found: $policy_name"
return 1
fi
log_action "Enforcing Wi-Fi policy: $policy_name"
# Load policy configuration
source "$policy_file"
# Get Wi-Fi interface
local wifi_interface
wifi_interface=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}')
if [[ -z "$wifi_interface" ]]; then
log_action "ERROR: No Wi-Fi interface found"
return 1
fi
# Apply policy settings
case "${WIFI_POLICY_ACTION:-disable}" in
"disable")
networksetup -setairportpower "$wifi_interface" off
log_action "Wi-Fi disabled per policy: $policy_name"
;;
"enable")
networksetup -setairportpower "$wifi_interface" on
log_action "Wi-Fi enabled per policy: $policy_name"
;;
"scheduled")
apply_scheduled_wifi_policy "$wifi_interface" "$policy_name"
;;
"conditional")
apply_conditional_wifi_policy "$wifi_interface" "$policy_name"
;;
*)
log_action "ERROR: Unknown policy action: ${WIFI_POLICY_ACTION}"
return 1
;;
esac
return 0
}
# Scheduled Wi-Fi control
apply_scheduled_wifi_policy() {
local interface="$1"
local policy_name="$2"
local current_hour=$(date +%H)
local current_day=$(date +%u) # 1=Monday, 7=Sunday
log_action "Applying scheduled Wi-Fi policy: $policy_name"
# Load schedule from policy
local enable_hours="${WIFI_ENABLE_HOURS:-09-17}"
local enable_days="${WIFI_ENABLE_DAYS:-1-5}"
# Parse time range
local start_hour end_hour
start_hour=$(echo "$enable_hours" | cut -d'-' -f1)
end_hour=$(echo "$enable_hours" | cut -d'-' -f2)
# Parse day range
local start_day end_day
start_day=$(echo "$enable_days" | cut -d'-' -f1)
end_day=$(echo "$enable_days" | cut -d'-' -f2)
# Check if current time is within allowed window
local time_allowed=false
local day_allowed=false
if [[ "$current_hour" -ge "$start_hour" && "$current_hour" -lt "$end_hour" ]]; then
time_allowed=true
fi
if [[ "$current_day" -ge "$start_day" && "$current_day" -le "$end_day" ]]; then
day_allowed=true
fi
if [[ "$time_allowed" == "true" && "$day_allowed" == "true" ]]; then
networksetup -setairportpower "$interface" on
log_action "Wi-Fi enabled - within scheduled hours"
else
networksetup -setairportpower "$interface" off
log_action "Wi-Fi disabled - outside scheduled hours"
fi
}
# Conditional Wi-Fi control
apply_conditional_wifi_policy() {
local interface="$1"
local policy_name="$2"
log_action "Applying conditional Wi-Fi policy: $policy_name"
# Check battery level
local battery_level
battery_level=$(pmset -g batt | grep -o '[0-9]*%' | tr -d '%')
# Check power source
local power_source
power_source=$(pmset -g ps | head -1 | grep -o "'.*'" | tr -d "'")
# Check ethernet connection
local ethernet_connected=false
local ethernet_interfaces
ethernet_interfaces=$(networksetup -listallhardwareports | grep -A 1 Ethernet | grep Device | awk '{print $2}')
for eth_interface in $ethernet_interfaces; do
local eth_status
eth_status=$(ifconfig "$eth_interface" 2>/dev/null | grep "status: active")
if [[ -n "$eth_status" ]]; then
ethernet_connected=true
break
fi
done
# Apply conditional logic
local should_enable_wifi=true
# Disable Wi-Fi if ethernet is connected and policy requires it
if [[ "$ethernet_connected" == "true" && "${DISABLE_WIFI_WITH_ETHERNET:-false}" == "true" ]]; then
should_enable_wifi=false
log_action "Wi-Fi disabled - Ethernet connection detected"
fi
# Disable Wi-Fi if battery is low and policy requires it
if [[ -n "$battery_level" && "$battery_level" -lt "${MIN_BATTERY_FOR_WIFI:-20}" ]]; then
should_enable_wifi=false
log_action "Wi-Fi disabled - Low battery level: $battery_level%"
fi
# Enable/disable Wi-Fi based on conditions
if [[ "$should_enable_wifi" == "true" ]]; then
networksetup -setairportpower "$interface" on
log_action "Wi-Fi enabled - Conditions met"
else
networksetup -setairportpower "$interface" off
log_action "Wi-Fi disabled - Conditions not met"
fi
}
# Security compliance check
check_wifi_security_compliance() {
log_action "Performing Wi-Fi security compliance check"
local compliance_issues=()
local wifi_interface
wifi_interface=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}')
if [[ -z "$wifi_interface" ]]; then
log_action "ERROR: No Wi-Fi interface found for compliance check"
return 1
fi
# Check if Wi-Fi is enabled when it should be disabled
local power_status
power_status=$(networksetup -getairportpower "$wifi_interface")
if echo "$power_status" | grep -q "On"; then
# Wi-Fi is enabled, check current network security
local current_network
current_network=$(networksetup -getairportnetwork "$wifi_interface" | cut -d' ' -f4-)
if [[ "$current_network" != "You are not associated with an AirPort network." ]]; then
log_action "Connected to network: $current_network"
# Check if network is in approved list
local approved_networks_file="$POLICY_DIR/approved_networks.list"
if [[ -f "$approved_networks_file" ]]; then
if ! grep -q "^$current_network$" "$approved_networks_file"; then
compliance_issues+=("Connected to non-approved network: $current_network")
fi
fi
# Check for open networks
if command -v airport >/dev/null 2>&1; then
local network_security
network_security=$(airport -I | grep "Security")
if echo "$network_security" | grep -q "none"; then
compliance_issues+=("Connected to open/unsecured network")
fi
fi
fi
fi
# Check for saved insecure networks
local network_profiles
network_profiles=$(networksetup -listpreferredwirelessnetworks "$wifi_interface" 2>/dev/null | tail -n +2)
while IFS= read -r network; do
if [[ -n "$network" ]]; then
# Clean network name
network=$(echo "$network" | sed 's/^[[:space:]]*//')
# Check against blacklist
local blacklist_file="$POLICY_DIR/blacklisted_networks.list"
if [[ -f "$blacklist_file" ]]; then
if grep -q "^$network$" "$blacklist_file"; then
compliance_issues+=("Blacklisted network in saved profiles: $network")
fi
fi
fi
done <<< "$network_profiles"
# Generate compliance report
if [[ ${#compliance_issues[@]} -eq 0 ]]; then
log_action "✅ Wi-Fi security compliance check passed"
return 0
else
log_action "❌ Wi-Fi security compliance violations found:"
for issue in "${compliance_issues[@]}"; do
log_action " - $issue"
done
return 1
fi
}
# Network profile management
manage_wifi_profiles() {
local action="$1"
local network_name="$2"
local wifi_interface
wifi_interface=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}')
if [[ -z "$wifi_interface" ]]; then
log_action "ERROR: No Wi-Fi interface found"
return 1
fi
case "$action" in
"list")
log_action "Listing saved Wi-Fi profiles"
echo "=== Saved Wi-Fi Profiles ==="
networksetup -listpreferredwirelessnetworks "$wifi_interface"
;;
"remove")
if [[ -z "$network_name" ]]; then
log_action "ERROR: Network name required for removal"
return 1
fi
log_action "Removing Wi-Fi profile: $network_name"
networksetup -removepreferredwirelessnetwork "$wifi_interface" "$network_name"
if [[ $? -eq 0 ]]; then
log_action "✅ Wi-Fi profile removed: $network_name"
else
log_action "❌ Failed to remove Wi-Fi profile: $network_name"
return 1
fi
;;
"removeall")
log_action "Removing all saved Wi-Fi profiles"
# Get list of networks and remove each one
local networks
networks=$(networksetup -listpreferredwirelessnetworks "$wifi_interface" 2>/dev/null | tail -n +2)
while IFS= read -r network; do
if [[ -n "$network" ]]; then
network=$(echo "$network" | sed 's/^[[:space:]]*//')
networksetup -removepreferredwirelessnetwork "$wifi_interface" "$network"
log_action "Removed profile: $network"
fi
done <<< "$networks"
log_action "✅ All Wi-Fi profiles removed"
;;
*)
log_action "ERROR: Invalid action for profile management: $action"
return 1
;;
esac
}
# Power management optimization
optimize_wifi_power() {
local optimization_level="${1:-balanced}" # aggressive, balanced, conservative
log_action "Applying Wi-Fi power optimization: $optimization_level"
local wifi_interface
wifi_interface=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}')
if [[ -z "$wifi_interface" ]]; then
log_action "ERROR: No Wi-Fi interface found"
return 1
fi
case "$optimization_level" in
"aggressive")
# Maximum power savings
log_action "Applying aggressive power optimization"
# Disable Wi-Fi when ethernet is connected
local ethernet_connected=false
local ethernet_interfaces
ethernet_interfaces=$(networksetup -listallhardwareports | grep -A 1 Ethernet | grep Device | awk '{print $2}')
for eth_interface in $ethernet_interfaces; do
local eth_status
eth_status=$(ifconfig "$eth_interface" 2>/dev/null | grep "status: active")
if [[ -n "$eth_status" ]]; then
ethernet_connected=true
break
fi
done
if [[ "$ethernet_connected" == "true" ]]; then
networksetup -setairportpower "$wifi_interface" off
log_action "Wi-Fi disabled - Ethernet connection detected"
fi
;;
"balanced")
# Moderate power savings
log_action "Applying balanced power optimization"
# Check battery level and disable if very low
local battery_level
battery_level=$(pmset -g batt | grep -o '[0-9]*%' | tr -d '%')
if [[ -n "$battery_level" && "$battery_level" -lt 10 ]]; then
networksetup -setairportpower "$wifi_interface" off
log_action "Wi-Fi disabled - Critical battery level: $battery_level%"
fi
;;
"conservative")
# Minimal power optimization
log_action "Applying conservative power optimization"
# Only disable in extreme circumstances
local battery_level
battery_level=$(pmset -g batt | grep -o '[0-9]*%' | tr -d '%')
if [[ -n "$battery_level" && "$battery_level" -lt 5 ]]; then
networksetup -setairportpower "$wifi_interface" off
log_action "Wi-Fi disabled - Emergency battery level: $battery_level%"
fi
;;
*)
log_action "ERROR: Invalid optimization level: $optimization_level"
return 1
;;
esac
return 0
}
# Generate Wi-Fi management report
generate_wifi_report() {
local report_file="$BACKUP_DIR/wifi_management_report_$(date +%Y%m%d_%H%M%S).json"
log_action "Generating Wi-Fi management report: $report_file"
local wifi_interface
wifi_interface=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}')
{
echo "{"
echo " \"report_type\": \"wifi_management\","
echo " \"timestamp\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\","
echo " \"hostname\": \"$(hostname)\","
echo " \"system_info\": {"
echo " \"macos_version\": \"$(sw_vers -productVersion)\","
echo " \"user\": \"$(whoami)\""
echo " },"
if [[ -n "$wifi_interface" ]]; then
local power_status current_network ip_address
power_status=$(networksetup -getairportpower "$wifi_interface" | grep -o "On\|Off")
current_network=$(networksetup -getairportnetwork "$wifi_interface" | cut -d' ' -f4-)
ip_address=$(ifconfig "$wifi_interface" | grep "inet " | awk '{print $2}')
echo " \"wifi_status\": {"
echo " \"interface\": \"$wifi_interface\","
echo " \"power_status\": \"$power_status\","
echo " \"current_network\": \"$current_network\","
echo " \"ip_address\": \"${ip_address:-null}\""
echo " },"
else
echo " \"wifi_status\": {"
echo " \"interface\": null,"
echo " \"power_status\": \"unavailable\","
echo " \"current_network\": null,"
echo " \"ip_address\": null"
echo " },"
fi
# Battery and power information
local battery_level power_source
battery_level=$(pmset -g batt | grep -o '[0-9]*%' | tr -d '%')
power_source=$(pmset -g ps | head -1 | grep -o "'.*'" | tr -d "'")
echo " \"power_info\": {"
echo " \"battery_level\": ${battery_level:-null},"
echo " \"power_source\": \"${power_source:-unknown}\""
echo " },"
# Network profiles
echo " \"saved_profiles\": ["
if [[ -n "$wifi_interface" ]]; then
local first_profile=true
networksetup -listpreferredwirelessnetworks "$wifi_interface" 2>/dev/null | tail -n +2 | while IFS= read -r network; do
if [[ -n "$network" ]]; then
network=$(echo "$network" | sed 's/^[[:space:]]*//')
if [[ "$first_profile" == "false" ]]; then
echo ","
fi
first_profile=false
echo -n " \"$network\""
fi
done
fi
echo ""
echo " ]"
echo "}"
} > "$report_file"
log_action "Wi-Fi management report generated: $report_file"
echo "$report_file"
}
# Main management function
main() {
local action="${1:-status}"
local parameter1="$2"
local parameter2="$3"
setup_directories
log_action "MacFleet Wi-Fi Control started with action: $action"
case "$action" in
"disable"|"off")
turn_off_wifi_basic
;;
"enable"|"on")
turn_on_wifi "$parameter1"
;;
"status")
check_wifi_status
;;
"scan")
scan_wifi_networks "$parameter1" "$parameter2"
;;
"discover")
discover_wifi_interfaces
;;
"policy")
enforce_wifi_policy "$parameter1"
;;
"compliance")
check_wifi_security_compliance
;;
"profiles")
manage_wifi_profiles "$parameter1" "$parameter2"
;;
"optimize")
optimize_wifi_power "$parameter1"
;;
"report")
generate_wifi_report
;;
*)
check_wifi_status
;;
esac
log_action "MacFleet Wi-Fi Control completed with action: $action"
}
# Execute main function with all arguments
main "$@"
Wi-Fi Policy Templates
Corporate Wi-Fi Policy Configuration
# /etc/macfleet/wifi_policies/corporate_standard.policy
# MacFleet Corporate Wi-Fi Policy
# Policy action: disable, enable, scheduled, conditional
WIFI_POLICY_ACTION="conditional"
# Scheduled settings (for scheduled action)
WIFI_ENABLE_HOURS="08-18" # 8 AM to 6 PM
WIFI_ENABLE_DAYS="1-5" # Monday to Friday
# Conditional settings
DISABLE_WIFI_WITH_ETHERNET="true"
MIN_BATTERY_FOR_WIFI="15"
# Security settings
REQUIRE_WPA2_MINIMUM="true"
BLOCK_OPEN_NETWORKS="true"
AUTO_CONNECT_CORPORATE_ONLY="true"
# Power management
POWER_OPTIMIZATION_LEVEL="balanced"
AUTO_DISABLE_IDLE="true"
IDLE_TIMEOUT_MINUTES="30"
Security-Focused Wi-Fi Policy
# /etc/macfleet/wifi_policies/high_security.policy
# MacFleet High Security Wi-Fi Policy
WIFI_POLICY_ACTION="conditional"
# Security requirements
REQUIRE_WPA3_MINIMUM="true"
BLOCK_OPEN_NETWORKS="true"
BLOCK_WEP_NETWORKS="true"
APPROVED_NETWORKS_ONLY="true"
# Conditional disabling
DISABLE_WIFI_WITH_ETHERNET="true"
DISABLE_WIFI_IN_SECURE_AREAS="true"
MIN_BATTERY_FOR_WIFI="25"
# Monitoring
ENABLE_CONNECTION_LOGGING="true"
ALERT_ON_UNAPPROVED_NETWORKS="true"
SCAN_INTERVAL_MINUTES="5"
Power Conservation Policy
# /etc/macfleet/wifi_policies/power_saver.policy
# MacFleet Power Conservation Wi-Fi Policy
WIFI_POLICY_ACTION="conditional"
# Aggressive power saving
DISABLE_WIFI_WITH_ETHERNET="true"
MIN_BATTERY_FOR_WIFI="30"
POWER_OPTIMIZATION_LEVEL="aggressive"
# Scheduled disabling for night hours
WIFI_ENABLE_HOURS="07-22" # 7 AM to 10 PM
WIFI_ENABLE_DAYS="1-7" # All days
# Auto-disable features
AUTO_DISABLE_IDLE="true"
IDLE_TIMEOUT_MINUTES="15"
DISABLE_BACKGROUND_SCANNING="true"
Security and Compliance Functions
Network Whitelist Management
#!/bin/bash
# Manage approved Wi-Fi networks
manage_network_whitelist() {
local action="$1"
local network_name="$2"
local whitelist_file="$POLICY_DIR/approved_networks.list"
case "$action" in
"add")
if [[ -z "$network_name" ]]; then
echo "ERROR: Network name required"
return 1
fi
# Add network to whitelist
if ! grep -q "^$network_name$" "$whitelist_file" 2>/dev/null; then
echo "$network_name" >> "$whitelist_file"
log_action "Added network to whitelist: $network_name"
else
log_action "Network already in whitelist: $network_name"
fi
;;
"remove")
if [[ -z "$network_name" ]]; then
echo "ERROR: Network name required"
return 1
fi
# Remove network from whitelist
if [[ -f "$whitelist_file" ]]; then
grep -v "^$network_name$" "$whitelist_file" > "${whitelist_file}.tmp"
mv "${whitelist_file}.tmp" "$whitelist_file"
log_action "Removed network from whitelist: $network_name"
fi
;;
"list")
echo "=== Approved Networks Whitelist ==="
if [[ -f "$whitelist_file" ]]; then
cat "$whitelist_file"
else
echo "No approved networks configured"
fi
;;
"enforce")
enforce_network_whitelist
;;
*)
echo "ERROR: Invalid whitelist action: $action"
return 1
;;
esac
}
# Enforce network whitelist
enforce_network_whitelist() {
local wifi_interface
wifi_interface=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}')
if [[ -z "$wifi_interface" ]]; then
log_action "ERROR: No Wi-Fi interface found"
return 1
fi
# Get current network
local current_network
current_network=$(networksetup -getairportnetwork "$wifi_interface" | cut -d' ' -f4-)
if [[ "$current_network" == "You are not associated with an AirPort network." ]]; then
log_action "No active Wi-Fi connection"
return 0
fi
# Check if current network is approved
local whitelist_file="$POLICY_DIR/approved_networks.list"
if [[ -f "$whitelist_file" ]]; then
if grep -q "^$current_network$" "$whitelist_file"; then
log_action "✅ Connected to approved network: $current_network"
return 0
else
log_action "❌ Connected to non-approved network: $current_network"
# Disconnect from non-approved network
networksetup -setairportpower "$wifi_interface" off
sleep 2
networksetup -setairportpower "$wifi_interface" on
log_action "Disconnected from non-approved network"
return 1
fi
else
log_action "⚠️ No whitelist configured - allowing all networks"
return 0
fi
}
# Example usage
# manage_network_whitelist "add" "CorporateWiFi"
# manage_network_whitelist "list"
# manage_network_whitelist "enforce"
Wi-Fi Audit and Monitoring
#!/bin/bash
# Comprehensive Wi-Fi audit
audit_wifi_configuration() {
local audit_file="$BACKUP_DIR/wifi_audit_$(date +%Y%m%d_%H%M%S).txt"
log_action "Performing comprehensive Wi-Fi audit: $audit_file"
{
echo "MacFleet Wi-Fi Configuration Audit"
echo "=================================="
echo "Generated: $(date)"
echo "Hostname: $(hostname)"
echo "Auditor: $(whoami)"
echo ""
# Wi-Fi interface information
echo "WI-FI INTERFACE ANALYSIS:"
echo "------------------------"
discover_wifi_interfaces
echo ""
# Current connection status
echo "CONNECTION STATUS:"
echo "-----------------"
check_wifi_status
echo ""
# Security compliance
echo "SECURITY COMPLIANCE:"
echo "-------------------"
if check_wifi_security_compliance; then
echo "✅ Security compliance check passed"
else
echo "❌ Security compliance violations detected"
fi
echo ""
# Saved network profiles
echo "SAVED NETWORK PROFILES:"
echo "----------------------"
manage_wifi_profiles "list"
echo ""
# Policy enforcement status
echo "POLICY ENFORCEMENT:"
echo "------------------"
local policy_files
policy_files=$(ls "$POLICY_DIR"/*.policy 2>/dev/null)
if [[ -n "$policy_files" ]]; then
for policy_file in $policy_files; do
local policy_name
policy_name=$(basename "$policy_file" .policy)
echo "Policy: $policy_name"
echo " File: $policy_file"
echo " Status: $(if [[ -f "$policy_file" ]]; then echo "Active"; else echo "Inactive"; fi)"
done
else
echo "No policies configured"
fi
echo ""
# Power management status
echo "POWER MANAGEMENT:"
echo "----------------"
local battery_level power_source
battery_level=$(pmset -g batt | grep -o '[0-9]*%' | tr -d '%')
power_source=$(pmset -g ps | head -1 | grep -o "'.*'" | tr -d "'")
echo "Battery Level: ${battery_level:-Unknown}%"
echo "Power Source: ${power_source:-Unknown}"
echo ""
# Recommendations
echo "SECURITY RECOMMENDATIONS:"
echo "------------------------"
echo "• Implement approved network whitelist"
echo "• Enable automatic disconnection from open networks"
echo "• Configure scheduled Wi-Fi disable during off-hours"
echo "• Implement battery-based power management"
echo "• Regular audit of saved network profiles"
echo "• Monitor for connections to unapproved networks"
} > "$audit_file"
log_action "Wi-Fi audit completed: $audit_file"
echo "$audit_file"
}
audit_wifi_configuration
Important Technical Notes
NetworkSetup Command Reference
networksetup -setairportpower <device> on|off
: Enable/disable Wi-Finetworksetup -getairportpower <device>
: Check Wi-Fi power statusnetworksetup -getairportnetwork <device>
: Get current Wi-Fi networknetworksetup -listpreferredwirelessnetworks <device>
: List saved networksnetworksetup -removepreferredwirelessnetwork <device> <network>
: Remove saved network
Security Considerations
- Admin Privileges: Many network commands require admin access
- Network Isolation: Ensure proper network segmentation
- Profile Management: Regularly audit and clean saved network profiles
- Open Network Protection: Block connections to unsecured networks
- Compliance Monitoring: Implement continuous security compliance checking
Power Management Best Practices
- Battery Optimization: Disable Wi-Fi when battery is critically low
- Ethernet Priority: Prefer wired connections when available
- Scheduled Control: Implement time-based Wi-Fi policies
- Idle Management: Disable Wi-Fi during extended idle periods
- Resource Monitoring: Track power consumption patterns
Enterprise Use Cases
- Security-First Environments: Strict network whitelisting and compliance
- Power-Conscious Deployments: Battery optimization for mobile workers
- Scheduled Operations: Time-based Wi-Fi control for different work shifts
- Compliance Requirements: Audit trails and security monitoring
- Fleet Standardization: Consistent Wi-Fi policies across all devices
Remember to test all scripts on individual devices before deploying across your MacFleet environment, and ensure compliance with corporate security policies when implementing enterprise Wi-Fi control systems.