Reply
Thread Tools
Posts: 43 | Thanked: 32 times | Joined on Jan 2010
#111
Hello everybody. Just to let you know that I've updated my script (more like the whole instructions). Now, the script only runs when a connection is active and it closes itself after the device is disconnected (instead of running at boot and staying like that). If you had already used my script, please re-read the whole instructions, a couple of things besides the script are different. Please see back here post #107 or click here http://talk.maemo.org/showpost.php?p...&postcount=107
 

The Following User Says Thank You to azstunt For This Useful Post:
Posts: 5 | Thanked: 0 times | Joined on Jan 2010
#112
Thanks for the script!

hey Azstunt can you help me with the install?

I just finish installing VIM and Rootfs

how do I install the script?
 
Posts: 43 | Thanked: 32 times | Joined on Jan 2010
#113
Originally Posted by msnger View Post
Thanks for the script!

hey Azstunt can you help me with the install?

I just finish installing VIM and Rootfs

how do I install the script?
Sorry... it's pretty straight forward as it is... google "vim manual" and give it a read.
 
Posts: 5 | Thanked: 0 times | Joined on Jan 2010
#114
thanks

123456789732
 
Posts: 17 | Thanked: 14 times | Joined on Jan 2010 @ London, UK
#115
I've merged the scripts from azstunt and calvin_42 since they both have great features - I like the visual notification for starting connection monitoring and ending a connection. if-up was a good choice too.

Seems to work quite well - but not when switching connections. For best results disconnect from a connection before connecting to another one.

Instructions for install pretty much as azstunt says. Read the comments...

/usr/local/bin/autodisconnect:
Code:
#!/bin/sh

# ---------------------------------------------------------------------------
# MODIFY THESE PARAMETERS TO SUIT
# ---------------------------------------------------------------------------

# How often to test for inactivity
g_samplerate=90

# If only a few bytes are received you may still want to disconnect.
# Specify the number of bytes per minute here.
g_dontcountbytespermin=5000

# Don't auto disconnect for the listed interfaces in g_disable
# Effectively disables this script:
#g_disable="wlan gprs"
# Disable for gprs:
#g_disable="gprs"
# Disable for wlan:
#g_disable="wlan"
# Disconnect enabled for wlan and gprs:
g_disable=

# Enable logging
#g_logging="true"
g_logging="false"

# Where to store the logfile. This is cleared when it goes over 100k.
logfile=/root/autodisconnect.log

# ---------------------------------------------------------------------------
# DON'T MODIFY ANYTHING BELOW THIS POINT
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
# GLOBALS
# ---------------------------------------------------------------------------

# Never drop openvpn. Iface is just a heading therefore not real.
g_disable="Iface tun "$g_disable

# Number of bytes to ignore for each time period
g_dontcountbytes=$((g_dontcountbytespermin*g_samplerate/60))

# ---------------------------------------------------------------------------
# FUNCTIONS
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
init()
# ---------------------------------------------------------------------------
{
    # Allow dbus to work
    [[ -e /tmp/dbus-info ]] && eval `cat /tmp/dbus-info`

    export DBUS_SESSION_BUS_ADDRESS \
           DBUS_SESSION_BUS_PID \
           DBUS_SESSION_BUS_WINDOWID

    # Don't let log file get too big
    if [[ -w "$logfile" ]]; then
        size=`stat $logfile | grep Size | awk '{ print $2; }'`
        [[ "$size" -gt 100000 ]] && :>$logfile
    fi
}

# ---------------------------------------------------------------------------
logentry()
# ---------------------------------------------------------------------------
# Send a string to this function to append it to the log file
#
# Arg 1 - The text to log
{
    [[ "$g_logging" == "true" ]] && {
        echo -e "`date` $1" >> $logfile
        echo "`date` $1"
    }
}

# ---------------------------------------------------------------------------
osnotify()
# ---------------------------------------------------------------------------
# Send a string to this function to generate a system popup
#
# Arg 1 - The text to display
{
    /usr/bin/dbus-send --type=method_call \
        --dest=org.freedesktop.Notifications \
        /org/freedesktop/Notifications \
        org.freedesktop.Notifications.SystemNoteInfoprint \
        string:"$1"
}

# ---------------------------------------------------------------------------
disconnect()
# ---------------------------------------------------------------------------
# Disconnect current connection
#
# No args
{
    dbus-send --system --dest=com.nokia.icd \
        /com/nokia/icd_ui \
        com.nokia.icd_ui.disconnect \
        boolean:true
}

# ---------------------------------------------------------------------------
disable_quit()
# ---------------------------------------------------------------------------
{
    # Disable for some interfaces
    [[ -n "$g_disable" ]] && {
        if echo "$g_disable" | grep -qs "$interface" 2>/dev/null; then
            #osnotify "Auto-disconnect disabled for $interface"
            logentry "Auto-disconnect disabled for $interface"
            exit 0
        else
            osnotify "Auto-disconnect enabled for $interface"
            logentry "Auto-disconnect enabled for $interface"
        fi
    }
}

