aboutsummaryrefslogtreecommitdiff
path: root/linux-bin/wpa
blob: 0ffbc1439f20b22baa9712f5b195b0cda139b7ca (plain) (blame)
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
#!/bin/sh
set -e
# manual configuration of wifi made a little bit easier with wpa_supplicant
session="wpa"
t=3
interface=
nwid=
wpakey=

help() {
cat <<EOF
$0 [-i <interface>] [-n <network_name>] [-p <password>]
EOF
exit 1;
}

while [ $# -gt 0 ] ; do case $1 in
	-i|if) interface="$2"; shift ; shift ;;
	-n|nwid) nwid="$2"; shift ; shift ;;
	-p|wpakey) wpakey="$2"; shift ; shift ;;
	*) help ;;
esac ; done

err=0
for _v in interface nwid wpakey ; do
	eval v="\$$_v";
	if [ -z "$v" ] ; then
		echo "$_v cannot be empty"
		err=1
	fi
done
if [ $err -eq 1 ] ; then exit 1 ; fi


wpakey="$(echo "$wpakey" | sed -e's/"/\\"/g')"

conf_f="$(mktemp /dev/shm/wpa.XXXX)"
trap 'rm -f $conf_f; exit 1;' EXIT INT
cat > "$conf_f" <<EOF
network={
	ssid="$nwid"
	scan_ssid=1
	key_mgmt=WPA-PSK
	psk="$wpakey"
}
EOF


if tmux ls -F '#{session_name}' | grep -q "^$session\$" ; then
	exec tmux att -t "$session"
fi

tmux new-session -s "$session" \;\
	send-keys -t "${session}:0" "sudo wpa_supplicant -i \"$interface\" -c \"$conf_f\"" \;\
	send-keys -t "${session}:0" Enter \;\
	new-window -n "dhclient" -t 1 \;\
	send-keys -t "${session}:1" "sleep $t ; sudo dhclient -d -i \"$interface\"" \;\
	send-keys -t "${session}:1" Enter \;\
	select-window -t "${session}:0" \;\