NSO RESTCONF API サンプルスクリプト
下記リンクをもとにNSO経由でのデバイスのコンフィグ取得、コマンド実行、コンフィグ設定、サービス作成を行うサンプルスクリプトを作成してみました。下記を参考にしています。
デバイスコンフィグ表示 (nso_get_device_config.py)
import requests
import json
requests.packages.urllib3.disable_warnings()
HOST = '**.**.**.**'
PORT = 8080
USER = 'admin'
PASS = 'admin'
def main():
url = "http://{h}:{p}/restconf/data/tailf-ncs:devices/device=R3/config?depth=3".format(h=HOST, p=PORT)
headers={'Content-Type':'application/yang-data+json',
'Accept':'application/yang-data+json'}
response = requests.get(url,auth=(USER,PASS),headers=headers,verify=False)
print(response.text)
if __name__ == '__main__':
main()
実行結果
$ python3 nso_get_device_config.py
{
"tailf-ncs:config": {
"tailf-ned-cisco-ios-xr:admin": {
"fpd": {
"auto-upgrade": {
}
},
"disk_status_config": {
"minor": 80,
"severe": 90,
"critical": 95
},
"memory_status_config": {
"minor": 80,
"severe": 90,
--- snip ---
"name": "tailf-ned-cisco-ios-xr-stats",
"revision": "",
"namespace": "http://tail-f.com/ned/cisco-ios-xr-stats",
"conformance-type": "implement"
}
]
}
}
}
デバイスへのコマンド実行 (nso_exec_command.py)
import requests
import json
requests.packages.urllib3.disable_warnings()
HOST = '**.**.**.**'
PORT = 8080
USER = 'admin'
PASS = 'admin'
def main():
url = "http://{h}:{p}/restconf/operations/tailf-ncs:devices/device=R3/live-status/exec/show".format(h=HOST, p=PORT)
headers={'Content-Type':'application/yang-data+json',
'Accept':'application/yang-data+json'}
body={"show":{
"args":"users"
}
}
response = requests.post(url,auth=(USER,PASS),headers=headers,data=json.dumps(body),verify=False)
print(response.text)
if __name__ == '__main__':
main()
実行結果
$ python3 nso_exec_command.py
{
"tailf-ned-cisco-ios-xr-stats:output": {
"result": "\r\n\rTue Jul 27 03:49:43.586 UTC\r\n Line User Service Conns Idle Location\r\n con0/RP0/CPU0 cisco hardware 0 6d15h \r\n* vty0 cisco telnet 0 00:00:07 192.168.1.1\r\nRP/0/RP0/CPU0:R3#"
}
}
デバイスの設定 (nso_add_loopback.py)
import requests
import json
requests.packages.urllib3.disable_warnings()
HOST = '**.**.**.**'
PORT = 8080
USER = 'admin'
PASS = 'admin'
def main():
url = "http://{h}:{p}/restconf/data/tailf-ncs:devices/device=R3/config/tailf-ned-cisco-ios-xr:interface/Loopback=10".format(h=HOST, p=PORT)
headers={'Content-Type':'application/yang-data+json',
'Accept':'application/yang-data+json'}
body={ "Loopback": [
{
"id": 10,
"ipv4": {
"address": {
"ip": "1.1.1.1",
"mask": "255.255.255.255"
}
}
}
]
}
response = requests.put(url,auth=(USER,PASS),headers=headers,data=json.dumps(body),verify=False)
print(response.text)
if __name__ == '__main__':
main()
実行結果
RP/0/RP0/CPU0:R3#show run int loopback 10
Tue Jul 27 14:22:13.969 UTC
interface Loopback10
ipv4 address 1.1.1.1 255.255.255.255
!
RP/0/RP0/CPU0:R3#
サービスの設定 (nso_add_service.py)
import requests
import json
requests.packages.urllib3.disable_warnings()
HOST = '**.**.**.**'
PORT = 8080
USER = 'admin'
PASS = 'admin'
def main():
url = "http://{h}:{p}/restconf/data/".format(h=HOST, p=PORT)
headers={'Content-Type':'application/yang-data+json',
'Accept':'application/yang-data+json'}
body={
"add_loopback": {
"name": "test",
"dev": "R3",
"id": "10",
"addr": "1.1.1.1",
"mask": "255.255.255.255"
}
}
response = requests.post(url,auth=(USER,PASS),headers=headers,data=json.dumps(body),verify=False)
print(response.text)
if __name__ == '__main__':
main()
実行結果
RP/0/RP0/CPU0:R3#show run int lo 10
Tue Jul 27 15:37:09.793 UTC
interface Loopback10
ipv4 address 1.1.1.1 255.255.255.255
!
RP/0/RP0/CPU0:R3#
ネットワーク自動化を基礎から体系的に学びたい方は下記の本がおすすめです。
リンク
コメント