# ---------------------------------------------------------------------------
main()
# ---------------------------------------------------------------------------
# Main entry point
{
    init

    # Log monitor startup to file & notify user via popup
    logentry "Auto-disconnect starting"
    interface=`grep -v tun /proc/net/route | tail -1 | cut -f1 | tr -d 0-9`
    logentry "Current connection : $interface"

    # Quit if this interface is in the g_disable list
    disable_quit

    while true; do
        sleep $g_samplerate
        a=`grep -v tun /proc/net/route | tail -1 |cut -f1` 
        while [[ "$a" != "Iface" ]]; do
            b=`ifconfig $a | grep RX.b | cut -d"(" -f1 | cut -d: -f2`
            c=$b
            sleep $g_samplerate
            b=`ifconfig $a | grep RX.b | cut -d"(" -f1 | cut -d: -f2`
            a=`grep -v tun /proc/net/route | tail -1 |cut -f1`
            if [ "$a" == "Iface" ]; then
                osnotify "Auto-disconnect for $interface stopped."
                logentry "$interface lost. Quitting."
                exit 0
            fi
            if [ "$b" -lt "$((c+g_dontcountbytes))" -a "$a" != "Iface" ]; then
                disconnect
                osnotify "Connection $interface closed due to inactivity"
                logentry "$interface closed. Bytes received : $((b-c))"
                exit 0
            else
                logentry "$interface: Bytes received : $((b-c))"
            fi
        done
    done
}

# Start
main

exit 0
/etc/network/if-up.d/99_autodisconnect:
Code:
#!/bin/sh

AUTODISCONNECT=/usr/local/bin/autodisconnect

if [ ! -x $AUTODISCONNECT ]; then
  exit 0
fi

$AUTODISCONNECT
/etc/network/if-down.d/99_autodisconnect:
Code:
#!/bin/sh

AUTODISCONNECT=/usr/local/bin/autodisconnect

if [ ! -x $AUTODISCONNECT ]; then
  exit 0
fi

kill `pidof autodisconnect`
Enjoy!

Last edited by mclarson; 2010-02-14 at 23:40. Reason: Don't let log file brick your phone + fixed disabler + removed break short circuit + added caveat.
 

The Following 6 Users Say Thank You to mclarson For This Useful Post:
Posts: 196 | Thanked: 54 times | Joined on Jan 2010 @ UK
#116
I have got the above script working successfully.

How do I get email to automaticaly connect to network, its set to auto update?
 
Posts: 17 | Thanked: 14 times | Joined on Jan 2010 @ London, UK
#117
Originally Posted by fred123 View Post
I have got the above script working successfully.

How do I get email to automaticaly connect to network, its set to auto update?
Mostly luck I think! My email is set to update every 5 mins and I set my internet connection so that it comes up every 10 mins. My connection idles out after 2 mins with g_samplerate=60 so the email check needs to coincide with this. I don't know what the email retry connection algorithm is but it seems to keep updated pretty well. Maybe there's a better way...
 
Posts: 196 | Thanked: 54 times | Joined on Jan 2010 @ UK
#118
Thanks I had just set mine up to
connection any
search 5mins

email to update 5mins

as you say seems to work, I will see how it goes.
 
calvin_42's Avatar
Posts: 286 | Thanked: 219 times | Joined on Feb 2010 @ France
#119
Hey mclarson!

Thanks for the merge! The script begins to be pretty clean now. I will use it in the application.

I've got a few questions :

1) Why have you chosen to keep the 2 while instructions ? -> See my script with only 1 while
2) Why have you chosen to read the RX instead of the TX value ?
3) Number of bytes to ignore for each time period : where does it come from in fact ? Broadcast ? I ask because in my tests on my device, neither the 3G nor the WLAN connexion have generated transmitted packets when I was connected with no activity. But in my case I read TX value and not the RX value.

Thanks!

Last edited by calvin_42; 2010-02-11 at 18:37.
 
Posts: 17 | Thanked: 14 times | Joined on Jan 2010 @ London, UK
#120
Originally Posted by calvin_42 View Post
Hey mclarson!

Thanks for the merge! The script begins to be pretty clean now. I will use it in the application.

I've got a few questions :

1) Why have you chosen to keep the 2 while instructions ? -> See my script with only 1 while
I didn't think too much about it. I just used the known working script. But looking at both, yours is more efficient code but run time should be the same. I just noticed I put a useless 'break' after an 'exit'.

2) Why have you chosen to read the RX instead of the TX value ?
As the original but made sense to me. TX won't usually make much traffic - I would guess it would be harder to detect an idle connection using TX.

3) Number of bytes to ignore for each time period : where does it come from in fact ? Broadcast ? I ask because in my tests on my device, neither the 3G nor the WLAN connexion have generated transmitted packets when I was connected with no activity. But in my case I read TX value and not the RX value.
Interesting. I agreed with azstunt's detection method but did not think too hard about it as it felt right. Conversely I find it hard to believe there are no TX packets - what about email checks, weather updates or whatever.

I would expect there to be a much bigger difference between idle and busy RX than idle and busy TX. More samples should be more accurate. It would take fewer TX packets to affect the measurement considerably.

For a bulk download relativey tiny ACKS would be sent, ie tiny sample, but would have lots of rx, ie big sample. My *idle* connection does about 2k per minute - there /must/ be tx in there but I've not checked what that traffic is.

I'd be intersted to see how well your tx method works.
 
Reply

Tags
automatic, connect, connect automatically, connect on activity, connect on demand, connect on use, data, data connection, disconnect, disconnect automatically, disconnect when idle, fremantle, gprs, internet, maemo, maemo 5, n900, on demand, on use


 
Forum Jump


All times are GMT. The time now is 09:47.