📜 ⬆️ ⬇️

Auto Transmission Speed ​​Control

After acquiring RPi, installing a heap of all sorts of goodies, I realized that something had to be done with the speed of transmission. When he shakes at full speed - the tracks are swinging fast, but it’s impossible to use the Internet normally. You have to constantly climb into the web interface and cut speed. If it pumps slowly, it strains the long process. It’s all pretty boring, so it was decided to automate this process.
image

As you know, transmission has the function of "alternative speed" and its launch on a schedule. At first it suited me, but still you can download it not only at night to the fullest, but also all the time when nobody uses the Internet.

Change the speed through the configuration file

In the beginning, all my thoughts fell on the configuration file transmission
/etc/transmission-daemon/settings.json

Please note that all changes in this file will take effect only if you first stop the transmission
/etc/init.d/transmission-daemon stop
make changes and run
/etc/init.d/transmission-daemon start
The parameters that interested me were:
speed-limit-down
speed-limit-up

Limit the download speed and return, respectively.
The values ​​of these parameters need to be changed depending on whether anyone is online.

Is there anyone online?

To determine the presence of someone on the network, I used the usual ping. Since all devices (PC, laptop, mobile phone) are connected to a router with DHCP, the IP addresses of the devices are assigned sequentially, which means we will cycle through the loop.
')
Thus was born the script version 1.0

 #!/bin/bash #v1.0 #  ip=192.168.1. #     n=3 #      i0=2 i=$i0 # . 0 -    ,   ; 1-  -  ,   speedlimit=0 #    ,    upspeed=50 downspeed=500 #    ,  -    upspeedlim=10 downspeedlim=10 let end=n+i result=0 #  while [ 1 = 1 ]; do #     i    end until [ "$i" -eq "$end" ];do result=$(ping -c 1 $ip$i | tail -2 | head -1 | awk '{print $4}') #    -  ,     .. . if if [ "$result" = 1 ]; then break fi let i++ done #  =1 (-  )          ,    #,   =0 ( )         ,    if [ "$result" = 1 ]; then if [ $speedlimit = 0 ]; then sudo /etc/init.d/transmission-daemon stop #   sudo perl -pi~ -e 's/"speed-limit-down":(.+)/"speed-limit-down": '$downspeedlim',/' /etc/transmission-daemon/settings.json #   sudo perl -pi~ -e 's/"speed-limit-up":(.+)/"speed-limit-up": '$upspeedlim',/' /etc/transmission-daemon/settings.json sudo /etc/init.d/transmission-daemon start speedlimit=1 fi fi if [ "$result" = 0 ]; then if [ $speedlimit = 1 ]; then sudo /etc/init.d/transmission-daemon stop #   sudo perl -pi~ -e 's/"speed-limit-down":(.+)/"speed-limit-down": '$downspeed',/' /etc/transmission-daemon/settings.json #   sudo perl -pi~ -e 's/"speed-limit-up":(.+)/"speed-limit-up": '$upspeed',/' /etc/transmission-daemon/settings.json sudo /etc/init.d/transmission-daemon start speedlimit=0 fi fi # 30  sleep 30 i=$i0 done 

He certainly is not great, but it works.

Improving further

Then I began to argue that restarting the transmission is somehow dull, the web interface does it all without rebooting, and what makes me worse.
Thanks to a friend andbas , the direction towards the transmission-remote utility was chosen. I will not paint and praise it, I will briefly say that with its help connecting to the web interface you can control the transmission. The only drawback I would call is that in the script you have to explicitly specify the username and password, although this may be possible to change.
For some simplification, I decided to use the “alternative speed” function or “turtle” hereafter, because in the web interface the icon is depicted just in the form of a turtle and intuitively hints that with its help we will “slow down”.
So, if someone is on the network, then turn on the bug, no - turn off. If we are online and want to force the download to full, then it is quite simple - click on the bug, instead of entering the settings and change the speed.

