How to enable/disable mobile broadband from terminal?

Open terminal window and type:

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

Then copy and paste this (Change for your needs):

Note: Replace the <Your Mobile Broadband Connection Name Here> with the name of your connection.

#!/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

Change the permissions of this file for execution:

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

To run this script has a service, do:

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

The script is registered as a system startup service so you can start, stop, or check the status of the script with :

sudo service mobile-broadband-connect start

sudo service mobile-broadband-connect stop

sudo service mobile-broadband-connect status

Reboot to complete installation and auto connect.

  • Reboot your system to complete the installation.
  • After reboot it takes up to 60 seconds before the USB device is active.
  • When active - The Mobile Broadband Connection will be activated and auto connected.

Done ...


I created a shell script as follows and put that in Startup Applications and it works like a charm ! I am happy with this but if you can make it better I'll be very thankful.

#!/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

#!/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