debian-config-functions-network 19.6 KB
Newer Older
1
2
3
4
5
6
7
8
#!/bin/bash
#
# Copyright (c) 2017 Igor Pecovnik, igor.pecovnik@gma**.com
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.

9
# Functions:
10
11
# initialize_lte
# function lte
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# check_hostapd
# check_advanced_modes
# create_if_config
# reload-nety
# check_port
# check_ht_capab
# check_vht_capab
# check_channels
# nm_ip_editor
# systemd_ip_editor
# ip_editor
# wlan_edit_basic
# wlan_edit
# wlan_exceptions
# check_and_warn
# get_wlan_interface
# select_default_interface
# connect_bt_interface
30
31


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#
# 3G/4G general stuff
#
function initialize_lte ()
{
# This file defines udev rules for some 3G/UMTS/LTE modems.  It
# addresses the issue that the ttyUSB devices are numbered randomly, and
# their numbering can vary between server reboots.  These rules create
# persistent symlinks which can be reliably used in WWAN interface
# startup scripts.

# These rules assume that there is only one WWAN modem in the system. In
# order to address multiple WWAN cards, the rules need to be more
# specific and associate with serial numbers of the modems.

# Copyright (c) 2016 Stanislav Sinyagin <ssinyagin@k-open.com>.

# This content is published under Creative Commons Attribution 4.0
JC Staudt's avatar
JC Staudt committed
51
# International (CC BY 4.0) license.
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
147
148
149
# Source repository: https://github.com/ssinyagin/wwan_udev_rules

if ! check_if_installed ppp then; then
        apt-get -y -qq install ppp >/dev/null 2>&1
fi
cat >/etc/udev/rules.d/99-wwan.rules <<'EOT'


# Source repository: https://github.com/ssinyagin/wwan_udev_rules

# Huawei ME909s-120 LTE modem
SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="15c1", SYMLINK+="ttyWWAN%E{ID_USB_INTERFACE_NUM}"
SUBSYSTEM=="net", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="15c1", NAME="lte0"

# Huawei MU709s-2 UMTS modem
SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1c25", SYMLINK+="ttyWWAN%E{ID_USB_INTERFACE_NUM}"
SUBSYSTEM=="net", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1c25", NAME="umts0"

# Qualcomm Gobi2000
SUBSYSTEM=="tty", ATTRS{idVendor}=="03f0", ATTRS{idProduct}=="251d", SYMLINK+="ttyWWAN%E{ID_USB_INTERFACE_NUM}"

# Sierra Wireless MC8775
SUBSYSTEM=="tty", ATTRS{idVendor}=="03f0", ATTRS{idProduct}=="1e1d", SYMLINK+="ttyWWAN%E{.ID_PORT}"

# Novatel Wireless, Inc. Expedite E371
SUBSYSTEM=="tty", ATTRS{idVendor}=="413c", ATTRS{idProduct}=="819b", SYMLINK+="ttyWWAN%E{ID_USB_INTERFACE_NUM}"

# SIMCom SIM7100
SUBSYSTEM=="tty", ATTRS{idVendor}=="1e0e", ATTRS{idProduct}=="9001", SYMLINK+="ttyWWAN%E{ID_USB_INTERFACE_NUM}"
SUBSYSTEM=="net", ATTRS{idVendor}=="1e0e", ATTRS{idProduct}=="9001", NAME="lte0"
EOT

# reload udev rules
udevadm control --reload-rules && udevadm trigger
}




#
# 3G/4G init scripts add + remove
#
function lte ()
{
case $1 in
	"12d1:15c1" )
	# Huawei ME909s-120 LTE modem
	LTE_MODEM_ID=$1
	if [[ $2 == on && $(cat /sys/class/net/lte0/operstate) == "up" ]]; then
		show_box  "Warning" "Huawei ME909s-120 LTE modem is up and running" 7
	elif [[ $2 == on ]]; then
		show_box  "Info" "Huawei ME909s-120 LTE modem will try to connect" 7
