~mil/f_scripts

ref: 1db55f84eb61328d80e363858c90645e60b8c887 f_scripts/scripts/f_networks -rwxr-xr-x 3.1 KiB
1db55f84Miles Alan Regenerate README documentation 10 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
#!/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
}

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

  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