4

Is there any program or setting which can automatically disable a network connection after 5 minutes when it is enabled manually?

So that the user will have to enable the connection every 5 minutes manually, if they need it more than 5 minutes.

  • 1
    Not that I know, but you can easily write a script using the command nmcli to control NetworkManager. Wait for the connection to go up (you can ping google every 30 s or something like that), then wait five minutes, and then switch connection off. Look at man nmcli for instructions. – Rmano Mar 11 '14 at 14:32
  • pinging every 30s is not so interesting. besides bothering google, it is volatile and may hinder things on some computers. And network connection may be internal, virtual or so.. – Minimus Heximus Mar 11 '14 at 15:07
  • How should the user re-enable? Do you want the process to be user-interactive or operate in background? It would be nice if you expand your specifications.. – rusty Mar 15 '14 at 04:38
  • @rusty: the process will be hidden, user will look at network icon in plasma or gnome to see if it is disabled. if so, he'll enable it by clicking on the network icon or so. – Minimus Heximus Mar 15 '14 at 04:41

2 Answers2

1

No need to ping google, you can check the status of the network directly with nmcli.

Here's a script I whipped up for ya real quick:

#!/bin/bash

while :; do
    if $(nmcli nm enable | grep -q enabled); then
        echo 'Found connection! You got 5 minutes!'
        sleep 300
        nmcli nm enable false
    else
        echo 'No connection, checking again in 30s.'
        sleep 30
    fi
done

Just run this script at startup and it will stay running forever, constantly checking for an internet connection, and if it finds one it will disable it 5 minutes later.

robru
  • 664
  • I'd like to connect or disconnect a particular network cable (a network connection as titled); and not networking completely, something like this. btw way very brilliant script and I may be able to change it to get ny answer. – Minimus Heximus Mar 15 '14 at 05:03
  • Yes, just read the man page for nmcli and it will tell you what commands you want to substitute. Should not be difficult at all, just make sure to change both the invocations of nmcli in the script (the one that checks and the one that disables). They both have to point at the same connection or they'll get confused. – robru Mar 15 '14 at 05:35
0

Just to archive the answer:

#!/bin/bash

while :; do    
    if $(nmcli dev list iface eth0 | grep -q "not connected"); then
        sleep 60        
    else        
        sleep 300        
        nmcli dev disconnect iface eth0
    fi
done

where eth0 is the connection that is to be disconnected.