Advertisement
Guest User

Untitled

a guest
Nov 1st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #!/usr/bin/python
  2. import requests
  3. import getpass
  4. import json
  5. from pprint import pprint
  6.  
  7. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  8.  
  9. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  10.  
  11.  
  12. def login(session):
  13. url = "http://" + host + "/rest/login"
  14. # print url
  15. try:
  16. response = session.post(url=url)
  17. # print dir(response)
  18. # print response
  19. # print response.headers
  20. except:
  21. print "Unable to login to CMCNE"
  22. return None
  23. return response.headers['WStoken']
  24.  
  25.  
  26. def get_fabrics(session, host):
  27. url = "https://" + host + "/rest/resourcegroups/All/fcfabrics"
  28. # print url
  29. try:
  30. response = session.get(url=url)
  31. # print response
  32. return json.loads(response.content)
  33. except:
  34. print "Failed to list fabrics"
  35. return None
  36. return response.content
  37.  
  38.  
  39. def get_fcswitches(session, host):
  40. url = "https://" + host + "/rest/resourcegroups/All/fcfabrics/" + fabric_key + "/fcswitches"
  41. print url
  42. try:
  43. response = session.get(url=url)
  44. # print response
  45. return json.loads(response.content)
  46. except:
  47. print "Failed to list switches"
  48. return None
  49. return response.content
  50.  
  51. def get_enddevices(session):
  52. url = 'http://' + host + '/rest/resourcegroups/All/fcfabrics/' + fabric_key + '/enddevices'
  53.  
  54. # Get the response
  55. response = session.get(url)
  56. # Display the response status
  57. print("Status=", response.status)
  58. # If successful (status = 200), display the returned list in JSON format
  59. if response.status == 200:
  60. print("Logged in Devices:")
  61. json_response_bytes = response.read()
  62. json_response_string = str(json_response_bytes, encoding='utf8')
  63. enddevices_dict = json.loads(json_response_string)
  64. return enddevices_dict
  65. else:
  66. print(response.status, response.reason)
  67. return None
  68.  
  69.  
  70. def find_switch(switches, wwn):
  71. for switch in switches:
  72. if switch['key'] == wwn:
  73. return switch
  74.  
  75.  
  76. # 10.188.131.20 for Crit/FlexDC/Backup
  77. # 10.185.55.41 for Prod/Colo12
  78. host = raw_input("Enter CMCNE host IP: ")
  79. print "Enter your credentials"
  80. username = raw_input("Enter your username: ")
  81. password = getpass.getpass("Enter your password: ")
  82. session = requests.Session()
  83. session.verify = False
  84. session.headers.update({"WSUsername": "%s" % username, "WSPassword": "%s" % password,
  85. "Accept": "application/vnd.brocade.networkadvisor+json;version=v1"})
  86. session.auth = (username, password)
  87. token = login(session)
  88. session.headers.update({"WStoken": "%s" % token})
  89.  
  90. content = get_fabrics(session, host)
  91. print json.dumps(content, indent=4)
  92.  
  93. fabric_key = raw_input("Enter the Fabric WWN of principle switch: ")
  94. content = get_fcswitches(session, host)
  95.  
  96. content = get_enddevices(session)
  97. # print("Displaying the list of switches in fabric "+fabric_key+"...")
  98. print json.dumps(content, indent=4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement