Auto disconnect network after 20 minutes of inactivity

The place to post if you need help or advice

Moderators: ChrisThornett, LXF moderators

Auto disconnect network after 20 minutes of inactivity

Postby serene » Tue Mar 26, 2013 12:50 pm

Hi.

I wish to have my network disconnect after 20 minutes of inactivity.
I'm using the network-manager-gnome.

1. To determine if there are any network connections;
$ iptstate -1 -t | grep -c "Total States: 0"

0 = There are connections
1 = There are no connections

2. To disable the network;
$ nmcli nm enable false

It's been a while, my bash scripting is rusty.
Could someone please recommend a simple bash script I could run a cron job every 15-20 minutes that would check if there had been zero network activity so I can disable the network ?

Thank you.
Serene
User avatar
serene
LXF regular
 
Posts: 111
Joined: Sun Jun 19, 2005 12:43 pm

Postby nelz » Tue Mar 26, 2013 2:39 pm

Code: Select all
iptstate -1 -t | grep -q "Total States: 0" && nmcli nm enable false


Provided iptstate shows details for all connections since the last run and not just the current time.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8497
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby serene » Wed Mar 27, 2013 12:07 pm

Thanks Nelz.

I didn't explain well enough.

Every 10 minutes, check for network activity...
if none, note.

Revisit 10 minutes later, if there is 'still' no network activity,
disconnect.

Thank you.
Serene
User avatar
serene
LXF regular
 
Posts: 111
Joined: Sun Jun 19, 2005 12:43 pm

Postby nelz » Wed Mar 27, 2013 4:20 pm

Something like this?
Code: Select all
if iptstate -1 -t | grep -qv "Total States: 0"; then
  rm -f /tmp/no-network-activity
  exit
  fi

if [[ -f /tmp/no-network-activity ]]; then
  nmcli nm enable false
  rm -f /tmp/no-network-activity
else
  touch /tmp/no-network-activity
  fi
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8497
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby serene » Wed Mar 27, 2013 5:49 pm

Thank you so much Nelz.

Mine didn't look anything like that.

Beau-ti-fulllllllll. Much appreciated.

Odd how there isn't a feature in gnome or network-manager
to acomplish this.

Best regards,
Serene.

[SOLVED]
User avatar
serene
LXF regular
 
Posts: 111
Joined: Sun Jun 19, 2005 12:43 pm

Postby nelz » Wed Mar 27, 2013 6:10 pm

serene wrote:Odd how there isn't a feature in gnome or network-manager to acomplish this.


Maybe because no one has asked for it? I can't think of a need for it myself.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8497
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK


Return to Help!

Who is online

Users browsing this forum: No registered users and 3 guests