gns3api - Simple python module to access the GNS3 API
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.
 

72 lines
2.1 KiB

#!/usr/bin/env python3
#
# get_console - get console port of GNS3 node
#
# Usage: get_console [project] node
#
import sys
import gns3api
def die(*msg_list):
""" abort program with error message """
error_msg = ' '.join(str(x) for x in msg_list)
sys.exit(error_msg.rstrip("\n\r"))
# get command line parameter
if len(sys.argv) == 2:
proj_name = None
node_name = sys.argv[1]
elif len(sys.argv) == 3:
proj_name = sys.argv[1]
node_name = sys.argv[2]
else:
die("Usage: get_console [project] node")
# connect to GNS3 controller
try:
api = gns3api.GNS3Api()
except gns3api.GNS3ApiException as err:
die("Can't connect to GNS3 controller:", err)
# search for the project
project = None
if proj_name is None: # search for the only open project
for proj in api.request('GET', '/v2/projects'):
if proj['status'] == 'opened':
if project is None:
project = proj
else:
die("Multiple projects active, set project as first param")
if project is None:
die("No project is open")
else: # search for proj_name
for proj in api.request('GET', '/v2/projects'):
if proj['name'] == proj_name:
if proj['status'] != 'opened':
die("Project '{}' not open".format(proj_name))
project = proj
break
if project is None:
die("Project '{}' not found".format(proj_name))
# search for node_name
node = None
for node in api.request('GET', ('/v2/projects', project['project_id'], 'nodes')):
if node['name'] == node_name:
break
else:
die("Node '{}' not found in project '{}'".format(node_name, project['name']))
if node['status'] != 'started':
die("Node '{}' is {}".format(node_name, node['status']))
# print console host and port
console_port = node['console']
if console_port is None:
die("Node '{}' doesn't use the console".format(node_name))
console_host = node['console_host']
if console_host in ('0.0.0.0', '::'):
compute = api.request('GET', ('/v2/computes', node['compute_id']))
console_host = compute['host']
print("{} {}".format(console_host, console_port))