summaryrefslogtreecommitdiffstats
path: root/contrib/netcfg-auto-wireless
blob: e4cf8699521d90cd51708026b8afd88d929dc9ad (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
#! /bin/bash
# Originally contributed by Neuro: http://bbs.archlinux.org/viewtopic.php?pid=278148#p278148

. /usr/lib/network/network.subr
. /usr/lib/network/wireless.subr
. /etc/rc.conf
. /etc/rc.d/functions
# wifi_auto
#   autoconnect wireless interface
#   $1 - wireless interface

wifi_auto()
{
    INTERFACE=$1; RETRIES=6
    stat_busy "Scanning for networks"

    networks="$(list_networks $INTERFACE)"

    if [[ ! "$networks" ]]; then
        stat_append "- No networks available."
        stat_fail
        exit 1
    fi

    while read essid; do
        # awfully long grep that finds a file which has: 
        # CONNECTION=wireless, ESSID=$essid, INTERFACE=$INTERFACE
        profile=$(grep -rlP --exclude=/etc/network.d/last "CONNECTION=\"?wireless\"?(\n|.)*INTERFACE=\"?$INTERFACE\"?(\n|.)*ESSID=\"?$essid\"?" $PROFILE_DIR/|head -n 1)
        if [[ -n "$profile" ]]; then
            break # If we found a profile, use it.
        fi
    done < $networks
    
    # If there's a profile, connect, else fail.
    if [[ -n "$profile" ]]; then
        stat_done
        netcfg2 $(basename $profile)  
        exit $?                                                
    else
        stat_append "- No profiles matched the found networks"
        stat_fail
        exit 1
    fi
}

if [[ $(id -u) -ne 0 ]]; then
    err "This script needs to be run with root priviledges"
    exit 1
fi
if [[ -z $1 ]]; then
    err "Please supply an interface to connect"
    exit 1
fi
wifi_auto $1