-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi
executable file
·126 lines (116 loc) · 2.88 KB
/
wifi
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env zsh
# constants
# ---------
wifi_config="/etc/wpa_supplicant/wifi_networks"
# wifi_config="$DOTFILES$wifi_file"
wifi_card="$WIFI_CARD"
# reconnect using wpa_supplicant
# ------------------------------
wifi_connect() {
wifi_disconnect
sudo wpa_supplicant -B -Dnl80211 -i"$wifi_card" -c"$wifi_config"
sudo dhcpcd "$wifi_card"
}
# disconnect
# ----------
wifi_disconnect() {
sudo pkill wpa_supplicant
sudo pkill dhcpcd
}
# scan for networks in area
# -------------------------
wifi_scan() {
sudo iw dev "$wifi_card" scan \
| grep 'SSID:' \
| sed -e 's/^[ \t]*SSID:[ \t]*//' \
| grep -v '\x00'
}
# get network status
# ------------------
wifi_list() {
local net_card="$1"
[[ -z "$net_card" ]] && net_card="$wifi_card"
ip route | grep 'default' | cut --delimiter=' ' --fields='5,8' \
| case "$net_card" in
first)
head -n1
;;
second)
head -n2 | tail -n1
;;
*)
grep "$net_card"
;;
esac \
| cut --delimiter=' ' --field='2'
}
# create new network connection
# -----------------------------
wifi_new() {
local ssid="$1"
[[ -z "$ssid" ]] && return 1
echo "$ssid"
printf "[o]pen or [c]losed network: "
read network_type
echo
echo "Outputting to '$wifi_config'..."
case "$network_type" in
o*|O*)
echo '\n' | sudo tee -a "$wifi_config"
echo 'network={' | sudo tee -a "$wifi_config"
echo ' ssid="'"$ssid"'"' | sudo tee -a "$wifi_config"
echo ' key_mgmt=NONE' | sudo tee -a "$wifi_config"
echo '}' | sudo tee -a "$wifi_config"
echo '\n' | sudo tee -a "$wifi_config"
;;
c*|C*)
echo "enter passphrase..."
wpa_passphrase "$ssid" | sudo tee -a "$wifi_config"
;;
*)
echo "Unrecognized network type: '$network_type'"
;;
esac
}
# nice interactive wifi-finder
# ----------------------------
wifi_menu() {
sudo ip link set "$wifi_card" up
local aps=$(wifi_scan)
wifi_new "$(echo $aps | sort -r | uniq | sk)" $@
}
# edit the wifi networks file
# ---------------------------
wifi_edit() {
sudo -E "$EDITOR" "$wifi_config"
}
# interface
# ---------
wifi_command="$1"
shift
case "$wifi_command" in
c|connect)
wifi_connect $@
;;
d|disconnect)
wifi_disconnect $@
;;
e|edit)
wifi_edit $@
;;
s|scan)
wifi_scan $@
;;
n|new)
wifi_new $@
;;
m|menu)
wifi_menu $@
;;
l|list)
wifi_list $@
;;
*)
echo "Unrecognized command: '$wifi_command'"
;;
esac