summaryrefslogtreecommitdiffstats
path: root/usr/bin/autowifi
blob: 90a584def75c4452b5238ccf905779291dbd563f (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
#!/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 $6;then
        encr=psk2
    elif $5; then
        encr=psk
    elif [ $4 == on ]; then
        encr=wep
    fi
    for hack in $(find /usr/lib/autowifi/ -type f); do
        key=$($hack $@);
        if ! [ $key -eq 1 ];then
            connect_wifi $3 $1 $encr $key
            break
        fi
    done
}
check_internet(){
    wget -O- --spider http://google.de/index.html
}
loop_over_networks(){
    . /tmp/${wifi}.scan
    for i in `seq 1 $WIFI_COUNT`; do
        crack_wifi \${ESSID_${i}} \${MAC_${i}} \${CHANNEL_${i}} \${ENCRYPTION_${i}} \${WPA_${i}} \${WPA2_${i}}
    done

}

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