~mil/f_scripts

ref: 74b5b03866c16eceb9cbac37a8c9f82d2f4b6404 f_scripts/scripts/f_networks -rwxr-xr-x 3.4 KiB
74b5b038Miles Alan f_maps: populate fbp strings based on mepo_ui_central_menu.sh menuoptions kbs 4 months 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
#!/usr/bin/env osh
shopt -s strict:all
DEP="networkmanager"
DEC="Allows managing gsm/wpa connections via underlying nmcli calls"
DOC="
  Allows mangaging gsm/wpa connectiona via nmcli. Allows for creating,
  deleting, enabling, and disabling networks via prompts. Prints
  status of connected networks between prompts. Also allows scanning
  wireless networks.
"

promptok() {
  [ -p /tmp/fbp.fifo ] && echo -e "\b\f\rok" > /tmp/fbp.fifo
  read -p Ok
}

addgsm() {
  [ -p /tmp/fbp.fifo ] && echo -e "\f\e\acancel" > /tmp/fbp.fifo
  read -p "Alias: " ALIAS
  [ "$ALIAS" = "cancel" ] && return

  [ -p /tmp/fbp.fifo ] && echo -e "\f\e\acancel" > /tmp/fbp.fifo
  read -p "APN: " APN
  [ "$APN" = "cancel" ] && return
 
  nmcli c add \
    type gsm \
    ifname "$(nmcli d | grep -m 1 "gsm" | cut -d' ' -f1)" \
    con-name "$ALIAS" \
    apn "$APN"
}

installpolkitrule() {
  TARGETFILE="/etc/polkit-1/rules.d/00-f_scripts-f_networks-nmplugdev.rules"

  test -f "$TARGETFILE" || {
    echo "Installing polkit rule for modemmanager"
    [ -p /tmp/fbp.fifo ] && echo -e "\a" > /tmp/fbp.fifo
    sudo mkdir -p "$(dirname "$TARGETFILE")"
    echo '
      polkit.addRule(function(action, subject) {
        if (
          action.id.indexOf("org.freedesktop.NetworkManager.") == 0 &&
          subject.isInGroup("plugdev")
        ) { return polkit.Result.YES; }
      });
    ' | sudo tee "$TARGETFILE"
    sudo chmod -R +r "$(dirname "$TARGETFILE")"
    sudo addgroup "$USER" plugdev
  }
}

addwpa() {
  [ -p /tmp/fbp.fifo ] && echo -e "\f\e\acancel" > /tmp/fbp.fifo
  read -p "SSID: " SSID
  [ "$SSID" = "cancel" ] && return

  [ -p /tmp/fbp.fifo ] && echo -e "\f\e\acancel" > /tmp/fbp.fifo
  read -p "PASSPHRASE: " PASSPHRASE
  [ "$PASSPHRASE" = "cancel" ] && 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"
}

deletenetwork() {
  echo "Delete a network:"
  networkslist
  [ -p /tmp/fbp.fifo ] && { echo -e "\b\f\r"; echo "cancel"; networkslist; } > /tmp/fbp.fifo
  read -p "Network: " NETWORK
  [ "$NETWORK" = "cancel" ] && return
  nmcli c delete "$NETWORK"
}

scanwifi() {
  nmcli d wifi list
}

status() {
  echo "Status:"
  nmcli -p device

  echo "Active networks:"
  nmcli -c no -t c show --active

  echo "All networks:"
  nmcli -c no -t c show
}

networkslist() {
  nmcli -c no -t c show | cut -d: -f1
}

ensurenmrunning() {
  rc-service networkmanager status | grep started &&
  rc-service ntpd status | grep started || 
  {
    sudo /etc/init.d/networkmanager start
    sudo /etc/init.d/ntpd start
    sudo rc-update add networkmanager boot
    sudo rc-update add ntpd boot
  }
}

togglenetwork() {
  NETWORK="$1"
  if nmcli -c no -t c show --active | cut -d: -f1 | grep "$NETWORK"; then
    nmcli c down "$NETWORK"
  else
    nmcli c up "$NETWORK"
  fi
}

main() {
  local ACTION OPTS

  env | grep -q "^$(basename "$0" | tr '[a-z]' '[A-Z]')=" || eval "$VAR"

  installpolkitrule
  ensurenmrunning

  while true; do
    status

    OPTS="addgsm addwpa deletenetwork scanwifi"
    [ -p /tmp/fbp.fifo ] && { echo -e "\b\f\r"; echo "$OPTS" | tr " " "\n"; networkslist; } > /tmp/fbp.fifo
    read -p "Action ($OPTS): " ACTION

    networkslist | grep "$ACTION" && togglenetwork "$ACTION" && promptok && continue
    "$ACTION"; promptok
  done
}

if [ -n "$1" ]; then "$@"; else main; fi