Rebirth script version 1.1
 #!/bin/bash #ver.1.1 #transmission ip:port tip=192.168.1.100:9091 #login login=* #password password=* #  ip=192.168.1. #     n=3 #      i0=2 i=$i0 let end=n+i result=0 #     start=1 #   speedlimit=0 #  while [ 1 = 1 ]; do #     i    end until [ "$i" -eq "$end" ];do result=$(ping -c 1 $ip$i | tail -2 | head -1 | awk '{print $4}') #    -  ,     .. . if if [ "$result" = 1 ]; then break fi let i=$i+1 done #  =1 (-  )          ,    #,   =0 ( )         ,    if [ "$result" = 1 ]; then if [ $start = 1 ]; then #   # ,     transmission-remote $tip -n $login:$password -as speedlimit=1 start=0 else if [ $speedlimit = 0 ]; then # ,    transmission-remote $tip -n $login:$password -as speedlimit=1 fi fi fi if [ "$result" = 0 ]; then if [ $start = 1 ]; then #   # ,     transmission-remote $tip -n $login:$password -AS speedlimit=0 start=0 else if [ $speedlimit = 1 ]; then # ,    transmission-remote $tip -n $login:$password -AS speedlimit=0 fi fi fi # 30  sleep 30 i=$i0 done 

Twenty-five again. When someone is on the network, we download slowly, do not hammer the channel, do not interfere with anyone, but the hard drive works: it is heated, it consumes energy. With my settings (10kb / s download on the turtle) it does not look quite economical, it's better to let nothing swing at all and the hard drive is not buzzing.

Put an end (possibly temporarily), version 1.2 script
 #!/bin/bash #v1.2 #transmission ip:port tip=192.168.1.100:9091 #login login=login #password password=pass #  ip=192.168.1. #     n=3 #      i0=2 i=$i0 let end=n+i result=0 #     start=1 #   speedlimit=0 #  while [ 1 = 1 ]; do #     i    end until [ "$i" -eq "$end" ];do result=$(ping -c 1 $ip$i | tail -2 | head -1 | awk '{print $4}') #    -  ,       if [ "$result" = 1 ]; then break fi let i++ done #  =1 (-  )          ,    #,   =0 ( )         ,    if [ "$result" = 1 ]; then if [ $start = 1 ]; then #   #  ,     transmission-remote $tip -n $login:$password -t all -S speedlimit=1 #    start=0 else if [ $speedlimit = 0 ]; then #  ,     transmission-remote $tip -n $login:$password -t all -S speedlimit=1 fi fi fi if [ "$result" = 0 ]; then if [ $start = 1 ]; then #   #  ,     transmission-remote $tip -n $login:$password -t all -s speedlimit=0 #    start=0 else if [ $speedlimit = 1 ]; then #  ,     transmission-remote $tip -n $login:$password -t all -s speedlimit=0 fi fi fi # 30  sleep 30 i=$i0 done 

Now everything seems to be fine ...

Finally, autoload script

I completely forgot, because the script should start with the system. Therefore, it was decided to make a demon.
 #! /bin/sh ### BEGIN INIT INFO # Provides: tsl # Required-Start: $remote_fs $syslog $all # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Example initscript # Description: This file should be used to construct scripts to be # placed in /etc/init.d. ### END INIT INFO case "$1" in start) start-stop-daemon -Sbvx /usr/bin/tsl ;; stop) sudo pkill tsl ;; *) echo "Usage: /etc/init.d/tsl-deamon {start|stop}" ;; esac : 

For beginners linuksoidov: the script must be made starting
sudo chmod +x <bash_script_name>
and put in the usr / bin folder
Put the daemon in etc / ini.d and prescribe autorun
update-rc.d <daemon_name> defaults
to remove from startup
update-rc.d -f <daemon_name> remove

* in my case, the name of the script tsl (transmission speed limiter), if you are going to change it, then you also need to change it in the daemon

Epilogue

Everything is not described in detail, this is so clear. There should also be mistakes, omissions and so on. I myself am a beginner linux (thanks to raspberries for this), so in some places nonsense are possible ...
But in general, I think the idea is clear. On the basis of this template, you can make two, three ... positional control of the download / pumping speed, depending on the number of users in the network, for example (if 1 user - one speed, 2 - another, etc.). Also, if our ip is not in order, then you can use an array of addresses and loop through the elements of the array.

Source: https://habr.com/ru/post/166821/


All Articles