¿Cómo habilitar / deshabilitar la banda ancha móvil desde la terminal?


8

Uso el módem USB ZTE en Natty Narwhal. Todo funciona bien, pero a veces se desconecta. Quiero escribir un script de Shell que vuelva a conectar la banda ancha móvil si está desconectada o si los datos recibidos son inferiores a 20 KB después de 5 segundos de conexión.

¿Entonces mi pregunta es cómo habilitar / deshabilitar la banda ancha móvil? ¿Cómo verificar los datos recibidos? y cómo habilitar / deshabilitar el servicio de red?

nota: solo comandos de terminal O si puedes escribir script, te lo agradeceré mucho.

Respuestas:


8

Abra la ventana de terminal y escriba:

sudo gedit /etc/init.d/mobile-broadband-connect

Luego copie y pegue esto (Cambie según sus necesidades):

Nota: Reemplace el <Your Mobile Broadband Connection Name Here>con el nombre de su conexión.

#!/bin/bash

case "$1" in
start)
      echo "Starting Mobile Broadband Connection."
      while true; do
        # testing...to see if gsm is on the list of active devices
        LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^gsm:disconnected$"
        if [ $? -eq 0 ]; then
            break
        else
         # not connected, sleeping for a second
            sleep 1
        fi
      done
      # now once GSM modem shows up, run these commands
      nmcli -t nm wwan on
      nmcli -t con up id <Your Mobile Broadband Connection Name Here>
;;
stop)
      echo "Stopping Mobile Broadband Connection."
      nmcli -t con down id <Your Mobile Broadband Connection Name Here>
      nmcli -t nm wwan off
;;
status)
      # Check to see if the process is running with Network Manager dev status
      nmcli -p dev
;;

*)
      echo "Mobile Broadband Startup Service"
      echo $"Usage: $0 {start|stop|status}"
      exit 1
esac
exit 0

Cambie los permisos de este archivo para su ejecución:

sudo chmod +x /etc/init.d/mobile-broadband-connect

Para ejecutar este script tiene un servicio, haga:

sudo update-rc.d mobile-broadband-connect defaults

El script está registrado como un servicio de inicio del sistema para que pueda iniciar, detener o verificar el estado del script con:

sudo service mobile-broadband-connect start

sudo service mobile-broadband-connect stop

sudo service mobile-broadband-connect status

Reinicie para completar la instalación y la conexión automática.

  • Reinicie su sistema para completar la instalación.
  • Después de reiniciar, el dispositivo USB demora hasta 60 segundos.
  • Cuando está activo: la conexión de banda ancha móvil se activará y se conectará automáticamente.

Hecho ...


se tarda una eternidad en apagarse después de instalar este servicio. Quiero decir, no se apaga cuando apago mi computadora portátil. Se atascó en el logotipo de ubuntu. Intenté sudo rm /etc/init.d/mobile-broadband-connect && sudo update-rc.d mobile-broadband-connect removey eliminé este servicio. Entonces todo salió bien. ¿Cómo deshacerse de esto?
Rahul Virpara

No pongas esto como un servicio. Comience manualmente.
Octávio Filipe Gonçalves

si lo inicio manualmente, ¿seguirá funcionando en segundo plano y se conectará si se desconecta la banda ancha móvil?
Rahul Virpara

2

¡Creé un script de shell de la siguiente manera y lo puse Startup Applicationsy funciona como un encanto! Estoy contento con esto, pero si puedes mejorarlo, te lo agradeceré.

#!/bin/bash

while true; do
    LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^gsm:disconnected$"
    if [ $? -eq 0 ]; then
        #jdownloader is still in the download status so stop it because
        #internet is disconnected and jdownloader won't resume download 
        #when connected again
        #jdownloader --stop-download
        #sometimes I can not get connected after disconnection when 
        #I click on <name of the network connection>. I have to disable
        #and enable Mobile Broadband
        nmcli -t nm wwan off
        sleep 1
        nmcli -t nm wwan on
        sleep 1
        nmcli -t con up id "Tata Docomo Internet"
        #wait approximately 15 sec to get connected
        #if anyone can add better command to check for it just comment it :-p 
        sleep 15
        #now connected to internet so start download
        #jdownloader --start-download
    fi
    #it does not worth keep it checking every millisecond.
    #my connection will be reestablished within 5-15 seconds
    sleep 2
    #if anyone can code it better please feel free to comment
    #TO-DO:: check for data received. if data < 15 KB after 20 seconds of connection
    #reconnect mobile broadband connection  
done

1
#!/bin/sh 
echo "Starting Mobile Broadband Connection. Tej"
      while true; do
        # testing...to see if gsm is on the list of active devices
        LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^gsm:disconnected$"
        if [ $? -eq 0 ]; then
            break
        else
         # not connected, sleeping for a second
            sleep 1
        fi
      done
      # now once GSM modem shows up, run these commands

  while true; do
  # Enable Mobile Broadband
nmcli -t nm wwan on

  # Connect to network
nmcli -t con up id "BSNL/CellOne New GPRS/3G 1"

  # Check status if connected or not
nmcli -f device,state -t dev | grep ttyACM0 | awk -F':' '{print $2}' | { read status; }

echo $status;

if [$status == "connected"]; then
    break
else
     # not connected, sleeping for a second
    nmcli -t nm wwan off
            sleep 1
 fi
  done
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.