Created
September 17, 2019 15:40
-
-
Save garis/b197e0cdf9423e50b56b880503c2df8e to your computer and use it in GitHub Desktop.
torrentSpeedByPing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# obsolete with a decent qos | |
import paramiko, sys, os | |
import time | |
from subprocess import call | |
import signal | |
filename="/tmp/" | |
filename+=str(time.time()) | |
filename+=".csv" | |
#sys.stdout = open(filename, "w") | |
print("time;pingTimeAVG;") | |
run = True | |
def signal_handler(signal, frame): | |
global run | |
print ("exiting") | |
run = False | |
signal.signal(signal.SIGINT, signal_handler) | |
hostname = "192.168.1.254" # ip of the router | |
username = "xxxxxxxxxxx" | |
password = "xxxxxxxxxxx" | |
nextHop = "xxx.xxx.xxx.xxx" # ip on the internet, be carefull because hop in between may have icmp with low priority | |
pingWaitTime= 3 | |
commandSpeed='ping -c '+str(pingWaitTime)+' '+nextHop+'\n' | |
reallyMaxUpSpeed=1600 | |
currentTrSpeed=reallyMaxUpSpeed/2 #current speed up torrent | |
minTrSpeed=100 #min speed up torrent | |
stepSpeed=50 | |
targetAvgPingtime=90 | |
TrAddr="localhost" | |
TrPort="9091" | |
TrUser="xxxxxxxxxxx" | |
TrPasswd="xxxxxxxxxxx" | |
interval=10 | |
sshWait=int(pingWaitTime*1.3) | |
client = paramiko.SSHClient() | |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy) | |
print("connect") | |
client.connect(hostname, username=username, password=password) | |
remote_conn = client.invoke_shell() | |
print("connected") | |
output=remote_conn.recv(2024) | |
remote_conn.send("\n") | |
time.sleep(sshWait) | |
output=remote_conn.recv(2024) | |
prev_time=0 | |
now_time=0 | |
strMSG="" | |
avgTime=0 | |
while run: | |
now_time = time.time() | |
remote_conn.send(commandSpeed) | |
time.sleep(sshWait) | |
output=remote_conn.recv(2024) | |
res=(output.decode()).split('\r\n') | |
# round-trip min/avg/max | |
avgTime=float(((res[len(res)-2].split(' '))[3]).split('/')[1]) #avg | |
if avgTime > targetAvgPingtime: | |
newBack=avgTime/2 | |
strMSG=strMSG+"line is quite busy, backing down minus "+str(newBack) | |
currentTrSpeed=int(currentTrSpeed-newBack) | |
else: | |
strMSG=strMSG+"line is quite free, going a litle bit faster" | |
currentTrSpeed=int(currentTrSpeed+stepSpeed) | |
if currentTrSpeed<minTrSpeed: | |
strMSG=strMSG+"don't go down a min threshold" | |
currentTrSpeed=minTrSpeed | |
#se il torrent non carica molto | |
#if currentTrSpeed > upValue + 100: | |
# currentTrSpeed=maxUpSpeed-stepSpeed | |
if currentTrSpeed<minTrSpeed: | |
currentTrSpeed=minTrSpeed | |
if currentTrSpeed>reallyMaxUpSpeed: | |
currentTrSpeed=reallyMaxUpSpeed | |
print(time.time(),";",avgTime,";",currentTrSpeed,";",strMSG) | |
strMSG="" | |
stringTr="transmission-remote " | |
stringTr+=TrAddr | |
stringTr+=":" | |
stringTr+=TrPort | |
stringTr+=" -n " | |
stringTr+=TrUser | |
stringTr+=":" | |
stringTr+=TrPasswd | |
stringTr+=" -asu " | |
stringTr+=str(currentTrSpeed) | |
os.system(stringTr) | |
prev_time = time.time() | |
sys.stdout.flush() | |
time.sleep(interval) | |
time.sleep(sshWait) | |
remote_conn.send('exit\n') | |
client.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment