summaryrefslogtreecommitdiffstats
path: root/usr/bin/autowifi
blob: a133b3c364e374a31732bed622d051c179d9990c (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh -x
wifi=wlan0
iface=@wifi-iface[0]
radio=$(uci get wireless.${iface}.device)
connect_wifi(){
    # channel ssid encryption key
    uci set wireless.${iface}.mode=sta

    ifconfig $wifi up
    uci set wireless.${radio}.channel=$1
    uci set "wireless.${iface}.ssid=$2"
    if [ $3 == "none" ] ; then
        uci set wireless.${iface}.encryption=none
        uci -q delete wireless.${iface}.key
    else
        uci set "wireless.${iface}.key=$4"
        uci set wireless.${iface}.encryption=$3
    fi 
    uci commit wireless
    wifi up
}

iwlist_scan(){
    count=0
    
    iwlist scan ${1:-} 2>/dev/null | ( while read line;
    do
        case "$line" in
            *"Cell "*)
                #echo
                : $((count+=1))
                echo MAC_${count}="${line#*Address: }"
                ;;
            *Channel:*)
                echo CHANNEL_${count}="${line#*:}"
                ;;
            *Quality=*)
                echo QUALITY_${count}="`printf '%s' ${line#*Quality=} | cut -d/  -f 1`"
                ;;
            *"Encryption key:"*)
                echo ENCRYPTION_${count}="${line#*key:}"
                ;;
            *ESSID:*)
                echo ESSID_${count}="${line#*ESSID:}"
                ;;
            *"IE: IEEE 802.11i/WPA2"*)
                echo WPA2_${count}=1
                ;;
            *"IE: WPA Version 1"*)
                echo WPA_${count}=1
                ;;
            *);;
        esac
    done; echo WIFI_COUNT=$count)
}
crack_wifi(){
    if [ -n "$6" ];then
        encr=psk2
    elif [ -n "$5" ]; then
        encr=psk
    elif [ "$4" == on ]; then
        encr=wep
    fi
    for hack in $(find /usr/lib/autowifi/ -type f); do
        key=$($hack $@);
        ret=$?
        if [ $ret -eq 0 ];then
            connect_wifi $3 $1 $encr $key
            sleep 20
            if check_gateway; then
                (cat /etc/autowifi/wifi_keys | grep -v "$1|$2|" ; echo "$1|$2|$key" ) | sort | uniq  >> /etc/autowifi/wifi_keys
                echo "yay internet"
                return 0
            fi
        fi
    done
}
check_gateway(){
    ping -c 1 -w 5 $(ip route | awk '/default/{print $3}')
}
check_internet(){
    wget -O- --spider http://google.de/index.html
}
loop_over_networks(){
    . /tmp/${wifi}.scan
    for i in `seq 1 $WIFI_COUNT`; do
        eval crack_wifi \${ESSID_${i}} \${MAC_${i}} \${CHANNEL_${i}} \${ENCRYPTION_${i}} \${WPA_${i}} \${WPA2_${i}}
    done

}

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