Guest User

Untitled

a guest
Dec 13th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. # requires fabric 2.x - run 'pip install fabric' to install it
  2. import logging, socket, paramiko.ssh_exception
  3. from fabric import Connection, Config, SerialGroup, ThreadingGroup, exceptions, runners
  4. from fabric.exceptions import GroupException
  5.  
  6. # Note: You need to supply your own valid servers here to ssh to of course!
  7. def main():
  8. testHosts("All should succeed", "validServer1,validServer2,validServer3")
  9. testHosts("Some should fail", "validServer1,validServer2,BADSERVER1,validServer3,BADSERVER2")
  10.  
  11. def testHosts(message, hostsAsString):
  12. print("")
  13. print(message)
  14.  
  15. # Get list of hosts from somewhere, and convert them to connections
  16. hosts = hostsAsString.split(",")
  17. servers = [Connection(host=host) for host in hosts]
  18.  
  19. # Create a thread group to run requests in parallel
  20. g = ThreadingGroup.from_connections(servers)
  21. try:
  22. command = "df -h / | tail -n1 | awk '{print $5}'"
  23. results = g.run(command, hide=True)
  24. for r in results:
  25. connection = results[r]
  26. print("{}".format(r.host) )
  27. print(" SUCCESS, " + connection.stdout.strip())
  28. except GroupException as e:
  29. # If an exception occurred, at least one request failed.
  30. # Iterate through results here
  31. for c, r in e.result.items():
  32. print("{}".format(c.host) )
  33. if isinstance(r,runners.Result) :
  34. print(" SUCCESS, " + r.stdout.strip())
  35. elif isinstance(r,socket.gaierror) :
  36. print(" FAILED, Network error")
  37. elif isinstance(r,paramiko.ssh_exception.AuthenticationException) :
  38. print(" FAILED, Auth failed")
  39. else:
  40. print(" FAILED, Something other reason")
  41.  
  42. main()
  43.  
  44. $ python test.py
  45.  
  46. All should succeed
  47. validServer1
  48. SUCCESS, 59%
  49. validServer2
  50. SUCCESS, 54%
  51. validServer3
  52. SUCCESS, 53%
  53.  
  54. Some should fail
  55. validServer1
  56. SUCCESS, 59%
  57. validServer2
  58. SUCCESS, 54%
  59. validServer3
  60. SUCCESS, 53%
  61. BADSERVER1
  62. FAILED, Network error
  63. BADSERVER2
  64. FAILED, Network error
Add Comment
Please, Sign In to add comment