View Single Post
stopgap's Avatar
Posts: 139 | Thanked: 135 times | Joined on Jan 2010 @ Cambridgeshire, UK
#42
I've spent some time on this script and refined it a little:

*It now only does anything if there's an active connection.
*It logs startup and disconnections to a file in mydocs/tmp
*It notifies you onscreen when disconnecting
*It distinguishes between wifi/cellular connections

I still cannot get it to autostart though - I don't get what I'm doing wrong. I have an entry in /etc/event.d as suggested and have tried everything I can see - to no avail. Ideally it needs to start once the desktop is up and loaded properly... any ideas anyone?

Here's my script for anyone who wants it

Code:
#!/bin/sh
logentry()
{
	#SEND A STRING TO THIS FUNCTION TO APPEND IT TO THE LOG FILE
	echo -e "$1" >> /home/user/MyDocs/tmp/autoDCscriptLOG.txt
}

osnotify()
{
	#SEND A STRING TO THIS FUNCTION TO GENERATE A SYSTEM POPUP
	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$1"
}

#PREPARE INITIAL VARIABLES FOR TEMPORAL MONITORING OF NETWORK INTERFACES
getcell=`ifconfig phonet0 | grep RX.p | cut -d: -f2`
getwifi=`ifconfig wlan0 | grep RX.p | cut -d: -f2`
bakcell=$getcell
bakwifi=$getwifi

getcellup=`ifconfig | grep gprs0 | cut -d: -f0`
getwifiup=`ifconfig wlan0 | grep inet.addr | cut -d: -f2`

#LOG MONITOR STARTUP TO FILE & NOTIFY USER VIA POPUP
logentry "\nIdle Monitor Startup $(date)\nEVENTS:\n"
osnotify "Automatic internet disconnection monitor has started"

#PERFORM MAIN MONITORING LOOPS
while true ;
do
	#CHANGE THE NUMBER AFTER SLEEP TO SET THE NUMBER OF SECONDS BEFORE
	#THE INTERNET IS DISCONNECTED IF NO INTERNET ACTIVITY IS DETECTED
        sleep 300

        getcell=`ifconfig phonet0 | grep RX.p | cut -d: -f2`
        getwifi=`ifconfig wlan0 | grep RX.p | cut -d: -f2`
	getcellup=`ifconfig | grep gprs0 | cut -d: -f0`
	getwifiup=`ifconfig wlan0 | grep inet.addr | cut -d: -f1`

	if [ -n "$getcellup" -o -n "$getwifiup" ]; then
		#IF EITHER NETWORK INTERFACE HAS AN IP PROCEED WITH INACTIVITY TEST

        	if [ "$getcell" == "$bakcell" -a "$getwifi" == "$bakwifi" ]; then
			#IF NO PACKETS WERE RECEIVED SINCE LAST RECORDING THEN SHUTDOWN INTERFACES

                	dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
			
			if [ -n "$getcellup" ]; then
				osnotify "The 2G/3G internet connection has been closed due to inactivity"
				logentry " - 2G/3G DISCONNECTED @ $(date)"

			elif [ -n "$getwifiup" ]; then
				osnotify "The WI-FI internet connection has been closed due to inactivity"
				logentry " - WI-FI DISCONNECTED @ $(date)"

			else
				osnotify "The internet connection has been closed due to inactivity"
				logentry " - ????? DISCONNECTED @ $(date)"
			
			fi


        	fi

		bakcell=$getcell
		bakwifi=$getwifi

	fi

done

exit
 

The Following 3 Users Say Thank You to stopgap For This Useful Post: