aboutsummaryrefslogtreecommitdiff
path: root/coWifi.sh
blob: 5e17619f670b401b030b6f01daa75ac716111dad (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/env bash

interface="";


function guess_wifi {
    toAll="$1"
    _interface=""
    for i in `ls /sys/class/net/`; do
        if [ -d "/sys/class/net/$i/wireless" ] ; then 
            _interface="$i"
        fi
    done
    if [ -z $interface ] ; then
#        echo 'No Wireless interface found\nExit'
        if [ "$toAll" == "all" ] ; then
#            echo 'flushing all'
            _interface=all
        else
            echo "${_interface}"
            exit 1
        fi
    fi
    echo "${_interface}"
}



if [ "$1" == 'off' ] ; then 
    if [ -z "$2" ] ; then 
        interface=$(guess_wifi '')
    else
        interface=$(guess_wifi 'all')
    fi
    echo 'OFF'
    sudo pkill -f dhcpcd
    sudo pkill -f wpa_supplicant
    if [ "${interface}" = 'all' ] ; then 
        sudo ip route flush all
    else
        sudo ip route flush dev "${interface}"
    fi
    sudo pkill -f ssh
    exit 0
elif [ -z "$1" ] ; then 
    interface=$(guess_wifi '')
else
    interface="$1"
fi



if [ -z "$2" ]
then
    sudo wpa_supplicant -B "-i${interface}" -D wext -c/etc/wpa_supplicant/wpa_supplicant.conf
else
    if [ -z "$3" ]
    then
        sudo iwconfig "${interface}" essid "$2"
    else
        sudo iwconfig "${interface}" ap "$3"
        sudo iwconfig "${interface}" essid "$2"
    fi
fi
sudo dhcpcd "${interface}"