Advertisement
zmatt

setup-ap.sh

Sep 22nd, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -e
  4. set -o pipefail
  5. shopt -s lastpipe
  6.  
  7. name="ap0"
  8.  
  9. ssid="AP Test"
  10. psk="Hello world!"
  11. # security config
  12. proto='WPA2'
  13. key_mgmt='WPA-PSK'
  14. group='CCMP'
  15. pairwise='CCMP'
  16. # note: in north america use channel 1, 6, or 11
  17. # in most other countries use channel 1, 5, 9, or 13
  18. channel=1
  19.  
  20.  
  21. die() {
  22.     echo "$*" >&2
  23.     exit 1
  24. }
  25.  
  26. dbus-do() {
  27.     local interface="$1" path="$2" action="$3"; shift 3
  28.     set -- busctl "$action" fi.w1.wpa_supplicant1 "$path" "$interface" "$@"
  29.     #echo "DEBUG: $*" >/dev/tty
  30.     "$@"
  31. }
  32.  
  33. manager() {
  34.     dbus-do fi.w1.wpa_supplicant1 /fi/w1/wpa_supplicant1 "$@"
  35. }
  36.  
  37. interface() {
  38.     dbus-do fi.w1.wpa_supplicant1.Interface "$@"
  39. }
  40.  
  41. parse-obj() {
  42.     read t x
  43.     [[ "$t" == o && "$x" == \"/*\" ]] || die "Expecting object return value, received: $t $x"
  44.     echo "${x:1:-1}"
  45. }
  46.  
  47.  
  48. config=(
  49.     mode        u  2
  50.     ssid        s  "$ssid"
  51.     psk         s  "$psk"
  52.     proto       s  "$proto"
  53.     key_mgmt    s  "$key_mgmt"
  54.     group       s  "$group"
  55.     pairwise    s  "$pairwise"
  56.     frequency   u  $(( 2412 + ($channel - 1) * 5 ))
  57. )
  58.  
  59.  
  60. {
  61.     manager call GetInterface 's' "$name" 2>/dev/null || \
  62.     manager call CreateInterface 'a{sv}' 1 Ifname s "$name"
  63. } | parse-obj | read intf
  64.  
  65. interface $intf call RemoveAllNetworks
  66.  
  67. if [[ "$1" == '--disable' ]]; then
  68.     echo "Access point disabled"
  69.     exit 0
  70. fi
  71.  
  72. # some sources include this but it gives a warning:
  73. #interface $intf set-property ApScan 'u' 2
  74.  
  75. interface $intf call AddNetwork 'a{sv}' $(( ${#config[@]} / 3 )) "${config[@]}" | parse-obj | read net
  76. interface $intf call SelectNetwork 'o' $net
  77.  
  78. echo "Access point enabled"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement