-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwlctl
executable file
·108 lines (85 loc) · 2.46 KB
/
wlctl
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
#!/bin/sh
ACTION=$1
NAME_PATTERN=$2
function help {
cat << EOM
wlctl - a simple wrapper for connmanctl to enable/disable Wi-Fi,
and to connect to / disconnect from a Wi-Fi network
Written by Giorgi Gzirishvili <drn+contact.tools.asteroidos.wlctl@disroot.org>
This code is licensed under the MIT License.
USAGE
wlctl ACTION [NAME_PATTERN]
Actions:
enable Enable Wi-Fi.
disable Disable Wi-Fi.
connect [NAME_PATTERN] Connect to a Wi-Fi network. You can specify a
unique name pattern in order to connect to a
specific network.
disconnect [NAME_PATTERN] Disonnect from a Wi-Fi network. You can specify
a unique name pattern in order to disconnect
from a specific network.
status Get connection and hardware status.
help Get this help.
EOM
}
# === BEGIN Utils ===
function blank_line {
echo ''
}
function scan_networks {
echo 'Scanning...'
connmanctl scan wifi >/dev/null
blank_line
}
function list_networks {
echo 'Available networks:'
connmanctl services
blank_line
}
function select_network {
if [ -z $NAME_PATTERN ]; then
echo -ne "Enter network name or regex pattern to match:\n> "
read NAME_PATTERN
blank_line
fi
SELECTED_NETWORK=$(connmanctl services | grep -i "$NAME_PATTERN" | \
head -n 1 | grep -io 'wifi_[^ ]*')
}
# ==== END Utils ====
function wifi_state {
connmanctl $1 wifi
blank_line
}
function network_action {
wifi_state enable 1>/dev/null 2>/dev/null
sleep 0.25
scan_networks
list_networks
select_network
connmanctl $1 $SELECTED_NETWORK
blank_line
}
function status {
echo 'Connection:'
connmanctl state
blank_line
WIFI_STATUS=$(connmanctl technologies | grep -A 6 'technology/wifi' | \
tail -n 4)
echo 'Wi-Fi:'
echo "$WIFI_STATUS"
blank_line
if [ -n "$(echo -e "$WIFI_STATUS" | grep Powered | grep True)" ]; then
scan_networks
list_networks
fi
}
case $ACTION in
enable) wifi_state enable ;;
disable) wifi_state disable ;;
connect) network_action connect ;;
disconnect) network_action disconnect ;;
status) status ;;
help) help ;;
'') echo "No action specified. Run \`wlctl help\` for usage information." ;;
*) echo "Invalid action: '$ACTION'. Run \`wlctl help\` for usage information." ;;
esac