[Script] Detect machines on LAN & adjust Deluge limits

Suggest, post, or discuss plugins for Deluge
Post Reply
lebrun
New User
New User
Posts: 3
Joined: Tue Jan 10, 2012 7:41 pm

[Script] Detect machines on LAN & adjust Deluge limits

Post 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.
Cas
Top Bloke
Top Bloke
Posts: 3679
Joined: Mon Dec 07, 2009 6:04 am
Location: Scotland

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

Post by Cas »

Thanks for this. Moved to the Plugins forum as its the most relevant place.
CSB
Leecher
Leecher
Posts: 66
Joined: Fri Dec 03, 2010 1:55 am

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

Post 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.
lebrun
New User
New User
Posts: 3
Joined: Tue Jan 10, 2012 7:41 pm

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

Post 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.
CSB
Leecher
Leecher
Posts: 66
Joined: Fri Dec 03, 2010 1:55 am

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

Post 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!
johnnyg
Top Bloke
Top Bloke
Posts: 1522
Joined: Sun Oct 28, 2007 4:00 am
Location: Sydney, Australia

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

Post 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.
Cas
Top Bloke
Top Bloke
Posts: 3679
Joined: Mon Dec 07, 2009 6:04 am
Location: Scotland

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

Post 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. :)
lebrun
New User
New User
Posts: 3
Joined: Tue Jan 10, 2012 7:41 pm

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

Post 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.
Post Reply