過去、以下のメモを書きました。
- CML2 へ Postman から REST API アクセスする
- Python で CML2 へログインし、Baerer Token を取得する
- Python で CML2 上のラボ一覧を取得する
- Python で CML2 上のトポロジーを取得する
上記のコードを流用し、Python からラボのリンク情報を取得するサンプルコードをメモしておきます。 今回は Python 3.9.6 でテストしました。
サンプルコード
import http.client
import json
import ssl
address = "10.0.0.1"
username = "admin"
password = "password"
longest_names = {'Lab': len('Lab'), 'Link': len('Link'), 'Node-A': len('Node-A'), 'Interface-A': len(
'Interface-A'), 'Node-B': len('Node-B'), 'Interface-B': len('Interface-B')}
def main():
results = {}
conn = http.client.HTTPSConnection(
address, context=ssl._create_unverified_context())
token = authenticate(conn, username, password)
labs = get_labs(conn, token)
for lab in labs:
check_longest_name(lab, 'Lab')
results[lab] = []
data = get_topology(conn, token, lab)
for link in data['links']:
link_id = link['id']
interface_a = get_interface(data, link['interface_a'])
interface_b = get_interface(data, link['interface_b'])
check_longest_name(link_id, 'Link')
check_longest_name(interface_a[0], 'Node-A')
check_longest_name(interface_a[1], 'Interface-A')
check_longest_name(interface_b[0], 'Node-B')
check_longest_name(interface_b[1], 'Interface-B')
results[lab].append(
(lab, link_id, interface_a[0], interface_a[1], interface_b[0], interface_b[1]))
for key in results.keys():
template = '{0:' + str(longest_names["Lab"]) + '} ' \
'{1:' + str(longest_names["Link"]) + '} ' \
'{2:' + str(longest_names["Node-A"]) + '} ' \
'{3:' + str(longest_names["Interface-A"]) + '} ' \
'{4:' + str(longest_names["Node-B"]) + '} ' \
'{5:' + str(longest_names["Interface-B"]) + '}'
print(template.format('Lab', 'Link', 'Node-A',
'Interface-A', 'Node-B', 'Interface-B'))
print(template.format('-' * longest_names['Lab'],
'-' * longest_names['Link'],
'-' * longest_names['Node-A'],
'-' * longest_names['Interface-A'],
'-' * longest_names['Node-B'],
'-' * longest_names['Interface-B']))
for result in sorted(results[key]):
print(template.format(*result))
print('')
def authenticate(conn, username, password):
payload = json.dumps({
"username": username,
"password": password
})
headers = {
'Content-Type': 'application/json'
}
conn.request("POST", "/api/v0/authenticate", payload, headers)
res = conn.getresponse()
data = res.read()
return data.decode("utf-8").replace('"', '')
def check_longest_name(item, value):
if len(item) > longest_names[value]:
longest_names[value] = len(item)
def get_interface(data, id):
for interface in data['interfaces']:
if interface['id'] == id:
for node in data['nodes']:
if node['id'] == interface['node']:
return node['data']['label'], interface['data']['label']
def get_labs(conn, token):
payload = ''
headers = {
'Authorization': 'Bearer ' + token
}
conn.request("GET", "/api/v0/labs", payload, headers)
res = conn.getresponse()
data = res.read()
return json.loads(data.decode("utf-8"))
def get_topology(conn, token, lab):
payload = ''
headers = {
'Authorization': 'Bearer ' + token
}
conn.request("GET", "/api/v0/labs/" + lab + "/topology", payload, headers)
res = conn.getresponse()
data = res.read()
return json.loads(data.decode("utf-8"))
if __name__ == "__main__":
main()
実行例
起動していないラボはリンク情報が取得出来ないようです。
# python links.py
Lab Link Node-A Interface-A Node-B Interface-B
------ ---- ------ ------------------ ------ ------------------
Lab Link Node-A Interface-A Node-B Interface-B
------ ---- ------ ------------------ ------ ------------------
abc123 l0 iosv-0 GigabitEthernet0/0 iosv-1 GigabitEthernet0/0
abc123 l1 iosv-1 GigabitEthernet0/1 iosv-2 GigabitEthernet0/0
abc123 l2 iosv-2 GigabitEthernet0/1 iosv-0 GigabitEthernet0/1
コメント