Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I use ZTE USB Modem on Natty Narwhal. Every thing works fine but sometimes it get disconnected. I want to write Shell script that reconnects mobile broadband if it is disconnected or the received data is less than 20 KB after 5 seconds of connection.

So my question is how to enable/disable mobile broadband? How to check for data received? and how to enable/disable network service ?

note: terminal commands only Or if you can write script, I'll be very thankful.

share|improve this question

3 Answers 3

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 ...

share|improve this answer
    
it takes forever to shutdown after installing this service. I mean, It do not get shutdown when I shutdown my laptop. It stuck at ubuntu logo. I tried sudo rm /etc/init.d/mobile-broadband-connect && sudo update-rc.d mobile-broadband-connect remove and removed this service. Then everything went fine. How to get rid of this? –  virpara May 16 '12 at 16:23
    
Don't put this as a service. Start it manualy. –  Subv3rsion May 16 '12 at 17:42
    
if I start it manually then will it keep running in background and connect if mobile broadband is disconnected? –  virpara May 17 '12 at 9:01
up vote 1 down vote accepted

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
share|improve this answer
#!/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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.