cat >/etc/chatscripts/sunrise.HUAWEI <<'EOT'
ABORT BUSY
ABORT 'NO CARRIER'
ABORT ERROR
TIMEOUT 10
'' ATZ
OK 'AT+CFUN=1'
OK 'AT+CMEE=1'
OK 'AT\^NDISDUP=1,1,"internet"'
OK
EOT
cat >/etc/chatscripts/gsm_off.HUAWEI <<'EOT'
ABORT ERROR
TIMEOUT 5
'' AT+CFUN=0 OK
EOT

cat >/etc/network/interfaces.d/lte0 <<'EOT'
allow-hotplug lte0
iface lte0 inet dhcp
    pre-up /usr/sbin/chat -v -f /etc/chatscripts/sunrise.HUAWEI >/dev/ttyWWAN02 </dev/ttyWWAN02
    post-down /usr/sbin/chat -v -f /etc/chatscripts/gsm_off.HUAWEI >/dev/ttyWWAN02 </dev/ttyWWAN02
EOT

		# disable interface
		ifup lte0 2> /dev/null

	elif [[ $2 == off ]]; then
		show_box  "Warning" "Huawei ME909s-120 LTE will kill connection" 7
		# enable interface
		ifdown lte0 2> /dev/null
		rm /etc/chatscripts/sunrise.HUAWEI
		rm /etc/chatscripts/gsm_off.HUAWEI
		rm /etc/network/interfaces.d/lte0
	elif [[ $(cat /sys/class/net/lte0/operstate) == "up" ]]; then
		LTE_MODEM="Huawei ME909s-120 LTE modem is online"
	else
		LTE_MODEM="Huawei ME909s-120 LTE modem is offline"
	fi
	;;
esac
}




