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.

102 lines
3.2KB

  1. #!/usr/local/bin/python3
  2. """
  3. console_port.py - change console port of one or more nodes
  4. """
  5. import os
  6. import sys
  7. import gns3api
  8. from qt_widgets import SimpleWidgets
  9. widget = SimpleWidgets()
  10. def die(text):
  11. """ terminate program with error message """
  12. widget.alert(None, text)
  13. sys.exit(text)
  14. def set_console(argv):
  15. """ parse command line, retrieve nodes and set console port """
  16. # get arguments
  17. if len(argv) < 4:
  18. die("usage:\nconsole_port version parameter-file project-id [sel-item ...]")
  19. try:
  20. with open(argv[2], "r") as file:
  21. cntl_url, cntl_user, cntl_passwd, *_ = file.read(512).splitlines()
  22. if argv[2].endswith(".tmp"):
  23. os.remove(argv[2])
  24. except (OSError, ValueError) as err:
  25. sys.exit("Can't get controller connection params: {}".format(err))
  26. project_id = argv[3]
  27. sel_items = argv[4:]
  28. # connect to GNS3 controller
  29. try:
  30. api = gns3api.GNS3Api(cntl_url, cntl_user, cntl_passwd)
  31. except gns3api.GNS3ApiException as err:
  32. die("Can't connect to GNS3 controller: {}".format(err))
  33. # get node information
  34. nodes = {}
  35. try:
  36. for node in api.request('GET', ('/v2/projects', project_id, 'nodes')):
  37. nodes[node['node_id']] = node
  38. except gns3api.GNS3ApiException as err:
  39. die("Can't get node information: {}".format(err))
  40. if not nodes:
  41. die("No node in project")
  42. # get selected nodes
  43. if not sel_items:
  44. msg_type = "in project"
  45. sel_nodes = list(nodes.keys())
  46. else:
  47. msg_type = "selected"
  48. sel_nodes = [item[6:] for item in sel_items
  49. if item.startswith("nodes/")]
  50. if not sel_nodes:
  51. die("No node selected")
  52. sel_nodes = [node_id for node_id in sel_nodes
  53. if nodes[node_id]['console']]
  54. if not sel_nodes:
  55. die("No node with console port " + msg_type)
  56. sel_nodes.sort(key=lambda k: nodes[k]['name'].lower())
  57. # New console port
  58. min_port = 5000
  59. max_port = 10000
  60. console_port = widget.get_int(None, "Console port", min_port,
  61. min_port, max_port)
  62. if console_port is None:
  63. return
  64. # update console port of selected nodes
  65. for node_id in sel_nodes:
  66. if nodes[node_id]['node_type'] == 'ethernet_switch':
  67. continue
  68. try:
  69. node = api.request('PUT',
  70. ('/v2/projects', project_id, "nodes", node_id),
  71. {"console": console_port})
  72. except gns3api.GNS3ApiException as err:
  73. if isinstance(err, gns3api.HTTPError) and err.args[0] == 409:
  74. widget.info(None, "{}: {}"
  75. .format(nodes[node_id]['name'], err.args[1]))
  76. else:
  77. die("Can't update node information: {}".format(err))
  78. else:
  79. if node['console'] != console_port:
  80. widget.info(None, "{}: Can't update console port, using {}"
  81. .format(nodes[node_id]['name'], node['console']))
  82. console_port += 1
  83. if console_port > max_port:
  84. console_port = min_port
  85. try:
  86. set_console(sys.argv)
  87. except KeyboardInterrupt:
  88. sys.stderr.write("Aborted\n")