Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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")
- 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 = "xxxxxxxx"
- password = "xxxxxxxx"
- shusername = "xxxxxxxx\n"
- shpassword = "xxxxxxxx\n"
- commandSpeed='ip -s link show ptm0\n'
- lineUpSpeed=1400
- maxUpSpeed=1050 #max speed up torrent
- currentTrSpeed=100 #current speed up torrent
- minTrSpeed=100 #min speed up torrent
- stepSpeed=100
- TrAddr="xxxxxxxx"
- TrPort="xxxxxxxx"
- TrUser="xxxxxxxx"
- TrPasswd="xxxxxxxx"
- 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
- TXKbytesOLD=0
- interrupted = False
- while run:
- now_time = time.time()
- remote_conn.send(commandSpeed)
- time.sleep(sshWait)
- output=remote_conn.recv(2024)
- res=(output.decode()).split('\r\n')
- TXKbytes=float((res[len(res)-2].split(' '))[4])/1000
- upValue=(TXKbytes-TXKbytesOLD)/(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
- prev_time=now_time;
- if upValue>maxUpSpeed:
- #if saturated back down fast, else backdown slow
- if upValue>lineUpSpeed:
- currentTrSpeed=int(currentTrSpeed-stepSpeed*4)
- else:
- currentTrSpeed=int(currentTrSpeed-stepSpeed)
- if upValue<maxUpSpeed:
- currentTrSpeed=int(currentTrSpeed+stepSpeed)
- if currentTrSpeed<minTrSpeed:
- currentTrSpeed=minTrSpeed
- if clientsConnected==3:#one client line + two useless line
- currentTrSpeed=lineUpSpeed
- #kilobytes/s
- print(time.time(),";",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')
Add Comment
Please, Sign In to add comment