maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Maemo 5 / Fremantle (https://talk.maemo.org/forumdisplay.php?f=40)
-   -   Connect to Internet on demand & disconnect automatically, particularly for GPRS? (https://talk.maemo.org/showthread.php?t=38832)

moneytoo 2010-02-07 22:36

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Quote:

Originally Posted by Matan (Post 483281)
This script should disconnect from network (wlan or GPRS/UMTS) after 5 to 10 minutes of no received packets:

Code:

#!/bin/sh
a=`ifconfig phonet0 | grep RX.p | cut -d: -f2`
c=`ifconfig wlan0 | grep RX.p | cut -d: -f2`
b=$a
d=$c

while true ; do
        sleep 300
        a=`ifconfig phonet0 | grep RX.p | cut -d: -f2`
        c=`ifconfig wlan0 | grep RX.p | cut -d: -f2`
        if [ "$a" == "$b" -a "$c" == "$d" ] ; then
                dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
        fi
        b=$a
        d=$c
done


I replaced phonet0 with gprs0 and set it the timeout to 180 seconds and it works great!

azstunt 2010-02-07 23:35

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Quote:

Originally Posted by moneytoo (Post 515006)
I replaced phonet0 with gprs0 and set it the timeout to 180 seconds and it works great!

Are you sure that it works? Let's asume you are connected to internet via wi-fi and the connection is idle (since I don't use gprs I will ignore it here but my point should still stand... unless for gprs you actually don't receive any packets while being idle). If you input

Code:

ifconfig wlan0 | grep RX.p | cut -d: -f2
as a normal user into the terminal you get the following:

Code:

-sh: ifconfig: not found
If you run that script as a whole as a normal user you get something like (not exactly): error line 2: ifconfig: not found and error line 3: ifconfig not found and it would disconnect you after 5 mins even if you were using the internet.

Get connected again.

Now, gain root and type:

Code:

ifconfig wlan0 | grep RX.p | cut -d: -f2
You'll see that this time it does work.

If you run the script as root you don't get any errors but you also don't get disconnected. To find out why, open another terminal and run (as root).

Code:

ifconfig wlan0 | grep RX.p | cut -d: -f2
Wait about 60-120 seconds and do it again. The values are different, aren't they? Even though you aren't using the connection. Since these values are different. The script doesn't disconnect you.

azstunt 2010-02-08 00:30

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Quote:

Originally Posted by jmk (Post 515002)
Could someone do 3G compatible version of this. Change wlan0>gprs0 etc. and edit cuts?

Maybe this script should run only when connected like this /etc/networks/if-pre-up.d/ or /etc/networks/if-up.d/
to start every time a network interface is upped.

And someone should make simple control panel applet to turn this script on/off. Like wifi-switcher check the source code.

Do you want to test my script? I'm not entirely sure about it being 3G compatible but it should work. Run it from terminal first to see if it works (if you can, test it with 2g, 3g and wi-fi). If it works, change the first two sleep to 180 (or any number that you may like... I just like 180) and erase all the "echo"s. If it doesn't work, please paste here the outcomes of the script.

Code:

#!/bin/sh
while true; do
      sleep 180
      a=`tail -1 /proc/net/route | cut -f1`
      while [ "$a" != "Iface" ]
      do
            b=`ifconfig wlan0 | grep RX.p | cut -d"(" -f1 | cut -d: -f2`
            c=`expr $b + 1000`
            sleep 180
            b=`ifconfig wlan0 | grep RX.p | cut -d"(" -f1 | cut -d: -f2`
            a=`tail -1 /proc/net/route | cut -f1`
            if [ "$b" -lt "$c" -a "$a" != "Iface" ]; then
                  dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
                  sleep 10
                  a=`tail -1 /proc/net/route | cut -f1`
            fi
      done
done

I wasn't able to add any file to /etc/networks/if-pre-up.d/ or /etc/networks/if-up.d/ so I could'nt make it start like you suggested. However, I believe that it may be possible to change the contents in /etc/event.d/autodisconnect to start when a connection is established and stop when the connection is lost. Even so, I lack knowledge about that.

raily 2010-02-08 11:56

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
@azstund,

I tested your modified script and at first after bootup it disconnected me correctly. Then I tested if it leaves a used connection unchanged by listening to internet radio streams. The script did not disconnect, fine. Afterwards i closed all programs causing traffic and waited for a disconnect, but nothing happens.

Can you figure out whats wrong?

bbhl 2010-02-08 12:16

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
I would rather attach it to the cron: http://talk.maemo.org/showthread.php?p=467732 with 3 minutes period instead of global while true...

azstunt 2010-02-08 13:07

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Quote:

Originally Posted by raily (Post 516047)
@azstund,

I tested your modified script and at first after bootup it disconnected me correctly. Then I tested if it leaves a used connection unchanged by listening to internet radio streams. The script did not disconnect, fine. Afterwards i closed all programs causing traffic and waited for a disconnect, but nothing happens.

Can you figure out whats wrong?

Were you using 3g, 2g or wi-fi? Would you mind reproducing it in the terminal (with all the echos that I have above) and pasting here the outcomes, please?

EDIT: Another thing, what time did you set for sleep and how much time did you wait for a disconnection after closing all programs?

azstunt 2010-02-09 06:17

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Quote:

Originally Posted by raily (Post 516047)
@azstund,

I tested your modified script and at first after bootup it disconnected me correctly. Then I tested if it leaves a used connection unchanged by listening to internet radio streams. The script did not disconnect, fine. Afterwards i closed all programs causing traffic and waited for a disconnect, but nothing happens.

Can you figure out whats wrong?

Disregard my last reply. I have made more changes to the script up here (post # 93). Please check them out and use that script.

raily 2010-02-09 09:20

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
ok, good I was to lazy yesterday to set up the test :)
But is you Script in post #93 affecting packet connection over 3G, GPRS? I just want to disconnect WLAN, hence I don't think I can idle in ICQ when on 3G anymore if I use Script #93.

damion 2010-02-09 09:48

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
I wrote a script for /etc/network/if-up.d/ a number of weeks back to check whether the slide was shut, whether ssh was running (rarely want disconnect in this case) and if so, disconnect. It exited if no route was up. Sorting idled bytes xfered was the missing piece, thanks for this thread! I can post my script if ppl are interested.

mclarson 2010-02-09 11:41

Re: Connect to Internet on demand & disconnect automatically, particularly for GPRS?
 
Quote:

Originally Posted by damion (Post 517446)
I wrote a script for /etc/network/if-up.d/ a number of weeks back to check whether the slide was shut, whether ssh was running (rarely want disconnect in this case) and if so, disconnect. It exited if no route was up. Sorting idled bytes xfered was the missing piece, thanks for this thread! I can post my script if ppl are interested.

This sounds really useful, I for one would like to see it.

Also, when I connect to a network the email app automatically checks for new emails. If the script could also automatically bring an interface up intermittently (then normal logic to bring it down) would email be checked? This would be perfect - long battery life plus email notifications.

I would also like to be able to bring down openvpn when on my home wifi connection. It looks like this thread might make this possible too...


All times are GMT. The time now is 05:04.

vBulletin® Version 3.8.8