~mil/sxmo-utils

ref: a5799bf3510d99513414d1bcd96279aa50a9e20a sxmo-utils/scripts/core/sxmo_networks.sh -rwxr-xr-x 2.9 KiB
a5799bf3Stacy Harper Add a check before toggeling wm a month ago
                                                                                
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env sh

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"

connections() {
	ACTIVE="$(nmcli -c no -t c show --active | cut -d: -f1,3 | sed "s/$/ $icon_chk/")"
	INACTIVE="$(nmcli -c no -t c show | cut -d: -f1,3)"
	printf %b "$ACTIVE\n$INACTIVE" | sort -u -t: -k1,1
}

toggleconnection() {
	CONNLINE="$1"
	CONNNAME="$(echo "$CHOICE" | cut -d: -f1)"
	if echo "$CONNLINE" | grep "$icon_chk"; then
		RES="$(nmcli c down "$CONNNAME" 2>&1)"
	else
		RES="$(nmcli c up "$CONNNAME" 2>&1)"
	fi
	notify-send "$RES"
}

deletenetworkmenu() {
	CHOICE="$(
		printf %b "Close Menu\n$(connections)" |
			dmenu -p "Delete Network"
	)"
	if [ "$CHOICE" = "Close Menu" ]; then
		return
	else
		CONNNAME="$(echo "$CHOICE" | cut -d: -f1)"
		RES="$(nmcli c delete "$CONNNAME" 2>&1)"
		notify-send "$RES"
	fi
}

getifname() {
	IFTYPE="$1"
	IFNAME="$(nmcli d | grep -m 1 "$IFTYPE" | cut -d' ' -f1)"
	[ -z "$IFNAME" ] && notify-send "No interface with type $IFTYPE found" && IFNAME=lo
	echo "$IFNAME"
}

addnetworkgsmmenu() {
	CONNNAME="$(
		echo "Close Menu" |
			sxmo_dmenu_with_kb.sh -p "Add GSM: Alias"
	)"
	[ "$CONNNAME" = "Close Menu" ] && return

	APN="$(
		echo "Close Menu" |
			sxmo_dmenu_with_kb.sh -p "Add GSM: APN"
	)"
	[ "$APN" = "Close Menu" ] && return

	# TODO: Support gsm bearer username & password
	nmcli c add \
		type gsm \
		ifname "$(getifname gsm)" \
		con-name "$CONNNAME" \
		apn "$APN"
}

addnetworkwpamenu() {
	SSID="$(
		nmcli d wifi list | tail -n +2 | grep -v '^\*' | awk -F'  ' '{ print $6 }' | grep -v '\-\-' |
		xargs -0 printf 'Close Menu\n%s' |
		sxmo_dmenu_with_kb.sh -p "Add WPA: SSID"
	)"
	[ "$SSID" = "Close Menu" ] && return

	PASSPHRASE="$(
		echo "Close Menu" |
			sxmo_dmenu_with_kb.sh -p "Add WPA: Passphrase"
	)"
	[ "$PASSPHRASE" = "Close Menu" ] && return

	nmcli c add \
		type wifi \
		ifname wlan0 \
		con-name "$SSID" \
		802-11-wireless-security.key-mgmt wpa-psk \
		ssid "$SSID" \
		802-11-wireless-security.psk "$PASSPHRASE"
}


networksmenu() {
	while true; do
		CHOICE="$(
			printf %b "
				$(connections)
				Add a GSM Network
				Add a WPA Network
				Add a Hotspot
				Delete a Network
				Launch Nmtui
				Launch Ifconfig
				Scan Wifi Networks
				System Menu
				Close Menu
			" |
			awk '{$1=$1};1' | grep '\w' | dmenu -p 'Networks'
		)"
		case "$CHOICE" in
			"System Menu" )
				sxmo_appmenu.sh sys && exit
				;;
			"Close Menu" )
				exit
				;;
			"Add a GSM Network" )
				addnetworkgsmmenu
				;;
			"Add a WPA Network" )
				addnetworkwpamenu
				;;
			"Add a Hotspot" )
				sxmo_hotspot.sh
				;;
			"Delete a Network" )
				deletenetworkmenu
				;;
			"Launch Nmtui" )
				sxmo_terminal.sh nmtui &
				;;
			"Launch Ifconfig" )
				sxmo_terminal.sh watch -n 2 ifconfig &
				;;
			"Scan Wifi Networks" )
				sxmo_terminal.sh watch -n 2 nmcli d wifi list &
				;;
			*)
				toggleconnection "$CHOICE"
				;;
		esac
	done
}

if [ $# -gt 0 ]; then
	"$@"
else
	networksmenu
fi