Collection of GNS3 GUI Tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.3 KiB

  1. #!/usr/local/bin/python3
  2. """
  3. link_resume.py - resume links of all/selected nodes
  4. """
  5. import os
  6. import sys
  7. import gns3api
  8. from qt_widgets import SimpleWidgets
  9. def die(text):
  10. """ terminate program with error message """
  11. SimpleWidgets().alert(None, text)
  12. sys.exit(text)
  13. def link_resume(argv):
  14. """ parse command line, retrieve nodes and resume connected links """
  15. # get arguments
  16. if len(argv) < 4:
  17. die("usage:\nlink_resume version parameter-file project-id [sel-item ...]")
  18. try:
  19. with open(argv[2], "r") as file:
  20. cntl_url, cntl_user, cntl_passwd, *_ = file.read(512).splitlines()
  21. if argv[2].endswith(".tmp"):
  22. os.remove(argv[2])
  23. except (OSError, ValueError) as err:
  24. sys.exit("Can't get controller connection params: {}".format(err))
  25. project_id = argv[3]
  26. sel_items = argv[4:]
  27. # connect to GNS3 controller
  28. try:
  29. api = gns3api.GNS3Api(cntl_url, cntl_user, cntl_passwd)
  30. except gns3api.GNS3ApiException as err:
  31. die("Can't connect to GNS3 controller: {}".format(err))
  32. try:
  33. if not sel_items:
  34. # resume all links
  35. for link in api.request('GET', ('/v2/projects', project_id, 'links')):
  36. if link['suspend']:
  37. api.request('PUT', ('/v2/projects', project_id, 'links', link['link_id']),
  38. {'suspend': False})
  39. else:
  40. # resume links of selected nodes
  41. sel_nodes = frozenset(item[6:] for item in sel_items
  42. if item.startswith("nodes/"))
  43. if not sel_nodes:
  44. die("No node selected")
  45. for link in api.request('GET', ('/v2/projects', project_id, 'links')):
  46. if not link['suspend']:
  47. continue
  48. for node in link['nodes']:
  49. if node['node_id'] in sel_nodes:
  50. api.request('PUT', ('/v2/projects', project_id, 'links', link['link_id']),
  51. {'suspend': False})
  52. break
  53. except gns3api.GNS3ApiException as err:
  54. die("Can't get/set link information: {}".format(err))
  55. try:
  56. link_resume(sys.argv)
  57. except KeyboardInterrupt:
  58. sys.stderr.write("Aborted\n")