-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunctions.sh
463 lines (386 loc) · 12.2 KB
/
functions.sh
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/bin/bash
#
# Open3G Shell Version
# title :open3gs
# file :functions.sh
# description :Functions to setup modem and handle connection
# author :protetore
# date :20150621
# version :0.1
# bash_version :4.1.5(1)-release
#=============================================================
BASE_DIR=$(dirname $(/bin/readlink -f $0))
. $BASE_DIR/variables.sh
checkDependencies()
{
retval=0;
IFS=","
for dep in $DEPENDENCIES; do
if [ ! $(/usr/bin/which $dep) ]; then
$ECHO "ERROR: Dependency no found: $dep"
retval=1
fi
done
return $retval
}
modemAttached()
{
# Find where modem is
attachedDevice=$(dmesg | $GREP GSM | $GREP attached | $AWK '{ print $12 }' | $HEAD -1)
if [ "$attachedDevice" == "" ]; then
$ECHO "ERROR: Modem not found. (1)";
return 1;
fi
if [ ! -f "/dev/$attachedDevice" ]; then
$ECHO "ERROR: Modem not found. (2)";
return 1;
fi
MODEM=/dev/$attachedDevice
return 0;
}
checkModem()
{
# Find where modem is
attachedDevice=$(dmesg | $GREP GSM | $GREP attached | $AWK '{ print $12 }' | $HEAD -1)
if [ "$attachedDevice" == "" ]; then
$ECHO "ERROR: Modem not found. (1)";
return 1;
fi
if [ ! -c "/dev/$attachedDevice" ]; then
$ECHO "ERROR: Modem not found. (2)";
return 1;
fi
$ECHO "INFO: Modem found in /dev/$attachedDevice"
MODEM=/dev/$attachedDevice
# Connect to the modem and capture the apn
# Initialize modem
$STTY -F $MODEM 9600
# Listen to its output
$RM -f $LOG_DIR/modem.out
$CAT $MODEM > $LOG_DIR/modem.out &
catPid=$?
# Send some basic commands
$ECHO "AT" > $MODEM
$ECHO "AT+CFUN=1" > $MODEM
$ECHO "AT+COPS=3,0" > $MODEM
$ECHO "AT+COPS?" > $MODEM
# Kill cat session
$KILL $catPid
# Read modem response
apnCode=$($CAT $LOG_DIR/modem.out)
apnCode=$($ECHO "$apnCode" | $GREP "+COPS:" | $AWK -F"," '{ print $3 }' | $SED -e 's/"//g')
if [ "$apnCode" == "" ]; then
$ECHO "ERROR: APN could not be detected! Modem sent an empty reply.";
return 1;
fi
$confFile=$($GREP -l "COD=$apnCode" -r $BASE_DIR/$PROVIDERS_DIR/*.conf)
if [ "$confFile" == "" ]; then
$ECHO "ERROR: No config file for APN: $apnCode in $BASE_DIR/$PROVIDERS_DIR/";
return 1;
fi
OP=$(basename $conFile)
OP=$($ECHO ${OP%.conf})
return 0;
}
reconfigure()
{
retval=0
if [ ! "$1" == "" ]; then
$ECHO "@ Loadind specific config ($1)..."
confFile=$1
else
$ECHO "@ Checking modem..."
checkModem
if [ "$?" == "1" ]; then
exit 1
fi
confFile=$BASE_DIR/$PROVIDERS_DIR/$OP.conf
fi
$RM -f $BASE_DIR/$WVDIAL_DIR/default.conf
$RM -f $BASE_DIR/$CONF_DIR/chat_default.conf
$RM -f $BASE_DIR/$CONF_DIR/ppp_default.conf
if [ \( ! -f $confFile \) -a \( ! -f $BASE_DIR/$PROVIDERS_DIR/$confFile \) ]; then
$ECHO "ERROR: Config file $confFile not found!"
retval=1
fi
#IFS="\n"
#confs=( $(cat "$BASE_DIR/$PROVIDERS_DIR/$OP.conf") )
#for conf in $confs
#do
# echo $conf
#done
APN=$($GREP -m 1 APN "$confFile" | $SED '/^#.*$/d; s/APN=//')
USR=$($GREP -m 1 USR "$confFile" | $SED '/^#.*$/d; s/USR=//')
PWD=$($GREP -m 1 PWD "$confFile" | $SED '/^#.*$/d; s/PWD=//')
ATD=$($GREP -m 1 ATD "$confFile" | $SED '/^#.*$/d; s/ATD=//')
COD=$($GREP -m 1 COD "$confFile" | $SED '/^#.*$/d; s/COD=//')
if [ "$APN" == "" ]; then
$ECHO "ERROR: No APN (apn url) setting in $confFile!"
retval=1
fi
if [ "$USR" == "" ]; then
$ECHO "ERROR: No USR (user) setting in $confFile!"
retval=1
fi
if [ "$PWD" == "" ]; then
$ECHO "ERROR: No PWD (password) setting in $confFile!"
retval=1
fi
if [ "$ATD" == "" ]; then
$ECHO "ERROR: No ATD (phone to dial) setting in $confFile!"
retval=1
fi
if [ "$COD" == "" ]; then
$ECHO "ERROR: No COD (operator code) setting in $confFile!"
retval=1
fi
if [ ! -f $BASE_DIR/$CONF_DIR/pppd.conf ]; then
$ECHO "ERROR: PPPD config file $BASE_DIR/$CONF_DIR/pppd.conf not found!"
retval=1
fi
if [ ! -f $BASE_DIR/$CONF_DIR/chat.conf ]; then
$ECHO "ERROR: CHAT config file $BASE_DIR/$CONF_DIR/chat.conf not found!"
retval=1
fi
if [ ! -f $BASE_DIR/$WVDIAL_DIR/template.conf ]; then
$ECHO "INFO: No template found in $BASE_DIR/$WVDIAL_DIR/template.conf..."
retval=1
fi
if [ "$retval" == "1" ]; then
exit 1
fi
PPP_OPTS=$($CAT $BASE_DIR/$CONF_DIR/pppd.conf | $TR '\n' ' ')
PPP_OPTS=$($ECHO $PPP_OPTS | $SED "s|__MODEM__|$MODEM|g" | $SED "s/__USR__/$USR/g" | $SED "s/__PWD__/$PWD/g")
CHAT_OPTS=$($CAT $BASE_DIR/$CONF_DIR/chat.conf | $TR '\n' ' ')
CHAT_OPTS=$($ECHO $CHAT_OPTS | $SED "s/__APN__/$APN/g" | $SED "s/__ATD__/$ATD/g")
$ECHO $CHAT_OPTS > $BASE_DIR/$CONF_DIR/chat_default.conf
$ECHO $PPP_OPTS > $BASE_DIR/$CONF_DIR/ppp_default.conf
WVDIAL_OPTS=$($CAT $BASE_DIR/$WVDIAL_DIR/template.conf)
WVDIAL_OPTS=$($ECHO $WVDIAL_OPTS | $SED "s|__MODEM__|$MODEM|g" | $SED "s/__USR__/$USR/g" | $SED "s/__PWD__/$PWD/g" | $SED "s/__APN__/$APN/g" | $SED "s/__ATD__/$ATD/g")
$ECHO "$WVDIAL_OPTS" > $BASE_DIR/$WVDIAL_DIR/default.conf;
$ECHO "@ Finished configuring."
return $retval;
}
install()
{
$ECHO "@ SETUP"
if [ "$EUID" -ne 0 ]; then
$ECHO "Please execute install with root privileges"
exit 1
fi
$ECHO "@ Checking dependencies..."
checkDependencies
if [ "$?" == "1" ]; then
exit 1
fi
$ECHO "@ Checking scripts..."
if [ "$0" == "/usr/bin/open3gs" ]; then
$ECHO "ERROR: Setup function must be called from the sh script not from the link in /usr/bin/"
$ECHO "Call: $BASE_DIR/open3gs install"
exit 1
fi
$ECHO "@ Configuring logrotate..."
if [ -d /etc/logrotate.d ];
then
if [ -f /etc/logrotate.d/open3gs ]; then
$ECHO "INFO: Previous logrotate file found. Erasing it..."
$RM -f /etc/logrotate.d/open3gs
fi
# $CAT > /etc/logrotate.d/open3gs <<- END_CFG
# $LOG_DIR/*log {
# missingok
# notifempty
# size 30k
# weekly
# create 0600 root root
# }
# END_CFG
else
$ECHO "INFO: Logrotate not found. Log will grow without supervision in $LOG_DIR"
fi
$ECHO "@ Creating logs directory..."
if [ ! -d $LOG_DIR ]; then
$MKDIR $LOG_DIR
else
$ECHO "INFO: Logs directory already exists. Skipping..."
fi
if [ ! -h /usr/bin/open3gs ]; then
$ECHO "@ Linking open3gs script to /usr/bin/..."
SCRIPT_PATH=$(/bin/readlink -f $0)
$LN -s $SCRIPT_PATH /usr/bin/open3gs
fi
$ECHO "@ Detectiong modem and configuring defaut settings..."
reconfigure
if [ "$?" == "1" ]; then
exit 1
fi
$ECHO "@ Finished configuring Open3Gs."
$ECHO
usage
}
initConn()
{
$ECHO "@ Activating modem..."
# Initialize modem
$STTY -F $MODEM 9600
# Listen to its output
$RM -f $LOG_DIR/modem.out
$CAT $MODEM > $LOG_DIR/modem.out &
catPid=$?
# Send some basic commands
$ECHO "AT" > $MODEM
$ECHO "AT+CFUN=1" > $MODEM
# Kill cat session
$KILL $catPid
$ECHO "@ Setting up first modem connection..."
# Calling pppd to initialize whatever is needed by the modem
chatOpts=$($CAT $BASE_DIR/$CONF_DIR/chat_default.conf)
pppdOpts=$($CAT $BASE_DIR/$CONF_DIR/ppp_default.conf)
chatOpts=$($ECHO $chatOpts | $SED 's/\"/\\\"/g')
#eval "$PPPD $pppdOpts \"$CHAT -v $chatOpts\""
command=($PPPD $pppdOpts "\"$CHAT -v $chatOpts\"")
"${command[@]}"
cmdPid=$!
sleep 5
$KILL -9 $cmdPid
return 0
}
wvdial()
{
if [ ! -f $BASE_DIR/$WVDIAL_DIR/default.conf ]; then
$ECHO "ERROR: WVDial default.conf config file not found. Run 'open3gs -r' or 'open3gs -i'. (3)"
exit 1
else
$ECHO "INFO: Using default wvdial file. You can reconfigure with 'open3gs -r'."
fi
initConn
$ECHO "@ Dialing..."
ppid=$(nohup $WVDIAL -C $BASE_DIR/$WVDIAL_DIR/default.conf > $LOG_DIR/open3gs.log 2>&1 & echo $!)
# writePID $ppid
return 0
}
openDial()
{
initConn
# Calling pppd to initialize whatever is needed by the modem
chatOpts=$($CAT $BASE_DIR/$CONF_DIR/chat_default.conf)
pppdOpts=$($CAT $BASE_DIR/$CONF_DIR/ppp_default.conf)
chatOpts=$($ECHO $chatOpts | $SED 's/\"/\\\"/g')
#eval "$PPPD $pppdOpts \"$CHAT -v $chatOpts\""
command=($PPPD $pppdOpts "\"$CHAT -v $chatOpts\"")
"${command[@]}"
cmdPid=$!
ppid=$(nohup connWatcher $cmdPid > $LOG_DIR/open3gs.log 2>&1 & echo $!)
# writePID $ppid
exit 0
}
connWatcher()
{
if [ "$1" == "" ]; then
$ECHO "ERROR: No PID to watch. Exiting now..."
exit 1
fi
while :; do
procNum=$($PS -ef | $GREP -v "grep" | $GREP "$1" | $GREP "pppd" | $WC -l)
if [ $procNum -eq 0 ]; then
$ECHO "INFO: pppd process (PID $1) not found. Dialing again..."
openDial
exit 0
fi
sleep 3
done
}
connect()
{
checkDependencies
if [ "$?" == "1" ]; then
exit 1
fi
modemAttached
if [ "$?" == "1" ]; then
exit 1
fi
if [ "$1" == "0" ]; then
reconfigure
if [ ! $? ];
then
exit 1
fi
fi
if [ ! "$2" == "" ]; then
reconfigure $2
if [ ! $? ];
then
exit 1
fi
fi
if [ ! -f $BASE_DIR/$CONF_DIR/chat_default.conf ]; then
$ECHO "ERROR: Config file not found. Run 'open3gs -r' or 'open3gs -i'. (1)"
exit 1
fi
if [ ! -f $BASE_DIR/$CONF_DIR/ppp_default.conf ]; then
$ECHO "ERROR: Config file not found. Run 'open3gs -r' or 'open3gs -i'. (2)"
exit 1
fi
if [ "$3" == "0" ]; then
$ECHO "@ Using experimental connection mode (no third party dialer)..."
openDial
else
$ECHO "@ Using wvdial to control connection.."
wvdial
fi
$ECHO "@ Connection active. Output can be found in $LOG_DIR/open3gs.log"
}
disconnect()
{
$ECHO "@ Stopping active connection..."
$KILLALL -9 wvdial
$KILLALL -9 pppd
scriptName=$(basename "$0")
procPid=$($PS -ef | $GREP -v "grep" | $GREP "$scriptName" | $AWK '{ print $2 }')
$KILL -9 $procPid
}
reconnect()
{
$ECHO "@ Disconnecting..."
disconnect
sleep 2
$ECHO "@ Reconnecting..."
connect $1 $2 $3
}
connStatus()
{
$ECHO "Work in progress.."
}
usage()
{
echo ">> Usage: open3gs [command] [option|optional]"
echo
echo "COMMANDS"
echo "--------"
echo " install First step: create link to bin, logs dir, configure logrotate, detect modem, carrier..."
echo " status Status of the connection"
echo " connect Connect to the auto-detected carrier"
echo " disconnect Close active connection"
echo " reconnect Finishes the actual connection/process and try to connect again"
echo " reconfigure Detect the modem and APN and build the default configuration file"
echo " help Display this screen"
echo
echo "OPTIONS"
echo "-------"
echo " -a Always detect modem, APN, configure the modem when dialing (Use with the -c option)"
echo " -e Experimental dialing option, don't use wvdial or other third party tools, use only pppd and basic linux tools"
echo " -f Force to use a specific config file (Use with -n or -c options)."
# echo " -p PID file. Useful when running more than one instance of open3gs"
echo
echo "EXAMPLES"
echo "--------"
echo " Connect using settings created by 'install' or 'reconfigure' or using specific config file"
echo " > open3gs connect"
echo " > open3gs connect -f /opt/open3gs/providers.d/oi.conf"
echo
echo " Configure open3gs auto-detecting settings or with specific file:"
echo " > open3gs reconfigure"
echo " > open3gs reconfigure -f /opt/open3gs/providers.d/oi.conf"
echo
}