debian-config-functions-network 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2017 Igor Pecovnik, igor.pecovnik@gma**.com
  4. #
  5. # This file is licensed under the terms of the GNU General Public
  6. # License version 2. This program is licensed "as is" without any
  7. # warranty of any kind, whether express or implied.
  8. # Functions:
  9. # initialize_lte
  10. # function lte
  11. # check_hostapd
  12. # check_advanced_modes
  13. # create_if_config
  14. # reload-nety
  15. # check_port
  16. # check_ht_capab
  17. # check_vht_capab
  18. # check_channels
  19. # netmask_to_cidr
  20. # nm_ip_editor
  21. # systemd_ip_editor
  22. # ip_editor
  23. # wlan_edit_basic
  24. # wlan_edit
  25. # wlan_exceptions
  26. # check_and_warn
  27. # get_wlan_interface
  28. # select_default_interface
  29. # connect_bt_interface
  30. #
  31. # 3G/4G general stuff
  32. #
  33. function initialize_lte ()
  34. {
  35. # This file defines udev rules for some 3G/UMTS/LTE modems. It
  36. # addresses the issue that the ttyUSB devices are numbered randomly, and
  37. # their numbering can vary between server reboots. These rules create
  38. # persistent symlinks which can be reliably used in WWAN interface
  39. # startup scripts.
  40. # These rules assume that there is only one WWAN modem in the system. In
  41. # order to address multiple WWAN cards, the rules need to be more
  42. # specific and associate with serial numbers of the modems.
  43. # Copyright (c) 2016 Stanislav Sinyagin <ssinyagin@k-open.com>.
  44. # This content is published under Creative Commons Attribution 4.0
  45. # International (CC BY 4.0) license.
  46. # Source repository: https://github.com/ssinyagin/wwan_udev_rules
  47. if ! check_if_installed ppp then; then
  48. apt-get -y -qq install ppp >/dev/null 2>&1
  49. fi
  50. cat >/etc/udev/rules.d/99-wwan.rules <<'EOT'
  51. # Source repository: https://github.com/ssinyagin/wwan_udev_rules
  52. # Huawei ME909s-120 LTE modem
  53. SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="15c1", SYMLINK+="ttyWWAN%E{ID_USB_INTERFACE_NUM}"
  54. SUBSYSTEM=="net", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="15c1", NAME="lte0"
  55. # Huawei MU709s-2 UMTS modem
  56. SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1c25", SYMLINK+="ttyWWAN%E{ID_USB_INTERFACE_NUM}"
  57. SUBSYSTEM=="net", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1c25", NAME="umts0"
  58. # Qualcomm Gobi2000
  59. SUBSYSTEM=="tty", ATTRS{idVendor}=="03f0", ATTRS{idProduct}=="251d", SYMLINK+="ttyWWAN%E{ID_USB_INTERFACE_NUM}"
  60. # Sierra Wireless MC8775
  61. SUBSYSTEM=="tty", ATTRS{idVendor}=="03f0", ATTRS{idProduct}=="1e1d", SYMLINK+="ttyWWAN%E{.ID_PORT}"
  62. # Novatel Wireless, Inc. Expedite E371
  63. SUBSYSTEM=="tty", ATTRS{idVendor}=="413c", ATTRS{idProduct}=="819b", SYMLINK+="ttyWWAN%E{ID_USB_INTERFACE_NUM}"
  64. # SIMCom SIM7100
  65. SUBSYSTEM=="tty", ATTRS{idVendor}=="1e0e", ATTRS{idProduct}=="9001", SYMLINK+="ttyWWAN%E{ID_USB_INTERFACE_NUM}"
  66. SUBSYSTEM=="net", ATTRS{idVendor}=="1e0e", ATTRS{idProduct}=="9001", NAME="lte0"
  67. EOT
  68. # reload udev rules
  69. udevadm control --reload-rules && udevadm trigger
  70. }
  71. #
  72. # 3G/4G init scripts add + remove
  73. #
  74. function lte ()
  75. {
  76. case $1 in
  77. "12d1:15c1" )
  78. # Huawei ME909s-120 LTE modem
  79. LTE_MODEM_ID=$1
  80. if [[ $2 == on && $(cat /sys/class/net/lte0/operstate) == "up" ]]; then
  81. show_box "Warning" "Huawei ME909s-120 LTE modem is up and running" 7
  82. elif [[ $2 == on ]]; then
  83. show_box "Info" "Huawei ME909s-120 LTE modem will try to connect" 7
  84. cat >/etc/chatscripts/sunrise.HUAWEI <<'EOT'
  85. ABORT BUSY
  86. ABORT 'NO CARRIER'
  87. ABORT ERROR
  88. TIMEOUT 10
  89. '' ATZ
  90. OK 'AT+CFUN=1'
  91. OK 'AT+CMEE=1'
  92. OK 'AT\^NDISDUP=1,1,"internet"'
  93. OK
  94. EOT
  95. cat >/etc/chatscripts/gsm_off.HUAWEI <<'EOT'
  96. ABORT ERROR
  97. TIMEOUT 5
  98. '' AT+CFUN=0 OK
  99. EOT
  100. cat >/etc/network/interfaces.d/lte0 <<'EOT'
  101. allow-hotplug lte0
  102. iface lte0 inet dhcp
  103. pre-up /usr/sbin/chat -v -f /etc/chatscripts/sunrise.HUAWEI >/dev/ttyWWAN02 </dev/ttyWWAN02
  104. post-down /usr/sbin/chat -v -f /etc/chatscripts/gsm_off.HUAWEI >/dev/ttyWWAN02 </dev/ttyWWAN02
  105. EOT
  106. # disable interface
  107. ifup lte0 2> /dev/null
  108. elif [[ $2 == off ]]; then
  109. show_box "Warning" "Huawei ME909s-120 LTE will kill connection" 7
  110. # enable interface
  111. ifdown lte0 2> /dev/null
  112. rm /etc/chatscripts/sunrise.HUAWEI
  113. rm /etc/chatscripts/gsm_off.HUAWEI
  114. rm /etc/network/interfaces.d/lte0
  115. elif [[ $(cat /sys/class/net/lte0/operstate) == "up" ]]; then
  116. LTE_MODEM="Huawei ME909s-120 LTE modem is online"
  117. else
  118. LTE_MODEM="Huawei ME909s-120 LTE modem is offline"
  119. fi
  120. ;;
  121. esac
  122. }
  123. #
  124. # check hostapd configuration. Return error or empty if o.k.
  125. #
  126. check_hostapd ()
  127. {
  128. systemctl daemon-reload
  129. hostapd_error=""
  130. [[ -n $1 && -n $2 ]] && dialog --title " $1 " --backtitle "$BACKTITLE" --no-collapse --colors --infobox "$2" 5 $((${#2}-3))
  131. service hostapd stop
  132. rm -f /var/run/hostapd/*
  133. sleep 1
  134. service hostapd start
  135. sleep 6
  136. if [[ "$(hostapd_cli ping 2> /dev/null| grep PONG)" == "PONG" ]]; then
  137. hostapd_error=""
  138. else
  139. hostapd_error=$(hostapd /etc/hostapd.conf)
  140. sleep 6
  141. [[ -n $(echo $hostapd_error | grep "channel") ]] && hostapd_error="channel_restriction"
  142. [[ -n $(echo $hostapd_error | grep "does not support" | grep "hw_mode") ]] && hostapd_error="hw_mode"
  143. [[ -n $(echo $hostapd_error | grep "not found from the channel list" | grep "802.11g") ]] && hostapd_error="wrong_channel"
  144. [[ -n $(echo $hostapd_error | grep "VHT") ]] && hostapd_error="unsupported_vht"
  145. [[ -n $(echo $hostapd_error | grep " HT capability") ]] && hostapd_error="unsupported_ht"
  146. fi
  147. }
  148. #
  149. # check all possible wireless modes
  150. #
  151. function check_advanced_modes ()
  152. {
  153. sed '/### IEEE 802.11n/,/^### IEEE 802.11n/ s/^# *//' -i /etc/hostapd.conf
  154. check_hostapd "Probing" "\n802.11n \Z1(150-300Mbps)\Z0"
  155. # check HT capability
  156. check_ht_capab
  157. if [[ -z "$hostapd_error" ]]; then
  158. sed '/### IEEE 802.11a\>/,/^### IEEE 802.11a\>/ s/^# *//' -i /etc/hostapd.conf
  159. sed -i "s/^channel=.*/channel=40/" /etc/hostapd.conf
  160. check_hostapd "Probing" "\n802.11a \Z1(5Ghz)\Z0"
  161. if [[ "$hostapd_error" == "channel_restriction" ]]; then check_channels; fi
  162. if [[ "$hostapd_error" == "channel_restriction" ]]; then
  163. # revering configuration
  164. sed -i "s/^channel=.*/channel=5/" /etc/hostapd.conf
  165. sed '/## IEEE 802.11a\>/,/^## IEEE 802.11a\>/ s/.*/#&/' -i /etc/hostapd.conf
  166. check_hostapd "Reverting" "\nWireless \Z1802.11a (5Ghz)\Z0 is not supported"
  167. else
  168. sed '/### IEEE 802.11ac\>/,/^### IEEE 802.11ac\>/ s/^# *//' -i /etc/hostapd.conf
  169. # check VHT capability
  170. check_vht_capab
  171. if [[ "$hostapd_error" == "unsupported_vht" || "$hostapd_error" == "channel_restriction" ]]; then
  172. # revering configuration
  173. sed '/## IEEE 802.11ac\>/,/^## IEEE 802.11ac\>/ s/.*/#&/' -i /etc/hostapd.conf
  174. check_hostapd "Reverting" "\nWireless 802.11ac \Z1(433Mbps x n @ 5Ghz)\Z0 is not supported"
  175. if [[ "$hostapd_error" == "channel_restriction" ]]; then check_channels; fi
  176. fi
  177. fi
  178. else
  179. sed '/## IEEE 802.11n/,/^## IEEE 802.11n/ s/.*/#&/' -i /etc/hostapd.conf
  180. fi
  181. }
  182. #
  183. # create interface configuration section
  184. #
  185. function create_if_config() {
  186. address=$(ip -4 addr show dev $1 | awk '/inet/ {print $2}' | cut -d'/' -f1)
  187. netmask=$(ip -4 addr show dev $1 | awk '/inet/ {print $2}' | cut -d'/' -f2)
  188. gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}' | sed -n '1p')
  189. echo -e "# orangepi-config created"
  190. echo -e "source /etc/network/interfaces.d/*\n"
  191. if [[ "$3" == "fixed" ]]; then
  192. echo -e "# Local loopback\nauto lo\niface lo init loopback\n"
  193. echo -e "# Interface $2\nauto $2\nallow-hotplug $2"
  194. echo -e "iface $2 inet static\n\taddress $address\n\tnetmask $netmask\n\tgateway $gateway\n\tdns-nameservers 8.8.8.8"
  195. fi
  196. }
  197. #
  198. # reload network related services
  199. #
  200. function reload-nety() {
  201. systemctl daemon-reload
  202. if [[ "$1" == "reload" ]]; then WHATODO="Reloading services"; else WHATODO="Stopping services"; fi
  203. (service network-manager stop >/dev/null 2>&1; service NetworkManager stop >/dev/null 2>&1; echo 10; sleep 1; service hostapd stop; echo 20; sleep 1; service dnsmasq stop; echo 30; sleep 1;\
  204. [[ "$1" == "reload" ]] && service dnsmasq start && echo 60 && sleep 1 && service hostapd start && echo 80 && sleep 1;\
  205. service network-manager start >/dev/null 2>&1; service NetworkManager start >/dev/null 2>&1; echo 90; sleep 5;) | dialog --backtitle "$BACKTITLE" --title " $WHATODO " --gauge "" 6 70 0
  206. systemctl restart systemd-resolved.service
  207. }
  208. #
  209. # Check if something is running on port $1 and display info
  210. #
  211. function check_port ()
  212. {
  213. [[ -n $(netstat -lnt | awk '$6 == "LISTEN" && $4 ~ ".'$1'"') ]] && dialog --backtitle "$BACKTITLE" --title "Checking service" \
  214. --msgbox "\nIt looks good.\n\nThere is $2 service on port $1" 9 52
  215. }
  216. #
  217. # check wifi high throughput
  218. #
  219. function check_ht_capab ()
  220. {
  221. declare -a arr=("[HT40+][LDPC][SHORT-GI-20][SHORT-GI-40][TX-STBC][RX-STBC1][DSSS_CCK-40][SMPS-STATIC]" \
  222. "[HT40-][SHORT-GI-40][SHORT-GI-40][DSSS_CCK-40]" "[SHORT-GI-20][SHORT-GI-40][HT40+]" "[DSSS_CK-40][HT20+]" "")
  223. local j=0
  224. for i in "${arr[@]}"
  225. do
  226. j=$((j+(100/${#arr[@]})))
  227. echo $j | dialog --title " Probing HT " --colors --gauge "\nSeeking for optimal \Z1high throughput\Z0 settings." 8 50 0
  228. sed -i "s/^ht_capab=.*/ht_capab=$i/" /etc/hostapd.conf
  229. check_hostapd
  230. if [[ "$hostapd_error" == "channel_restriction" ]]; then check_channels; fi
  231. if [[ "$hostapd_error" == "" ]]; then break; fi
  232. done
  233. }
  234. #
  235. # check wifi high throughput
  236. #
  237. function check_vht_capab ()
  238. {
  239. 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]" "")
  240. local j=0
  241. for i in "${arr[@]}"
  242. do
  243. j=$((j+(100/${#arr[@]})))
  244. echo $j | dialog --title " Probing VHT " --colors --gauge "\nSeeking for optimal \Z1very high throughput\Z0 settings." 8 54 0
  245. sed -i "s/^vht_capab=.*/vht_capab=$i/" /etc/hostapd.conf
  246. check_hostapd
  247. if [[ "$hostapd_error" == "channel_restriction" ]]; then check_channels; fi
  248. if [[ "$hostapd_error" == "" ]]; then break; fi
  249. done
  250. }
  251. #
  252. # check 5Ghz channels
  253. #
  254. function check_channels ()
  255. {
  256. declare -a arr=("36" "40")
  257. for i in "${arr[@]}"
  258. do
  259. sed -i "s/^channel=.*/channel=$i/" /etc/hostapd.conf
  260. check_hostapd "Probing" "\nChannel:\Z1 ${i}\Z0"
  261. if [[ "$hostapd_error" != "channel_restriction" ]]; then break; fi
  262. done
  263. }
  264. #
  265. # convert netmask to CIDR
  266. #
  267. function netmask_to_cidr ()
  268. {
  269. IFS=' '
  270. local bits=0
  271. for octet in $(echo $1| sed 's/\./ /g'); do
  272. binbits=$(echo "obase=2; ibase=10; ${octet}"| bc | sed 's/0//g')
  273. let bits+=${#binbits}
  274. done
  275. echo "${bits}"
  276. }
  277. #
  278. # edit ip address within network manager
  279. #
  280. function nm_ip_editor ()
  281. {
  282. exec 3>&1
  283. dialog --title " Static IP configuration" --backtitle "$BACKTITLE" --form "\nAdapter: $1
  284. \n " 12 38 0 \
  285. "Address:" 1 1 "$address" 1 15 15 0 \
  286. "Netmask:" 2 1 "$netmask" 2 15 15 0 \
  287. "Gateway:" 3 1 "$gateway" 3 15 15 0 \
  288. 2>&1 1>&3 | {
  289. read -r address;read -r netmask;read -r gateway
  290. if [[ $? = 0 ]]; then
  291. localuuid=$(LC_ALL=C nmcli -f UUID,DEVICE connection show | grep $1 | awk '{print $1}')
  292. # convert netmask value to CIDR if required
  293. if [[ $netmask =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  294. CIDR=$(netmask_to_cidr ${netmask})
  295. else
  296. CIDR=${netmask}
  297. fi
  298. if [[ -n "$localuuid" ]]; then
  299. # adjust existing
  300. nmcli con mod $localuuid ipv4.method manual ipv4.addresses "$address/$CIDR" >/dev/null 2>&1
  301. nmcli con mod $localuuid ipv4.method manual ipv4.gateway "$gateway" >/dev/null 2>&1
  302. nmcli con mod $localuuid ipv4.dns "8.8.8.8,$gateway" >/dev/null 2>&1
  303. nmcli con down $localuuid >/dev/null 2>&1
  304. sleep 2
  305. nmcli con up $localuuid >/dev/null 2>&1
  306. else
  307. # create new
  308. nmcli con add con-name "orangepi" ifname "$1" type 802-3-ethernet ip4 "$address/$CIDR" gw4 "$gateway" >/dev/null 2>&1
  309. nmcli con mod "orangepi" ipv4.dns "8.8.8.8,$gateway" >/dev/null 2>&1
  310. nmcli con up "orangepi" >/dev/null 2>&1
  311. fi
  312. fi
  313. }
  314. }
  315. #
  316. # edit ip address
  317. #
  318. function systemd_ip_editor ()
  319. {
  320. local filename="/etc/systemd/network/10-$1.network"
  321. if [[ -f $filename ]]; then
  322. sed -i '/Network/,$d' $filename
  323. exec 3>&1
  324. dialog --title " Static IP configuration" --backtitle "$BACKTITLE" --form "\nAdapter: $1
  325. \n " 12 38 0 \
  326. "Address:" 1 1 "$address" 1 15 15 0 \
  327. "Netmask:" 2 1 "$netmask" 2 15 15 0 \
  328. "Gateway:" 3 1 "$gateway" 3 15 15 0 \
  329. 2>&1 1>&3 | {
  330. read -r address;read -r netmask;read -r gateway
  331. if [[ $? = 0 ]]; then
  332. echo -e "[Network]" >>$filename
  333. echo -e "Address=$address" >> $filename
  334. echo -e "Gateway=$gateway" >> $filename
  335. echo -e "DNS=8.8.8.8" >> $filename
  336. fi
  337. }
  338. fi
  339. }
  340. #
  341. # edit ip address
  342. #
  343. function ip_editor ()
  344. {
  345. exec 3>&1
  346. dialog --title " Static IP configuration" --backtitle "$BACKTITLE" --form "\nAdapter: $1
  347. \n " 12 38 0 \
  348. "Address:" 1 1 "$address" 1 15 15 0 \
  349. "Netmask:" 2 1 "$netmask" 2 15 15 0 \
  350. "Gateway:" 3 1 "$gateway" 3 15 15 0 \
  351. 2>&1 1>&3 | {
  352. read -r address;read -r netmask;read -r gateway
  353. if [[ $? = 0 ]]; then
  354. echo -e "# orangepi-config created\nsource /etc/network/interfaces.d/*\n" >$3
  355. echo -e "# Local loopback\nauto lo\niface lo inet loopback\n" >> $3
  356. echo -e "# Interface $2\nauto $2\nallow-hotplug $2\niface $2 inet static\
  357. \n\taddress $address\n\tnetmask $netmask\n\tgateway $gateway\n\tdns-nameservers 8.8.8.8" >> $3
  358. fi
  359. }
  360. }
  361. #
  362. # edit hostapd parameters
  363. #
  364. function wlan_edit_basic ()
  365. {
  366. source /etc/hostapd.conf
  367. exec 3>&1
  368. dialog --title "AP configuration" --backtitle "$BACKTITLE" --form "\nWPA2 enabled, \
  369. advanced config: edit /etc/hostapd.conf\n " 12 58 0 \
  370. "SSID:" 1 1 "$ssid" 1 31 22 0 \
  371. "Password:" 2 1 "$wpa_passphrase" 2 31 22 0 \
  372. "Channel:" 3 1 "$channel" 3 31 3 0 \
  373. 2>&1 1>&3 | {
  374. read -r ssid;read -r wpa_passphrase;read -r channel
  375. if [[ $? = 0 ]]; then
  376. sed -i "s/^ssid=.*/ssid=$ssid/" /etc/hostapd.conf
  377. sed -i "s/^wpa_passphrase=.*/wpa_passphrase=$wpa_passphrase/" /etc/hostapd.conf
  378. sed -i "s/^channel=.*/channel=$channel/" /etc/hostapd.conf
  379. wpa_psk=$(wpa_passphrase $ssid $wpa_passphrase | grep '^[[:blank:]]*[^[:blank:]#;]' | grep psk | cut -d= -f2-)
  380. sed -i "s/^wpa_psk=.*/wpa_psk=$wpa_psk/" /etc/hostapd.conf
  381. fi
  382. }
  383. }
  384. #
  385. # edit hostapd parameters
  386. #
  387. function wlan_edit ()
  388. {
  389. # select default interfaces if there is more than one
  390. select_interface "default"
  391. dialog --title " Configuration edit " --colors --backtitle "$BACKTITLE" --help-button --help-label "Cancel" --yes-label "Basic" \
  392. --no-label "Advanced" --yesno "\n\Z1Basic:\Z0 Change SSID, password and channel\n\n\Z1Advanced:\Z0 Edit /etc/hostapd.conf file" 9 70
  393. if [[ $? = 0 ]]; then
  394. wlan_edit_basic
  395. elif [[ $? = 1 ]]; then
  396. dialog --backtitle "$BACKTITLE" --title " Edit hostapd configuration /etc/hostapd.conf" --no-collapse \
  397. --ok-label "Save" --editbox /etc/hostapd.conf 30 0 2> /etc/hostapd.conf.out
  398. [[ $? = 0 ]] && mv /etc/hostapd.conf.out /etc/hostapd.conf && service hostapd restart
  399. fi
  400. }
  401. #
  402. # here we add wifi exceptions
  403. #
  404. function wlan_exceptions ()
  405. {
  406. reboot_module=false
  407. [[ -n "$(lsmod | grep -w dhd)" && $1 = "on" ]] && \
  408. echo 'options dhd op_mode=2' >/etc/modprobe.d/ap6212.conf && rmmod dhd && modprobe dhd
  409. [[ -n "$(lsmod | grep -w dhd)" && $1 = "off" ]] && \
  410. rm /etc/modprobe.d/ap6212.conf && rmmod dhd && modprobe dhd
  411. # Cubietruck
  412. [[ -n "$(lsmod | grep -w ap6210)" && $1 = "on" ]] && \
  413. echo 'options ap6210 op_mode=2' >/etc/modprobe.d/ap6210.conf && reboot_module=true
  414. [[ -n "$(lsmod | grep -w ap6210)" && $1 = "off" ]] && \
  415. rm /etc/modprobe.d/ap6210.conf && reboot_module=true
  416. }
  417. #
  418. # here add shaddy wifi adaptors
  419. #
  420. function check_and_warn ()
  421. {
  422. local shaddy=false
  423. # blacklist
  424. [[ "$LINUXFAMILY" == "sun8i" && $BOARD == "orangepizero" ]] && shaddy=true
  425. [[ -n "$(lsmod | grep mt7601u)" ]] && shaddy=true
  426. [[ -n "$(lsmod | grep r8188eu)" ]] && shaddy=true
  427. # blacklist
  428. if [[ "$shaddy" == "true" ]]; then
  429. 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
  430. fi
  431. }
  432. #
  433. # search for wlan interfaces and provide a selection menu if there are more than one
  434. #
  435. function get_wlan_interface ()
  436. {
  437. IFS=$'\r\n'
  438. GLOBIGNORE='*'
  439. WLAN_INTERFACES=($(LC_ALL=C nmcli --wait 10 dev status | grep wifi | grep disconnected |awk '{print $1}'))
  440. local LIST=()
  441. for i in "${WLAN_INTERFACES[@]}"
  442. do
  443. LIST+=( "${i[0]//[[:blank:]]/}" "" )
  444. done
  445. LIST_LENGTH=$((${#LIST[@]}/2));
  446. if [ "$LIST_LENGTH" -eq 1 ]; then
  447. WIRELESS_ADAPTER=${WLAN_INTERFACES[0]}
  448. else
  449. exec 3>&1
  450. WIRELESS_ADAPTER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
  451. --title "Select wlan interface" --clear --menu "" $((6+${LIST_LENGTH})) 40 15 "${LIST[@]}" 2>&1 1>&3)
  452. exec 3>&-
  453. fi
  454. }
  455. function select_interface ()
  456. {
  457. IFS=$'\r\n'
  458. GLOBIGNORE='*'
  459. local ADAPTER=($(nmcli device status | grep ethernet | awk '{ print $1 }' | grep -v lo))
  460. local LIST=()
  461. for i in "${ADAPTER[@]}"
  462. do
  463. local IPADDR=$(LC_ALL=C ip -4 addr show dev ${i[0]} | awk '/inet/ {print $2}' | cut -d'/' -f1)
  464. ADD_SPEED=""
  465. [[ $i == eth* || $i == en* ]] && ADD_SPEED=$(ethtool $i | grep Speed)
  466. [[ $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)
  467. LIST+=( "${i[0]//[[:blank:]]/}" "${IPADDR} ${ADD_SPEED}" )
  468. done
  469. LIST_LENGTH=$((${#LIST[@]}/2));
  470. if [ "$LIST_LENGTH" -eq 0 ]; then
  471. SELECTED_ADAPTER="lo"
  472. elif [ "$LIST_LENGTH" -eq 1 ]; then
  473. SELECTED_ADAPTER=${ADAPTER[0]}
  474. else
  475. exec 3>&1
  476. SELECTED_ADAPTER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse --title "Select $1 interface" --clear \
  477. --menu "" $((6+${LIST_LENGTH})) 74 14 "${LIST[@]}" 2>&1 1>&3)
  478. exec 3>&-
  479. fi
  480. }
  481. #
  482. # select interface if there is more than one and adjust metric
  483. #
  484. # $1 = default | all
  485. #
  486. function select_default_interface ()
  487. {
  488. ALREADY_DEFINED=$(cat /etc/iptables.ipv4.nat 2> /dev/null | grep "POSTROUTING -o" | tail -1 | awk '{ print $4 }')
  489. if [[ -n "${ALREADY_DEFINED}" ]]; then
  490. DEFAULT_ADAPTER=${ALREADY_DEFINED}
  491. else
  492. IFS=$'\r\n'
  493. GLOBIGNORE='*'
  494. if [[ $1 == "default" ]]; then
  495. local ADAPTER=($(nmcli -t -f DEVICE connection show --active))
  496. else
  497. local ADAPTER=($(nmcli device status | tail -n +2 | awk '{ print $1 }' | grep -v lo))
  498. fi
  499. local LIST=()
  500. for i in "${ADAPTER[@]}"
  501. do
  502. local IPADDR=$(LC_ALL=C ip -4 addr show dev ${i[0]} | awk '/inet/ {print $2}' | cut -d'/' -f1)
  503. ADD_SPEED=""
  504. [[ $i == eth* || $i == en* ]] && ADD_SPEED=$(ethtool $i | grep Speed)
  505. [[ $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)
  506. if [[ $1 == "default" ]]; then
  507. [[ $IPADDR != "172.24.1.1" && -n $IPADDR ]] && LIST+=( "${i[0]//[[:blank:]]/}" "${IPADDR} ${ADD_SPEED}" )
  508. else
  509. [[ $IPADDR != "172.24.1.1" ]] && LIST+=( "${i[0]//[[:blank:]]/}" "${IPADDR} ${ADD_SPEED}" )
  510. fi
  511. done
  512. LIST_LENGTH=$((${#LIST[@]}/2));
  513. if [ "$LIST_LENGTH" -eq 0 ]; then
  514. DEFAULT_ADAPTER="lo"
  515. elif [ "$LIST_LENGTH" -eq 1 ]; then
  516. DEFAULT_ADAPTER=${ADAPTER[0]}
  517. else
  518. exec 3>&1
  519. DEFAULT_ADAPTER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
  520. --title "Select default interface" --clear --menu "" $((6+${LIST_LENGTH})) 74 14 "${LIST[@]}" 2>&1 1>&3)
  521. exec 3>&-
  522. fi
  523. fi
  524. # set highest metric to default adaptor
  525. HIGHEST_METRIC=$(nmcli -t -f UUID,TYPE,DEVICE connection show --active | grep $DEFAULT_ADAPTER | sed 's/:.*$//')
  526. # set metric to 50
  527. nmcli connection modify $HIGHEST_METRIC ipv4.route-metric 50 2> /dev/null
  528. METRIC=77
  529. # set others wired
  530. REMAINING=( `nmcli -t -f UUID,TYPE,DEVICE connection show --active | grep ethernet | grep -v $DEFAULT_ADAPTER | sed 's/:.*$//'` )
  531. if [[ ${#REMAINING[@]} -ge 1 ]]; then
  532. for i in "${REMAINING[@]}"
  533. do
  534. METRIC=$(( $METRIC + 1 ))
  535. nmcli connection modify ${i} ipv4.route-metric $METRIC
  536. done
  537. fi
  538. # set other wireless
  539. METRIC=88
  540. REMAINING=( `nmcli -t -f UUID,TYPE,DEVICE connection show --active | grep wireless | grep -v $DEFAULT_ADAPTER | sed 's/:.*$//'` )
  541. if [[ ${#REMAINING[@]} -ge 1 ]]; then
  542. for i in "${REMAINING[@]}"
  543. do
  544. METRIC=$(( $METRIC + 1 ))
  545. nmcli connection modify ${i} ipv4.route-metric $METRIC
  546. done
  547. fi
  548. # create default metrics file
  549. cat <<-EOF > /etc/NetworkManager/conf.d/orangepi-default-metric.conf
  550. [connection-ethernet-gateway]
  551. match-device=interface-name:$DEFAULT_ADAPTER
  552. ipv4.route-metric=50
  553. [connection-wifi-other]
  554. match-device=type:wifi
  555. ipv4.route-metric=88
  556. [connection-ethernet-other]
  557. match-device=type:ethernet
  558. ipv4.route-metric=77
  559. EOF
  560. }
  561. #
  562. # search and connect to Bluetooth devices
  563. #
  564. function connect_bt_interface ()
  565. {
  566. IFS=$'\r\n'
  567. GLOBIGNORE='*'
  568. dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nDiscovering Bluetooth devices ... " 5 37
  569. BT_INTERFACES=($(hcitool scan | sed '1d'))
  570. local LIST=()
  571. for i in "${BT_INTERFACES[@]}"
  572. do
  573. local a=$(echo ${i[0]//[[:blank:]]/} | sed -e 's/^\(.\{17\}\).*/\1/')
  574. local b=${i[0]//$a/}
  575. local b=$(echo $b | sed -e 's/^[ \t]*//')
  576. LIST+=( "$a" "$b")
  577. done
  578. LIST_LENGTH=$((${#LIST[@]}/2));
  579. if [ "$LIST_LENGTH" -eq 0 ]; then
  580. BT_ADAPTER=${WLAN_INTERFACES[0]}
  581. dialog --backtitle "$BACKTITLE" --title "Bluetooth" --msgbox "\nNo nearby Bluetooth devices were found!" 7 43
  582. else
  583. exec 3>&1
  584. BT_ADAPTER=$(dialog --backtitle "$BACKTITLE" --no-collapse --title "Select interface" \
  585. --clear --menu "" $((6+${LIST_LENGTH})) 50 15 "${LIST[@]}" 2>&1 1>&3)
  586. exec 3>&-
  587. if [[ $BT_ADAPTER != "" ]]; then
  588. dialog --backtitle "$BACKTITLE" --title "Please wait" --infobox "\nConnecting to $BT_ADAPTER " 5 35
  589. BT_EXEC=$(
  590. expect -c 'set prompt "#";set address '$BT_ADAPTER';spawn bluetoothctl;expect -re $prompt;send "disconnect $address\r";
  591. sleep 1;send "remove $address\r";sleep 1;expect -re $prompt;send "scan on\r";sleep 8;send "scan off\r";
  592. expect "Controller";send "trust $address\r";sleep 2;send "pair $address\r";sleep 2;send "connect $address\r";
  593. send_user "\nShould be paired now.\r";sleep 2;send "quit\r";expect eof')
  594. echo "$BT_EXEC" > /tmp/bt-connect-debug.log
  595. if [[ $(echo "$BT_EXEC" | grep "Connection successful" ) != "" ]]; then
  596. dialog --backtitle "$BACKTITLE" --title "Bluetooth" --msgbox "\nYour device is ready to use!" 7 32
  597. else
  598. dialog --backtitle "$BACKTITLE" --title "Bluetooth" --msgbox "\nError connecting. Try again!" 7 32
  599. fi
  600. fi
  601. fi
  602. }