0

我正在尝试使用带有restconf的python脚本在路由器中激活OSPF路由协议。我将请求发送到此 url https://(routerip)/restconf/data/Cisco-IOS-XE-ospf-native-router:10 但我收到此返回消息:{"errors":{"error":[{"error-message":"uri keypath not found ", "错误标签": "无效值", "错误类型": "应用程序"} ] } }

我尝试了不同的网址,但没有一个是正确的,我不知道什么是错的,网址或正文的内容,有人可以帮助我吗?

这是我的脚本的内容:

import json
import requests

requests.packages.urllib3.disable_warnings()

api_url = "https://(ip)/restconf/data/Cisco-IOS-XE-ospf-native-router:10"
headers = {"Accept": "application/yang-data+json",
           "Content-type": "application/yang-data+json"}
basicauth = ("user", "password")

body = '{"router":{"ospf":[{"id":10, "router-id":"1.1.1.1", "network":[{"ip": "172.16.199.1", "mask": "0.0.0.0", "area": 0},{"ip":"192.168.56.101", "mask": "0.0.0.0", "area":0}]}]}}'

requests.put(api_url, auth=basicauth, headers=headers, data=body, verify=False)

resp = requests.get(api_url, auth=basicauth, headers=headers, verify=False)

response_json = resp.json()

print(json.dumps(response_json, indent=5))
4

2 回答 2

0

带有 RESTCONF 的 OSPF 配置只能修改,不能创建。

ospf_data = '''
{
    "Cisco-IOS-XE-native:router": {
        "Cisco-IOS-XE-ospf:router-ospf": {
            "ospf": {
                "process-id": [
                    {
                        "id": 10
                    }
                ]
            }
        }
    }
}
'''

def modify_ospf(data):
    ospf_api = "/restconf/data/Cisco-IOS-XE-native:native/router"
    ospf_uri = "https://" + host + ospf_api
    headers = {'Content-Type': 'application/yang-data+json','Accept': 'application/yang-data+json'}

    ospf_config_result = requests.request("PUT",
                                         url=ospf_uri, 
                                         auth=(username,password), 
                                         data=data, 
                                         headers=headers, 
                                         verify=False)
    
modify_ospf(ospf_data)

于 2021-02-08T16:50:30.953 回答
0

你的身体似乎不适用于 YANG 格式。

body = '{"router":{"ospf":[{"id":10, "router-id":"1.1.1.1", "network":[{"ip": "172.16.199.1", "mask": "0.0.0.0", "area": 0},{"ip":"192.168.56.101", "mask": "0.0.0.0", "area":0}]}]}}'

您必须遵循 YANG 格式,例如:

$ curl -k -s -u user:password -X GET -H 'Accept: application/yang-data+json' https://172.31.5.1/restconf/data/Cisco-IOS-XE-native:native/Cisco-IOS-XE-native:router/ospf

{
  "Cisco-IOS-XE-ospf:ospf": [
    {
      "id": 10,
      "router-id": "1.1.1.1",
      "network": [
        {
          "ip": "172.16.199.1",
          "mask": "0.0.0.0",
          "area": 0
        },
        {
          "ip": "192.168.56.101",
          "mask": "0.0.0.0",
          "area": 0
        }
      ]
    }
  ]
}
于 2021-02-12T01:08:16.997 回答