|
|
2010-02-10
, 23:42
|
|
Posts: 43 |
Thanked: 32 times |
Joined on Jan 2010
|
#111
|
| The Following User Says Thank You to azstunt For This Useful Post: | ||
|
|
2010-02-11
, 01:12
|
|
Posts: 5 |
Thanked: 0 times |
Joined on Jan 2010
|
#112
|

|
|
2010-02-11
, 01:16
|
|
Posts: 43 |
Thanked: 32 times |
Joined on Jan 2010
|
#113
|
|
|
2010-02-11
, 12:04
|
|
Posts: 17 |
Thanked: 14 times |
Joined on Jan 2010
@ London, UK
|
#115
|
#!/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
#!/bin/sh AUTODISCONNECT=/usr/local/bin/autodisconnect if [ ! -x $AUTODISCONNECT ]; then exit 0 fi $AUTODISCONNECT
#!/bin/sh AUTODISCONNECT=/usr/local/bin/autodisconnect if [ ! -x $AUTODISCONNECT ]; then exit 0 fi kill `pidof autodisconnect`
|
|
2010-02-11
, 13:17
|
|
Posts: 196 |
Thanked: 54 times |
Joined on Jan 2010
@ UK
|
#116
|
|
|
2010-02-11
, 13:44
|
|
Posts: 17 |
Thanked: 14 times |
Joined on Jan 2010
@ London, UK
|
#117
|
|
|
2010-02-11
, 14:11
|
|
Posts: 196 |
Thanked: 54 times |
Joined on Jan 2010
@ UK
|
#118
|
|
|
2010-02-11
, 18:35
|
|
|
Posts: 286 |
Thanked: 219 times |
Joined on Feb 2010
@ France
|
#119
|
|
|
2010-02-11
, 19:45
|
|
Posts: 17 |
Thanked: 14 times |
Joined on Jan 2010
@ London, UK
|
#120
|
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.
![]() |
|