Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import sys
  2. import time
  3. import select
  4. import paramiko
  5.  
  6. host = '169.254.115.1'
  7. i = 1
  8.  
  9. #
  10. # Try to connect to the host.
  11. # Retry a few times if it fails.
  12. #
  13. while True:
  14. print ('Trying to connect to %s (%i/3)' % (host, i))
  15.  
  16. try:
  17. ssh = paramiko.SSHClient()
  18. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  19. ssh.connect(host, port=22, username='user', password='user')
  20. print ("Connected to %s" % host)
  21. break
  22. except paramiko.AuthenticationException:
  23. print ("Authentication failed when connecting to %s") % host
  24. sys.exit(1)
  25. except:
  26. print ("Could not SSH to %s, waiting for it to start" % host)
  27. i += 1
  28. time.sleep(2)
  29.  
  30. # If we could not connect within time limit
  31. if i == 3:
  32. print ("Could not connect to %s. Giving up") % host
  33. sys.exit(1)
  34.  
  35. # Send the command (non-blocking)
  36. stdin, stdout, stderr = ssh.exec_command("cd /opt/cohda/test; sudo ./runtest_iperf_tx.sh")
  37.  
  38. # Wait for the command to terminate
  39. while not stdout.channel.exit_status_ready():
  40. # Only print data if there is data to read in the channel
  41. if stdout.channel.recv_ready():
  42. rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
  43. if len(rl) > 0:
  44. # Print data from stdout
  45. print (stdout.channel.recv(1024)),
  46. #
  47. # Disconnect from the host
  48. #
  49. print ("Command done, closing SSH connection")
  50. ssh.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement