-
Notifications
You must be signed in to change notification settings - Fork 0
/
stamina
executable file
·126 lines (113 loc) · 3.65 KB
/
stamina
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
#!/bin/sh
#
# A script which provides a point to point connection to
# a USB modem(ex: /dev/ttyUSB*, ...). its dependencies are:
# usb_modeswitch, ModemManager, NetworkManager, systemd
#
# From: https://codeberg.org@tcheukueppo/ktoolbox.git
# Author: Kueppo Tcheukam
if test "$(uname)" != Linux ; then
printf '%s: ERROR: this is a linux script\n' "$0" >&2
exit 1
fi
if test "$(id -u)" -ne 0 ; then
printf '%s: ERROR: root permission needed\n' "$0" >&2
exit 1
fi
usage() {
cat<<EOF >&2
Usage: stamina [OPTIONS]... APN
establishes a connection to a usb modem, modems
of the type /dev/ttyUSB*, /dev/ttyS*, etc. stamina
takes as mandatory argument which is the APN.
APN depends on your service provider.
-k, --kill delete the network manager connection
called "stamina" which was created
when the usb modem was first plug-in.
EOF
}
check() {
local cmd=''
# to switch to from a storage device to a modem
# send bulk message to the device with usb_modeswitch
printf 'warn: mode switch the usb device(usb_modeswitch)\n'
printf 'checking dependencies ...'
for cmd in systemd usb_modeswitch ModemManger NetworkManager ; do
command -v "$cmd" >/dev/null || {
printf "\n%s: ERROR: \`%s' not found\n" "$0" "$cmd"
exit 1
}
done
printf 'ok'
}
servck() {
local service=
for service in ModemManger NetworkManager ; do
printf 'starting "%s" service ...' $service
systemctl start ModemManager >/dev/null 2>&1 && printf 'ok\n' || { printf 'failed\n'; exit 1; }
done
}
pppcon() {
local pport='' timeout=0
printf 'listening to usb modems ...\n'
while test "$(mmcli -L)" = "No modems were found"; do
test $timeout = 8 && break
sleep 4
timeout=$(($timeout + 1))
done
test $timeout -lt 8 && printf ok || { printf failed; return 1; }
# get modem index
index="$(mmcli -L | cut -d/ -f6 | cut -d ' ' -f1)"
if ! printf '%d\n' "$index" | grep -q '[0-9]\{1,\}' ; then
printf '%s: ERROR: could not get modem index\n' "$0" >&2
exit 1
fi
# create a persistent connection with networkmanager
if nmcli connection show 2>/dev/null | grep -q '^Stamina ' ; then
wmsg() {
printf 'warning: this script should be run once, it creates\n'
printf 'a persistent connection to a usb modem by creating\n'
printf 'an active connection with NetworkManager so run\n'
printf '`stamina -k` do delete the NetworkManager active\n'
printf 'connection.\n'
}
wmsg >&2
nmcli radio wwan on
exit 1
fi
pport="$(mmcli -m $index | fgrep 'primary port' | cut -d: -f2)"
pport=${pport## }
if test -c "/dev/$pport" ; then
nmcli radio wwan on
nmcli connection add type gsm ifname "$pport" con-name "Stamina" apn "$1"
test $? -eq 0 && printf 'usb modem connection succeeded\n'
fi
}
kilcon() {
local gguid
if ! nmcli connection show | grep -q '^Stamina ' ; then
printf '%s: ERROR: no connection found\n' "$0" >&2
return 1
fi
printf 'attempting to delete the connection ...'
gguid=$(nmcli connection show | grep '^Stamina ' | sed -n 's/^[^ ]\+ \+\([^ ]\+\).\+/\1/p')
nmcli connection delete "$gguid" && printf ok || printf failed
return 0
}
stamina() {
case $# in
0) usage >&2 ;;
1)
case $1 in
-k|--kill) kilcon ;;
-h|--help) usage ;;
*) check >&2 || exit 1
printf 'connecting to a usb modem with apn "%s"\n' "$1"
servck || return 1
pppcon "$1" || return 1 ;;
esac ;;
*) printf 'invalid number of arguments\n' >&2 ;;
esac
return 0
}
stamina "$@"