Page 1 of 1

[Script] Detect machines on LAN & adjust Deluge limits

Posted: Tue Jan 10, 2012 8:44 pm
by lebrun
Since I share my internet connection with other people I started using Deluge's bandwidth scheduler plugin to avoid hogging the connections (and avoid conflicts)

I set Deluge max to 60KBps, and on the evening hours it goes down to 40KBps.

But during the day, even this seemed wasteful if no one was home. Increasing the max (or setting it to unlimited) would make the connection useless for anyone working from home (and yes, sometimes that includes me)

So I made a small script to be run via cron say, every 5 minutes. It uses smbtree to count the number of Windows machines on the network, excluding the current host. When no other machine is found, deluge is set for a higher max download limit, and when other machines are detected, it reverts deluge to more modest settings.

Here is the code, which is also hosted at: https://github.com/acarroz/autobw

Code: Select all

#!/bin/bash

logfile=$(echo "$0.log" | sed "s/.sh//g")

maxdownspeed="90.0"
maxupspeed="30.0"

regdownspeed="60.0"
regupspeed="15.0"

wincount=$(smbtree -S -N -b | grep '\\\\' | grep -v "`hostname`" | wc -l)
currdownspeed=$(deluge-console "config max_download_speed" | sed 's/ max_download_speed: //g')
now=$(date +"%F %T")

if [ $wincount -gt 0 ]; then
if [ $currdownspeed != $regdownspeed ]; then
deluge-console "config --set max_download_speed $regdownspeed"
    deluge-console "config --set max_upload_speed $regupspeed"

    echo "$now *REG* $wincount hosts found" >> $logfile
  fi
else
if [ $currdownspeed != $maxdownspeed ]; then
deluge-console "config --set max_download_speed $maxdownspeed"
    deluge-console "config --set max_upload_speed $maxupspeed"

    echo "$now *MAX* $wincount hosts found" >> $logfile
  fi
fi

The script will only detect Windows (or Samba) hosts, but for my particular situation, I can live with that.

Re: [Script] Detect machines on LAN & adjust Deluge limits

Posted: Wed Jan 11, 2012 12:36 am
by Cas
Thanks for this. Moved to the Plugins forum as its the most relevant place.

Re: [Script] Detect machines on LAN & adjust Deluge limits

Posted: Wed Jan 11, 2012 5:18 pm
by CSB
Very interesting idea, a good solution for those who can't or don't want to run QoS, or manually manage the box (or allow others to) unfortunately it's no good for looking for things like set-top boxes, or if the people you share your connection with are in the habit of leaving their computers on.

I probably would have used arp to find MACs, since I'm not a Windows user.

Re: [Script] Detect machines on LAN & adjust Deluge limits

Posted: Wed Jan 11, 2012 7:26 pm
by lebrun
I'm not a Windows user either (at home) but the other guys I share the apartment with are. Would arp work without root (or sudo) ? The other commands I researched required root, and I really don't want to give that kind of permissions to the users this runs under.

Re: [Script] Detect machines on LAN & adjust Deluge limits

Posted: Thu Jan 12, 2012 8:06 am
by CSB
On both my OS X and Ubuntu machines, arp works under my user accounts.

You'd probably want to use arp-scan, though, which does require to be run as root, but let's not forget about setuid!

Re: [Script] Detect machines on LAN & adjust Deluge limits

Posted: Thu Jan 12, 2012 12:20 pm
by johnnyg
Most routers' status page lists which clients are connected; you could write a script to scrape that and adjust deluge accordingly although I think at this point configuring QoS is probably simpler.

Re: [Script] Detect machines on LAN & adjust Deluge limits

Posted: Thu Jan 12, 2012 12:39 pm
by Cas
lebrun wrote: The other commands I researched required root, and I really don't want to give that kind of permissions to the users this runs under.
You can give a user access to a root only command by adding the user and command to the sudoers file.

http://www.cyberciti.biz/tips/allow-a-n ... -root.html
johnnyg wrote:Most routers' status page lists which clients are connected;
Interesting idea but relies on a router's client list that is correct as I've found mine to either hold onto clients too long or just not see others. :)

Re: [Script] Detect machines on LAN & adjust Deluge limits

Posted: Wed Jan 18, 2012 6:33 pm
by lebrun
I forgot about the sudoers options.I'd do some experiments over the weekend. Maybe I can add a new scanning mode to the script. Also I think a config file is necessary.