Estou usando o Raspberry PI3 para conectar 3 modem USB automaticamente e simultaneamente. Para isso, eu uso 2 scripts: um para o modo de comutação e outro para a discagem.
Para mudar de modo, meu script é modem.sh
:
#!/bin/bash
#Script to switch_mode of mode. called from /etc/udev/rules.d/-#modem.rules.
# log_file="$HOME/modem.log"
log_file="/home/mv/modem.log"
function log(){
sed -i -e :a -e '$q;N;50,$D;ba' $log_file
echo $(date) - $* >>$log_file
}
log "Script started.."
dev="/dev/$1"
log "$dev"
info="$(/sbin/udevadm info --attribute-walk --name $dev)"
vendor_id=$(echo "$info" |grep idVendor | head -n 1 |cut -d '"' -f2 |sed 's/"//g')
product_id=$( echo "$info" |grep idProduct | head -n 1 |cut -d '"' -f2 |sed 's/"//g')
log "vendor id - $vendor_id"
log "product_id - $product_id"
/usr/sbin/usb_modeswitch -v "0x$vendor_id" -p "0x$product_id" -M "55534243123456780000000000000a11062000000000000100000000000000"
[[ "$product_id" != "1506" ]] && log "Not a modem, exit" && exit
log "Modem found."
#modem=$(readlink -f /dev/ttyUSB_utps_modem)
#[[ -z "$modem" ]] && log "device path not detected" && exit
#/usr/local/bin/dialer.sh $modem
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev | grep '\.1\/tty'); do
(
syspath="${sysdevpath%/dev}"
devname="$(udevadm info -q name -p $syspath)"
devpath="/dev/$devname"
[[ -z $devpath ]] || {
echo "Found modem control channel at: $devpath"
echo $devpath
echo "starting dialer...."
sudo /usr/local/bin/dialer.sh $devpath
}
)
done
E para discar o script é dialer.sh
:
#!/bin/bash
# Disc : 3G Dialer
#
#
#
#
#Udev rules pass 2 command line argument 1-Device, 2-status (Connect, dissconnect).
#
sleep 10
#logfile="/tmp/dialer.log"
logfile="/tmp/modem.log"
function log(){
echo "$(date) - $*">>"$logfile"
}
log "dialer started."
APN_LIST="/home/mv/apnlist"
MODEM="$1"
#log "$MODEM"
if [[ ! -c "$MODEM" ]];then
log "Modem not found."
exit
fi
#Detect Career
log "Detecting apn"
cat $MODEM >/tmp/apnno &
if [ $? -ne 0 ];then
log "Modem not responding."
exit
fi
CATID="$!"
sleep 2
echo -en "AT+CIMI\r" >$MODEM
sleep 2
kill -9 $CATID >/dev/null 2>&1
IMSI='cat -v /tmp/apnno |sed 's/\^M//g' |sed '/^$/d' |grep -v [a-zA-Z] |head -n 1'
#[[ $CIMINO =~ "^[0-9]+$" ]] || if true;then showmsg "Invalid CIMI Number." && exit ;fi
log "$IMSI"
[[ ${IMSI:3:2} -eq 0 ]] && log "CDMA Modem found." && exit
#First 3 digit of CIMINO are country code(MCC) and 5-7 digits are MNC code
APN='cat $APN_LIST |grep "${IMSI:0:3}" | grep ,${IMSI:3:2},'
[[ -z $APN ]] && log "Could not detect APN Setting." && exit
log "APN detected."
APN_NAME='echo $APN | cut -d , -f 4'
#PHONE_NO='echo $APN | cut -d , -f 3'
#PHONE_NO="*99#"
#USERNAME='echo $APN | cut -d , -f 5'
#if [[ -z $USERNAME ]];then USERNAME='echo $APN | cut -d , -f 7';fi
#PASSWORD='echo $APN | cut -d , -f 6'
#if [ -z $PASSWORD ];then PASSWORD='echo $APN | cut -d , -f 7';fi
config="$(tempfile)"
log $config
cat >$config <<EOF
[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0
Password = { }
New PPPD = yes
Phone = *99#
Idle Seconds = 0
Modem Type = Analog Modem
Stupid Mode = 1
Compuserve = 0
Baud = 9600
Auto DNS = 0
Dial Command = ATDT
Ask Password = 0
ISDN = 0
Username = { }
EOF
echo "Modem = $MODEM" >>$config
echo "Init3 = AT+CGDCONT=1,\"IP\",\"$APN_NAME\",\"\",0,0" >>$config
#echo "Phone = $PHONE_NO" >>$config
#echo "Username = $USERNAME" >>$config
#echo "Password = $PASSWORD" >>$config
#Redial untill modem is not removed physically.
log "starting dialer"
log $(whoami)
/usr/bin/wvdial -C "$config" >>$logfile &
#/usr/bin/wvdial -C "$config" >>$logfile 2>&1
if [[ $? -eq 0 ]];then
log "Dialed success"
else
log "Failed"
fi
E para automatizar esses scripts, eu uso um udev rule
. Qual é:
ACTION=="add", KERNEL=="sr[0-9]", SUBSYSTEM=="block", RUN+="/usr/local/bin/modem.sh '%k'&"
A partir desses scripts e regra, eu tenho conexão correta, mas depois de 5 a 6 minutos, ela é desconectada automaticamente.
Onde estou cometendo um erro? E como consertá-lo para que ele não seja desconectado?