Guest User

Untitled

a guest
Aug 24th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import paramiko, sys, os
  3. import time
  4. from subprocess import call
  5. import signal
  6.  
  7. filename="/tmp/"
  8. filename+=str(time.time())
  9. filename+=".csv"
  10. sys.stdout = open(filename, "w")
  11.  
  12. run = True
  13.  
  14. def signal_handler(signal, frame):
  15. global run
  16. print ("exiting")
  17. run = False
  18.  
  19. signal.signal(signal.SIGINT, signal_handler)
  20.  
  21. hostname = "xxx.xxx.xxx.xxx"
  22. username = "xxxxxxxx"
  23. password = "xxxxxxxx"
  24. shusername = "xxxxxxxx\n"
  25. shpassword = "xxxxxxxx\n"
  26.  
  27. commandSpeed='ip -s link show ptm0\n'
  28.  
  29. lineUpSpeed=1400
  30. maxUpSpeed=1050 #max speed up torrent
  31. currentTrSpeed=100 #current speed up torrent
  32. minTrSpeed=100 #min speed up torrent
  33. stepSpeed=100
  34.  
  35. TrAddr="xxxxxxxx"
  36. TrPort="xxxxxxxx"
  37. TrUser="xxxxxxxx"
  38. TrPasswd="xxxxxxxx"
  39.  
  40. interval=10
  41. sshWait=1
  42.  
  43. client = paramiko.SSHClient()
  44. client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
  45. print("connect")
  46. client.connect(hostname, username=username, password=password)
  47. remote_conn = client.invoke_shell()
  48.  
  49. print("connected")
  50. remote_conn.send('sh\n')
  51. time.sleep(1)
  52. remote_conn.send(shusername)
  53. time.sleep(1)
  54. remote_conn.send(shpassword)
  55. time.sleep(1)
  56. output=remote_conn.recv(2024)
  57. remote_conn.send(commandSpeed)
  58. output=remote_conn.recv(2024)
  59.  
  60. upValueOLD=0
  61.  
  62. prev_time=0
  63. now_time=0
  64. upValue=0
  65. TXKbytesOLD=0
  66.  
  67. interrupted = False
  68.  
  69. while run:
  70. now_time = time.time()
  71. remote_conn.send(commandSpeed)
  72. time.sleep(sshWait)
  73. output=remote_conn.recv(2024)
  74. res=(output.decode()).split('\r\n')
  75. TXKbytes=float((res[len(res)-2].split(' '))[4])/1000
  76. upValue=(TXKbytes-TXKbytesOLD)/(now_time-prev_time)
  77.  
  78. remote_conn.send("cat /tmp/hostlist\r")
  79. time.sleep(sshWait)
  80. output=remote_conn.recv(2024)
  81. res=(output.decode()).split('\r\n')
  82. clientsConnected=len(res)
  83.  
  84. TXKbytesOLD=TXKbytes
  85. prev_time=now_time;
  86.  
  87. if upValue>maxUpSpeed:
  88. #if saturated back down fast, else backdown slow
  89. if upValue>lineUpSpeed:
  90. currentTrSpeed=int(currentTrSpeed-stepSpeed*4)
  91. else:
  92. currentTrSpeed=int(currentTrSpeed-stepSpeed)
  93. if upValue<maxUpSpeed:
  94. currentTrSpeed=int(currentTrSpeed+stepSpeed)
  95. if currentTrSpeed<minTrSpeed:
  96. currentTrSpeed=minTrSpeed
  97. if clientsConnected==3:#one client line + two useless line
  98. currentTrSpeed=lineUpSpeed
  99. #kilobytes/s
  100. print(time.time(),";",upValue,";",currentTrSpeed)
  101.  
  102. stringTr="transmission-remote "
  103. stringTr+=TrAddr
  104. stringTr+=":"
  105. stringTr+=TrPort
  106. stringTr+=" -n "
  107. stringTr+=TrUser
  108. stringTr+=":"
  109. stringTr+=TrPasswd
  110. stringTr+=" -asu "
  111. stringTr+=str(currentTrSpeed)
  112. os.system(stringTr)
  113.  
  114. prev_time = time.time()
  115. sys.stdout.flush()
  116. time.sleep(interval)
  117.  
  118. remote_conn.send('exit\n')
  119. time.sleep(sshWait)
  120. remote_conn.send('exit\n')
Add Comment
Please, Sign In to add comment