150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#
# check hostapd configuration. Return error or empty if o.k.
#
check_hostapd ()
{

	systemctl daemon-reload
	hostapd_error=""
	[[ -n $1 && -n $2 ]] && dialog --title " $1 " --backtitle "$BACKTITLE" --no-collapse --colors --infobox "$2" 5 $((${#2}-3))
	service hostapd stop
	rm -f /var/run/hostapd/*
	sleep 1
	service hostapd start
	sleep 6
	if [[ "$(hostapd_cli ping 2> /dev/null| grep PONG)" == "PONG" ]]; then
			hostapd_error=""
		else
			hostapd_error=$(hostapd /etc/hostapd.conf)
			sleep 6
			[[ -n $(echo $hostapd_error | grep "channel") ]] && hostapd_error="channel_restriction"
			[[ -n $(echo $hostapd_error | grep "does not support" | grep "hw_mode") ]] && hostapd_error="hw_mode"
			[[ -n $(echo $hostapd_error | grep "not found from the channel list" | grep "802.11g") ]] && hostapd_error="wrong_channel"
			[[ -n $(echo $hostapd_error | grep "VHT") ]] && hostapd_error="unsupported_vht"
			[[ -n $(echo $hostapd_error | grep " HT capability") ]] && hostapd_error="unsupported_ht"
	fi

}




#
# check all possible wireless modes
#
function check_advanced_modes ()
{

	sed '/### IEEE 802.11n/,/^### IEEE 802.11n/ s/^# *//' -i /etc/hostapd.conf
	check_hostapd "Probing" "\n802.11n \Z1(150-300Mbps)\Z0"
	# check HT capability
	check_ht_capab
	if [[ -z "$hostapd_error" ]]; then
		sed '/### IEEE 802.11a\>/,/^### IEEE 802.11a\>/ s/^# *//' -i /etc/hostapd.conf
		sed -i "s/^channel=.*/channel=40/" /etc/hostapd.conf
		check_hostapd "Probing" "\n802.11a \Z1(5Ghz)\Z0"
		if [[ "$hostapd_error" == "channel_restriction" ]]; then check_channels; fi
		if [[ "$hostapd_error" == "channel_restriction" ]]; then
			# revering configuration
			sed -i "s/^channel=.*/channel=5/" /etc/hostapd.conf
			sed '/## IEEE 802.11a\>/,/^## IEEE 802.11a\>/ s/.*/#&/' -i /etc/hostapd.conf
			check_hostapd "Reverting" "\nWireless \Z1802.11a (5Ghz)\Z0 is not supported"
		else
			sed '/### IEEE 802.11ac\>/,/^### IEEE 802.11ac\>/ s/^# *//' -i /etc/hostapd.conf
			# check VHT capability
			check_vht_capab
			if [[ "$hostapd_error" == "unsupported_vht" || "$hostapd_error" == "channel_restriction" ]]; then
				# revering configuration
				sed '/## IEEE 802.11ac\>/,/^## IEEE 802.11ac\>/ s/.*/#&/' -i /etc/hostapd.conf
				check_hostapd "Reverting" "\nWireless 802.11ac \Z1(433Mbps x n @ 5Ghz)\Z0 is not supported"
				if [[ "$hostapd_error" == "channel_restriction" ]]; then check_channels; fi
			fi
		fi
	else
		sed '/## IEEE 802.11n/,/^## IEEE 802.11n/ s/.*/#&/' -i /etc/hostapd.conf
	fi

}




#
# create interface configuration section
#
function create_if_config() {

		address=$(ip -4 addr show dev $1 | awk '/inet/ {print $2}' | cut -d'/' -f1)
		netmask=$(ip -4 addr show dev $1 | awk '/inet/ {print $2}' | cut -d'/' -f2)
		gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}' | sed -n '1p')
		echo -e "# armbian-config created"
		echo -e "source /etc/network/interfaces.d/*\n"
		if [[ "$3" == "fixed" ]]; then
			echo -e "# Local loopback\nauto lo\niface lo init loopback\n"
			echo -e "# Interface $2\nauto $2\nallow-hotplug $2"
			echo -e "iface $2 inet static\n\taddress $address\n\tnetmask $netmask\n\tgateway $gateway\n\tdns-nameservers 8.8.8.8"
		fi

}




#
# reload network related services
#
function reload-nety() {

	systemctl daemon-reload
	if [[ "$1" == "reload" ]]; then WHATODO="Reloading services"; else WHATODO="Stopping services"; fi
	(service network-manager stop; echo 10; sleep 1; service hostapd stop; echo 20; sleep 1; service dnsmasq stop; echo 30; sleep 1;\
	[[ "$1" == "reload" ]] && service dnsmasq start && echo 60 && sleep 1 && service hostapd start && echo 80 && sleep 1;\
	service network-manager start; echo 90; sleep 5;) | dialog --backtitle "$BACKTITLE" --title " $WHATODO " --gauge "" 6 70 0
	systemctl restart systemd-resolved.service

}




#
# Check if something is running on port $1 and display info
#
262
function check_port ()
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
{
	[[ -n $(netstat -lnt | awk '$6 == "LISTEN" && $4 ~ ".'$1'"') ]] && dialog --backtitle "$BACKTITLE" --title "Checking service" \
	--msgbox "\nIt looks good.\n\nThere is $2 service on port $1" 9 52
}




#
# check wifi high throughput
#
function check_ht_capab ()
	{
	declare -a arr=("[HT40+][LDPC][SHORT-GI-20][SHORT-GI-40][TX-STBC][RX-STBC1][DSSS_CCK-40][SMPS-STATIC]" \
	"[HT40-][SHORT-GI-40][SHORT-GI-40][DSSS_CCK-40]" "[SHORT-GI-20][SHORT-GI-40][HT40+]" "[DSSS_CK-40][HT20+]" "")
	local j=0
	for i in "${arr[@]}"
	do
		j=$((j+(100/${#arr[@]})))
		echo $j | dialog --title " Probing HT " --colors --gauge "\nSeeking for optimal \Z1high throughput\Z0 settings." 8 50 0
		sed -i "s/^ht_capab=.*/ht_capab=$i/" /etc/hostapd.conf
		check_hostapd
		if [[ "$hostapd_error" == "channel_restriction" ]]; then check_channels; fi
		if [[ "$hostapd_error" == "" ]]; then break; fi
	done
}




#
# check wifi high throughput
#
function check_vht_capab ()
{

	declare -a arr=("[MAX-MPDU-11454][SHORT-GI-80][TX-STBC-2BY1][RX-STBC-1][MAX-A-MPDU-LEN-EXP3]" "[MAX-MPDU-11454][SHORT-GI-80][RX-STBC-1][MAX-A-MPDU-LEN-EXP3]" "")
	local j=0
	for i in "${arr[@]}"
	do
		j=$((j+(100/${#arr[@]})))
		echo $j | dialog --title " Probing VHT " --colors --gauge "\nSeeking for optimal \Z1very high throughput\Z0 settings." 8 54 0
		sed -i "s/^vht_capab=.*/vht_capab=$i/" /etc/hostapd.conf
		check_hostapd
		if [[ "$hostapd_error" == "channel_restriction" ]]; then check_channels; fi
		if [[ "$hostapd_error" == "" ]]; then break; fi
	done

}




#
# check 5Ghz channels
#
function check_channels ()
{

	declare -a arr=("36" "40")
	for i in "${arr[@]}"
	do
		sed -i "s/^channel=.*/channel=$i/" /etc/hostapd.conf
		check_hostapd "Probing" "\nChannel:\Z1 ${i}\Z0"
		if [[ "$hostapd_error" != "channel_restriction" ]]; then break; fi
	done

}




#
JC Staudt's avatar
JC Staudt committed
336
# edit ip address within network manager
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#
function nm_ip_editor ()
{

exec 3>&1
	dialog --title " Static IP configuration" --backtitle "$BACKTITLE" --form "\nAdapter: $1
	\n " 12 38 0 \
	"Address:"				1 1 "$address"				1 15 15 0 \
	"Netmask:"			2 1 "$netmask"	2 15 15 0 \
	"Gateway:"			3 1 "$gateway"			3 15 15 0 \
	2>&1 1>&3 | {
		read -r address;read -r netmask;read -r gateway
		if [[ $? = 0 ]]; then
				localuuid=$(LC_ALL=C nmcli -f UUID,DEVICE connection show | grep $1 | awk '{print $1}')
				nmcli con mod $localuuid ipv4.method manual ipv4.addresses "$address/$netmask" >/dev/null 2>&1
				nmcli con mod $localuuid ipv4.method manual ipv4.gateway  "$gateway" >/dev/null 2>&1
				nmcli con mod $localuuid ipv4.dns "8.8.8.8,$gateway" >/dev/null 2>&1
				nmcli con down $localuuid >/dev/null 2>&1
				sleep 2
				nmcli con up $localuuid >/dev/null 2>&1
		fi
		}
}




#
# edit ip address
#
function systemd_ip_editor ()
{

	local filename="/etc/systemd/network/10-$1.network"
	if [[ -f $filename ]]; then
	sed -i '/Network/,$d' $filename
	exec 3>&1
		dialog --title " Static IP configuration" --backtitle "$BACKTITLE" --form "\nAdapter: $1
		\n " 12 38 0 \
		"Address:"				1 1 "$address"				1 15 15 0 \
		"Netmask:"			2 1 "$netmask"	2 15 15 0 \
		"Gateway:"			3 1 "$gateway"			3 15 15 0 \
		2>&1 1>&3 | {
			read -r address;read -r netmask;read -r gateway
			if [[ $? = 0 ]]; then
					echo -e "[Network]" >>$filename
					echo -e "Address=$address" >> $filename
					echo -e "Gateway=$gateway" >> $filename
					echo -e "DNS=8.8.8.8" >> $filename
			fi
			}
	fi

}




#
# edit ip address
#
function ip_editor ()
{

	exec 3>&1
	dialog --title " Static IP configuration" --backtitle "$BACKTITLE" --form "\nAdapter: $1
	\n " 12 38 0 \
	"Address:"				1 1 "$address"				1 15 15 0 \
	"Netmask:"			2 1 "$netmask"	2 15 15 0 \
	"Gateway:"			3 1 "$gateway"			3 15 15 0 \
	2>&1 1>&3 | {
		read -r address;read -r netmask;read -r gateway
		if [[ $? = 0 ]]; then
				echo -e "# armbian-config created\nsource /etc/network/interfaces.d/*\n" >$3
				echo -e "# Local loopback\nauto lo\niface lo inet loopback\n" >> $3
				echo -e "# Interface $2\nauto $2\nallow-hotplug $2\niface $2 inet static\
				\n\taddress $address\n\tnetmask $netmask\n\tgateway $gateway\n\tdns-nameservers 8.8.8.8" >> $3
		fi
		}

}




#
# edit hostapd parameters
#
function wlan_edit_basic ()
{
	source /etc/hostapd.conf
	exec 3>&1
	dialog --title "AP configuration" --backtitle "$BACKTITLE" --form "\nWPA2 enabled, \
	advanced config: edit /etc/hostapd.conf\n " 12 58 0 \
	"SSID:"				1 1 "$ssid"				1 31 22 0 \
	"Password:"			2 1 "$wpa_passphrase"	2 31 22 0 \
	"Channel:"			3 1 "$channel"			3 31 3 0 \
	2>&1 1>&3 | {
		read -r ssid;read -r wpa_passphrase;read -r channel
		if [[ $? = 0 ]]; then
				sed -i "s/^ssid=.*/ssid=$ssid/" /etc/hostapd.conf
				sed -i "s/^wpa_passphrase=.*/wpa_passphrase=$wpa_passphrase/" /etc/hostapd.conf
				sed -i "s/^channel=.*/channel=$channel/" /etc/hostapd.conf
				wpa_psk=$(wpa_passphrase $ssid $wpa_passphrase | grep '^[[:blank:]]*[^[:blank:]#;]' | grep psk | cut -d= -f2-)
				sed -i "s/^wpa_psk=.*/wpa_psk=$wpa_psk/" /etc/hostapd.conf
		fi
		}
}




#
# edit hostapd parameters
#
function wlan_edit ()
{

	# select default interfaces if there is more than one
	select_default_interface
	dialog --title " Configuration edit " --colors --backtitle "$BACKTITLE" --help-button --help-label "Cancel" --yes-label "Basic" \
	--no-label "Advanced" --yesno "\n\Z1Basic:\Z0    Change SSID, password and channel\n\n\Z1Advanced:\Z0 Edit /etc/hostapd.conf file" 9 70
	if [[ $? = 0 ]]; then
		wlan_edit_basic
	elif [[ $? = 1 ]]; then
		dialog --backtitle "$BACKTITLE" --title " Edit hostapd configuration /etc/hostapd.conf" --no-collapse \
		--ok-label "Save" --editbox /etc/hostapd.conf 30 0 2> /etc/hostapd.conf.out
		[[ $? = 0 ]] && mv /etc/hostapd.conf.out /etc/hostapd.conf && service hostapd restart
	fi

}




#
# here we add wifi exceptions
#
function wlan_exceptions ()
{

	reboot_module=false
	[[ -n "$(lsmod | grep -w dhd)" && $1 = "on" ]] && \
	echo 'options dhd op_mode=2' >/etc/modprobe.d/ap6212.conf && rmmod dhd && modprobe dhd
	[[ -n "$(lsmod | grep -w dhd)" && $1 = "off" ]] && \
	rm /etc/modprobe.d/ap6212.conf && rmmod dhd && modprobe dhd
	# Cubietruck
	[[ -n "$(lsmod | grep -w ap6210)" && $1 = "on" ]] && \
	echo 'options ap6210 op_mode=2' >/etc/modprobe.d/ap6210.conf && reboot_module=true
	[[ -n "$(lsmod | grep -w ap6210)" && $1 = "off" ]] && \
	rm /etc/modprobe.d/ap6210.conf && reboot_module=true

}




#
# here add shaddy wifi adaptors
#
function check_and_warn ()
{

	local shaddy=false
	# blacklist
	[[ "$LINUXFAMILY" == "sun8i" && $BOARD == "orangepizero" ]] && shaddy=true
	[[ -n "$(lsmod | grep mt7601u)" ]] && shaddy=true
	[[ -n "$(lsmod | grep r8188eu)" ]] && shaddy=true
	# blacklist
	if [[ "$shaddy" == "true" ]]; then
	dialog --title " Warning " --ok-label " Accept and proceed " --msgbox '\nOne of your wireless drivers are on a black list due to poor quality.\n\nAP mode might not be possible!' 9 73
	fi

}




#
# search for wlan interfaces and provide a selection menu if there are more than one
#
function get_wlan_interface ()
{

	IFS=$'\r\n'
	GLOBIGNORE='*'

	WLAN_INTERFACES=($(LC_ALL=C nmcli --wait 10 dev status | grep wifi | grep disconnected |awk '{print $1}'))
	local LIST=()
	for i in "${WLAN_INTERFACES[@]}"
	do
			LIST+=( "${i[0]//[[:blank:]]/}" "" )
	done
JC Staudt's avatar
JC Staudt committed
530
	LIST_LENGTH=$((${#LIST[@]}/2));
531

JC Staudt's avatar
JC Staudt committed
532
	if [ "$LIST_LENGTH" -eq 1 ]; then
533
534
535
536
			WIRELESS_ADAPTER=${WLAN_INTERFACES[0]}
	else
			exec 3>&1
			WIRELESS_ADAPTER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
JC Staudt's avatar
JC Staudt committed
537
			--title "Select interface" --clear --menu "" $((6+${LIST_LENGTH})) 40 15 "${LIST[@]}" 2>&1 1>&3)
538
539
540
541
542
543
544
545
546
			exec 3>&-
	fi

}




#
547
# select interface if there is more than one and adjust metric
548
549
550
#
function select_default_interface ()
{
Igor Pecovnik's avatar
Igor Pecovnik committed
551
552
	ALREADY_DEFINED=$(cat /etc/iptables.ipv4.nat 2> /dev/null | grep "POSTROUTING -o" | tail -1 | awk '{ print $4 }')
	if [[ -n "${ALREADY_DEFINED}" ]]; then
553

Igor Pecovnik's avatar
Igor Pecovnik committed
554
		DEFAULT_ADAPTER=${ALREADY_DEFINED}
555
	else
Igor Pecovnik's avatar
Igor Pecovnik committed
556
557
		IFS=$'\r\n'
		GLOBIGNORE='*'
558
		local ADAPTER=($(nmcli -t -f DEVICE connection show --active))
Igor Pecovnik's avatar
Igor Pecovnik committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
		local LIST=()
		for i in "${ADAPTER[@]}"
		do
				local IPADDR=$(LC_ALL=C ip -4 addr show dev ${i[0]} | awk '/inet/ {print $2}' | cut -d'/' -f1)
				ADD_SPEED=""
				[[ $i == eth* || $i == en* ]] && ADD_SPEED=$(ethtool $i | grep Speed)
				[[ $i == wl* ]] && ADD_SPEED=$(LC_ALL=C nmcli -f RATE,DEVICE,ACTIVE dev wifi list | grep $i | grep yes | awk 'NF==4{print $1""$2}NF==1' | sed -e 's/^/  Speed: /' | tail -1)
				[[ $IPADDR != "172.24.1.1" && -n $IPADDR ]] && LIST+=( "${i[0]//[[:blank:]]/}" "${IPADDR} ${ADD_SPEED}" )
		done
		LIST_LENGTH=$((${#LIST[@]}/2));
		if [ "$LIST_LENGTH" -eq 0 ]; then
				DEFAULT_ADAPTER="lo"
		elif [ "$LIST_LENGTH" -eq 1 ]; then
				DEFAULT_ADAPTER=${ADAPTER[0]}
		else
				exec 3>&1
				DEFAULT_ADAPTER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
576
				--title "Select default interface" --clear --menu "" $((6+${LIST_LENGTH})) 74 14 "${LIST[@]}" 2>&1 1>&3)
Igor Pecovnik's avatar
Igor Pecovnik committed
577
578
				exec 3>&-
		fi
579
	fi
580
581
582
583

	# set highest metric to default adaptor
	HIGHEST_METRIC=$(nmcli -t -f UUID,TYPE,DEVICE connection show --active | grep $DEFAULT_ADAPTER | sed 's/:.*$//')

584
585
	# set metric to 50
	nmcli connection modify $HIGHEST_METRIC ipv4.route-metric 50 2> /dev/null
586

587
	METRIC=77
588
589
	# set others wired
	REMAINING=( `nmcli -t -f UUID,TYPE,DEVICE connection show --active | grep ethernet | grep -v $DEFAULT_ADAPTER | sed 's/:.*$//'` )
590
591
592
593
594
595
596
	if [[ ${#REMAINING[@]} -ge 1 ]]; then
		for i in "${REMAINING[@]}"
		do
			METRIC=$(( $METRIC + 1 ))
			nmcli connection modify ${i} ipv4.route-metric $METRIC
		done
	fi
597
598

	# set other wireless
599
	METRIC=88
600
	REMAINING=( `nmcli -t -f UUID,TYPE,DEVICE connection show --active | grep wireless | grep -v $DEFAULT_ADAPTER | sed 's/:.*$//'` )
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
	if [[ ${#REMAINING[@]} -ge 1 ]]; then
		for i in "${REMAINING[@]}"
		do
			METRIC=$(( $METRIC + 1 ))
			nmcli connection modify ${i} ipv4.route-metric $METRIC
		done
	fi

	# create default metrics file
	cat <<-EOF > /etc/NetworkManager/conf.d/armbian-default-metric.conf
	[connection-ethernet-gateway]
	match-device=interface-name:$DEFAULT_ADAPTER
	ipv4.route-metric=50

	[connection-wifi-other]
	match-device=type:wifi
	ipv4.route-metric=88

	[connection-ethernet-other]
	match-device=type:ethernet
	ipv4.route-metric=77
EOF
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
}




#
# search and connect to Bluetooth devices
#
function connect_bt_interface ()
{

		IFS=$'\r\n'
		GLOBIGNORE='*'
		dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nDiscovering Bluetooth devices ... " 5 37
		BT_INTERFACES=($(hcitool scan | sed '1d'))

		local LIST=()
		for i in "${BT_INTERFACES[@]}"
		do
			local a=$(echo ${i[0]//[[:blank:]]/} | sed -e 's/^\(.\{17\}\).*/\1/')
			local b=${i[0]//$a/}
			local b=$(echo $b | sed -e 's/^[ \t]*//')
			LIST+=( "$a" "$b")
		done

JC Staudt's avatar
JC Staudt committed
648
649
		LIST_LENGTH=$((${#LIST[@]}/2));
		if [ "$LIST_LENGTH" -eq 0 ]; then
650
651
652
653
654
			BT_ADAPTER=${WLAN_INTERFACES[0]}
			dialog --backtitle "$BACKTITLE" --title "Bluetooth" --msgbox "\nNo nearby Bluetooth devices were found!" 7 43
		else
			exec 3>&1
			BT_ADAPTER=$(dialog --backtitle "$BACKTITLE" --no-collapse --title "Select interface" \
JC Staudt's avatar
JC Staudt committed
655
			--clear --menu "" $((6+${LIST_LENGTH})) 50 15 "${LIST[@]}" 2>&1 1>&3)
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
			exec 3>&-
			if [[ $BT_ADAPTER != "" ]]; then
				dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nConnecting to $BT_ADAPTER " 5 35
				BT_EXEC=$(
				expect -c 'set prompt "#";set address '$BT_ADAPTER';spawn bluetoothctl;expect -re $prompt;send "disconnect $address\r";
				sleep 1;send "remove $address\r";sleep 1;expect -re $prompt;send "scan on\r";sleep 8;send "scan off\r";
				expect "Controller";send "trust $address\r";sleep 2;send "pair $address\r";sleep 2;send "connect $address\r";
				send_user "\nShould be paired now.\r";sleep 2;send "quit\r";expect eof')
				echo "$BT_EXEC" > /tmp/bt-connect-debug.log
					if [[ $(echo "$BT_EXEC" | grep "Connection successful" ) != "" ]]; then
						dialog --backtitle "$BACKTITLE" --title "Bluetooth" --msgbox "\nYour device is ready to use!" 7 32
					else
						dialog --backtitle "$BACKTITLE" --title "Bluetooth" --msgbox "\nError connecting. Try again!" 7 32
					fi
			fi
		fi

673
}