Last active
August 30, 2018 12:36
-
-
Save garis/b6648b919adae0115903fa859d5ff6cc to your computer and use it in GitHub Desktop.
Get upload speed form a modem/router and set the upload limit of transmission to avoid saturation
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 | |
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;downSpeed;upSpeed;limitUpSpeed") | |
run = True | |
def signal_handler(signal, frame): | |
global run | |
print ("exiting") | |
run = False | |
signal.signal(signal.SIGINT, signal_handler) | |
hostname = "xxx.xxx.xxx.xxx" | |
username = "xxxxxxx" | |
password = "xxxxxxx" | |
shusername = "xxxxxxx\n" | |
shpassword = "xxxxxxx\n" | |
commandSpeed='ip -s link show ptm0\n' | |
lineUpSpeed=1250 | |
maxUpSpeed=1050 #max speed up torrent | |
reallyMaxUpSpeed=2000 | |
currentTrSpeed=100 #current speed up torrent | |
minTrSpeed=100 #min speed up torrent | |
stepSpeed=40 | |
TrAddr="xxxxxxx" | |
TrPort="xxxxxxx" | |
TrUser="xxxxxxx" | |
TrPasswd="xxxxxxx" | |
interval=10 | |
sshWait=1 | |
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") | |
remote_conn.send('sh\n') | |
time.sleep(1) | |
remote_conn.send(shusername) | |
time.sleep(1) | |
remote_conn.send(shpassword) | |
time.sleep(1) | |
output=remote_conn.recv(2024) | |
remote_conn.send(commandSpeed) | |
output=remote_conn.recv(2024) | |
upValueOLD=0 | |
prev_time=0 | |
now_time=0 | |
upValue=0 | |
downValue=0 | |
TXKbytesOLD=0 | |
RXKbytesOLD=0 | |
while run: | |
now_time = time.time() | |
try: | |
remote_conn.send(commandSpeed) | |
time.sleep(sshWait) | |
output=remote_conn.recv(2024) | |
res=(output.decode()).split('\r\n') | |
RXKbytes=float((res[len(res)-4].split(' '))[4])/1000 | |
TXKbytes=float((res[len(res)-2].split(' '))[4])/1000 | |
upValue=(TXKbytes-TXKbytesOLD)/(now_time-prev_time) | |
downValue=(RXKbytes-RXKbytesOLD)/(now_time-prev_time) | |
remote_conn.send("cat /tmp/hostlist\r") | |
time.sleep(sshWait) | |
output=remote_conn.recv(2024) | |
res=(output.decode()).split('\r\n') | |
clientsConnected=len(res) | |
TXKbytesOLD=TXKbytes | |
RXKbytesOLD=RXKbytes | |
prev_time=now_time; | |
#if arbitrary limit is hit | |
if upValue>maxUpSpeed: | |
#and the line is saturated | |
if upValue>lineUpSpeed: | |
#back down fast | |
currentTrSpeed=int(currentTrSpeed-stepSpeed*4) | |
else: | |
#backdown slow | |
currentTrSpeed=int(currentTrSpeed-stepSpeed) | |
else: | |
#go a bit faster | |
currentTrSpeed=int(currentTrSpeed+stepSpeed) | |
#don't go down a min threshold | |
if currentTrSpeed<minTrSpeed: | |
currentTrSpeed=minTrSpeed | |
# but if there is nobody on the network go over the limit please | |
if clientsConnected==3:#two useless line | |
currentTrSpeed=lineUpSpeed+stepSpeed*4 | |
#kilobytes/s | |
except: | |
currentTrSpeed=int(currentTrSpeed-stepSpeed*4) | |
print(time.time(),";",downValue,";",upValue,";",currentTrSpeed,";ERROR") | |
try: | |
remote_conn.send('exit\n') | |
time.sleep(sshWait) | |
remote_conn.send('exit\n') | |
client.close() | |
client.close() | |
except: | |
print(";;;;I tried to close the ssh connection but something broke") | |
client = paramiko.SSHClient() | |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy) | |
print("reconnect") | |
client.connect(hostname, username=username, password=password) | |
remote_conn = client.invoke_shell() | |
print("connected") | |
remote_conn.send('sh\n') | |
time.sleep(1) | |
remote_conn.send(shusername) | |
time.sleep(1) | |
remote_conn.send(shpassword) | |
time.sleep(1) | |
output=remote_conn.recv(2024) | |
remote_conn.send(commandSpeed) | |
output=remote_conn.recv(2024) | |
#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(),";",downValue,";",upValue,";",currentTrSpeed) | |
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) | |
remote_conn.send('exit\n') | |
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