summaryrefslogtreecommitdiffstats
path: root/usr/bin/autowifi
blob: 7b92eb9bd280227db080420ea76ac81b490cd12b (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
62
63
64
65
#!/bin/sh -x
# States (LED):
#   Blinking   - running wifi plugins
#   Turned off - connected to wifi
#   Turned on  - waiting for next scan round

wifi=wlan0
iface=@wifi-iface[0]
radio=$(uci get wireless.${iface}.device)

# for connect_wifi
. /usr/lib/autowifi/lib/openwrt

# for iwlist_scan
. /usr/lib/autowifi/lib/iwlist

# for check_internet and check_gateway
. /usr/lib/autowifi/lib/network

crack_wifi(){
    #SSID MAC CHANNEL ENCRYPTION WPA WPA2
    all_led timer
    if [ "$4" == off ];then
        encr=open
    elif [ "$6" -eq 1 ]; then
        encr=psk2
    elif [ "$5" -eq 1 ]; then
        encr=psk
    elif [ "$4" == on ]; then
        encr=wep
    fi
    for hack in $(find /usr/lib/autowifi/plugins -type f); do
        key=$($hack "$@");
        ret=$?
        if [ $ret -eq 0 ];then
            connect_wifi "$3" "$1" $encr "$key"
            sleep 5
            if check_gateway; then
                (cat /etc/autowifi/wifi_keys | grep -v "$1|$2|" ; echo "$1|$2|$key" ) | sort | uniq  > /etc/autowifi/wifi_keys2
                mv /etc/autowifi/wifi_keys2 /etc/autowifi/wifi_keys
                echo "yay gateway"
                check_gateway && all_led none && return 0
            fi
        fi
    done
    return 1
}
loop_over_networks(){
    . /tmp/${wifi}.scan
    for i in `seq 1 $WIFI_COUNT`; do
        eval grep -q \${MAC_${i}} /etc/autowifi/blacklist && continue
        eval crack_wifi "\${ESSID_${i}}" \${MAC_${i}} \${CHANNEL_${i}} \${ENCRYPTION_${i}} \${WPA_${i}} \${WPA2_${i}} && break
    done
}

iwlist_scan > /tmp/${wifi}.scan
loop_over_networks

while sleep 60; do
    if ! check_internet; then
        all_led default-on
        iwlist_scan > /tmp/${wifi}.scan
        loop_over_networks
    fi
done