2

My question may seem weird but it really makes sense if your internet connection keeps going off every few hours.

I want to know how I can restart the network-manager by using a shell script.

now i know that sudo service network-manager restart is how you do it in the terminal, but sadly that doesn't work on a shell script.

Any ideas ?

Rupali
  • 751
  • I am newbie with shell script. What is 'MT=C' and 'LC_ALL=C' in front of 'nmcli' for? I don't see any different with or without MT=C and LC_ALL=C when executing nmcli. –  Mar 03 '14 at 08:38

4 Answers4

4

I too had a very similar problem. My internet is so flaky that when there are power fluctuations the modem goes off line and can't be seen unless you pull the modem and plug it in again. Otherwise you can use usb_modeswitch. So I've taken the above script and tweaked it to do both tasks.

#!/bin/bash

while true; do
#Anything less than a solid connection reboots the USB modem
LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^cdma:connected$"
if [ ! $? -eq 0 ]; then

#Reset USB Modem (12d1:1001 will have to be changed to match your modem)
sudo usb_modeswitch -R -v 12d1 -p 1001
#Wait 20 Seconds before trying to bring up the Broadband connections
sleep 20
    nmcli -t nm wwan on
#Wait Another 20 Seconds then test if the connection came up on its own as it is set to auto-connect
    sleep 20
LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^cdma:disconnected$"
if [ $? -eq 0 ]; then        
    nmcli -t con up id "Zantel connection"
    sleep 15
fi
    #wait approximately 15 sec to get connected

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

Thanks!

Xt8088
  • 111
  • 1
  • 2
3

No its not weird at all, I too experience connectivity issues a lot when I use my usb net-setter modem.

enter image description here

Here's how you do it

gksu service network-manager restart

save it in a file with .sh extension and grant executable file permission by right clicking>>properties>>permissions

3

nmcli is a command-line tool for controlling NetworkManager and getting its status.

I also had same problem while using Mobile Broadband.

I have created a shell script as follows. Save it, give execution permission and Put that in Startup Applications and it works like a charm ! It will connect automatically if connection is dropped. That is what I wanted.

You need to change network id (for me it is "Tata Docomo Internet"). Replace "Tata Docomo Internet" with name of your Mobile Broadband connection name.

#!/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
Rahul Virpara
  • 11,720
  • why this is down voted? – Rahul Virpara May 25 '12 at 14:33
  • I voted you back up. People should not be allowed to down vote without a reason/comment. Nice script btw. =) – wojox May 25 '12 at 14:38
  • @wojox I had also problem with connection dropping. So searched on google and didn't find any solution. But now I don't know even when my connection disconnected and reconnected!!. BTW thanks ;-] – Rahul Virpara May 25 '12 at 14:46
  • @virpara, I'm very interested in your approach because it doesn't require intervention. What do you mean by "which network you are on"? I have a cable connection which I've called DSL Connection 1. I had asked about exactly this problem some months ago here. If you would be kind enough to take a look, I'd appreciate it! –  May 25 '12 at 16:33
1

This is a better solution to the previous issue.

I've created 2 scripts the first is this...

sudo /home/{your user name}/UMM.sh

By launching this "In Terminal" if you create an icon, you can punch in you sudo password.

The UMM.sh it calls is as follows... To make it work for you you will have to read and change the stuff at the top of the script. You may also have to change some of the sleep lines to suit your situation.

Have Fun!

#!/bin/bash

# *****************************************************************
#Change the following values based on your connection type and name

#Running 'nmcli -t -f TYPE,STATE dev'
#cdma or gsm (edit below)
ConType="cdma"

#Running 'nmcli -t -f NAME,TYPE con'
#The name of your connection in the network manager (edit below)
ConName="Zantel connection"

#Running 'lsusb' 
#Get the Vendor and Model ID for your modem (edit below)
USB_Vendor=12d1
USB_Product=1001

#Running 'ifconfig' 
#Get the Internet Connection (edit below)
IntCon=ppp0

# ******************************************************************

Network_State=$(nmcli -t -f TYPE,STATE dev)

echo -e 
echo -e "Current Network Status: \r"
echo $Network_State

i=0
error_count=0
#default 600
Good_connect_count=600
i=$[Good_connect_count - 50]
Default_ping_host="google.com"
pingtest=0

while true; do

#First off determine nature of problem if one exists
#Test to see if our connection type even exists
MT=C nmcli -t -f TYPE,STATE dev | grep -q $ConType

if [ $? -eq 0 ]; then

    #Check to see if the WWAN is enabled
    MT=C nmcli -t -f WWAN nm | grep -q "enabled"

    if [ $? -eq 0 ]; then

        #Check to see if we have a ppp0 connection
        MT=C ifconfig | grep -q $IntCon

        if [ $? -eq 0 ]; then
            error_count=$error_count
        else

            dt=$(date)
            echo At $dt we have no $IntCon connection...
            echo Attempting to start $ConName
            nmcli -t con up id "$ConName"
            sleep 5
            MT=C ifconfig | grep -q $IntCon

            if [ $? -eq 0 ]; then
                #error_count=0
                i=$Good_connect_count
            else
                error_count=$[$error_count+1]
                echo "$error_count Error(s) in a row"
            fi


        fi
    else

        dt=$(date)
        echo At $dt we have no WWAN...attempting to Enable
        nmcli -t nm wwan on
        sleep 5

        MT=C nmcli -t -f WWAN nm | grep -q "enabled"

        if [ $? -eq 0 ]; then
            error_count=$error_count
        else
            error_count=$[$error_count+1]
            echo "$error_count Error(s) in a row"
        fi
    fi

else
    dt=$(date)
    echo At $dt we dont have our $ConType connection
    echo Rebooting USB Device $USB_Vendor : $USB_Product
    #Reset USB Modem
    usb_modeswitch -Q -R -v $USB_Vendor -p $USB_Product
    error_count=0
    sleep 25
fi

if [ $error_count -ge 3 ]; then
    dt=$(date)
    echo We have an issue. Rebooting USB Device at $dt. 
    usb_modeswitch -Q -R -v $USB_Vendor -p $USB_Product
    sleep 25
    error_count=0
    i=0
fi

i=$[$i+1]
if [ $i -ge $Good_connect_count ]; then
    dt=$(date)
    if [ $pingtest -eq 0 ]; then

        MT=C ping -c1 $Default_ping_host | grep -q "64 bytes from"
        if [ $? -eq 0 ]; then
            echo At $dt connection is up and ping test passed!
            #error_count=0
            i=0
            pingtest=1
        else
            echo Connection is present but cant confirm ping connectivity. Retrying...
            error_count=$[$error_count+1]
            echo "$error_count Error(s) in a row"
        fi
    else
        pingtest=0


        MT=C wget --spider http://www.hp.com 2>&1 | grep -q "200"
        if [ $? -eq 0 ]; then
            echo At $dt connection is up and http test passed!
            error_count=0
            i=0
        else
            echo Connection is present but cant confirm http connectivity. Retrying...
            error_count=$[$error_count+1]
            echo "$error_count Error(s) in a row"
        fi

    fi
fi

done
Xt8088
  • 111
  • 1
  • 2