You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从NetBox拉取设备信息推送至Cisco DNA Center时遇400错误的问题求助

从NetBox拉取设备信息推送至Cisco DNA Center时遇400错误的问题求助

我现在在运行一段Python脚本,想要从NetBox拉取设备的主机名和IP地址,再把这些信息推送到Cisco DNA Center,但一直收到400错误(提示客户端请求无法被服务器理解)。我对照过DNA Center的API文档,也调整过要发送的JSON数据结构,但都没解决问题,代码写得有点乱,麻烦帮忙排查下问题出在哪?

我的代码如下:

import pynetbox 
import requests
import json
from gettokenremote import *

# 创建NetBox连接
nb= pynetbox.api(
    'xx.xx.xx.xx',
    token="*****"
)

# 从NetBox获取设备列表
hostnamelist = list(nb.dcim.devices.all())
# print(hostnamelist)

# 从NetBox获取IP地址列表
ip_addresses = nb.ipam.ip_addresses.all()
ip_list = [ip.address for ip in ip_addresses]
# print(ip_list)
# 结束IP地址拉取

# DNA Center配置
dnac_url = "xx.xx.xx.xx"
USERNAME = "*********"                           
PASSWORD = "*********"
user_pass = (USERNAME, PASSWORD)

deviceurl = dnac_url + "/dna/intent/api/v1/network-device"

device_headers = {
        'Content-Type': 'application/json',
        'X-Auth-Token': getToken()
    }

# 将两个列表转成JSON格式
new_json = [{'hostname': str(host), 'ipaddress': str(ip)} for host, ip in zip(hostnamelist, ip_list)]
final_json = {"devices": new_json}
host_json = json.dumps(final_json)

# 另一种方式整理数据(暂时没用到)
datatobeadded = []
for hostname,ip in zip(hostnamelist, ip_list):
    new_device = str(hostname)
    new_device2 = str(ip)
    datatobeadded.append(new_device)
    datatobeadded.append(ip)

# print(hostnametobeadded)

print(new_json)
# 再次转成JSON(这里重复转了?)
host_json = json.dumps(new_json)

# print(host_json)
# 发送请求到DNA Center
response = requests.post(deviceurl, headers=device_headers, data=host_json, verify=False, debug=True)

# 处理响应状态码
if response.status_code == 201:
    print("Device succcessfully added to the dnac")
elif(response.status_code == 202):
    print("request is accepted but device is not added")
elif(response.status_code == 204):
    print("request was succefull but not content was returned")
elif(response.status_code == 206):
    print("The GET request included a Range Header, and the server responded with the partial content matching the range")
elif(response.status_code == 400):
    print("The client made a request that the server could not understand")
elif(response.status_code == 401):
    print("The client's authentication credentials included with the request are missing or invalid")
elif(response.status_code == 403):
    print("The server recognizes the authentication credentials, but the client is not authorized to perform this request")
elif(response.status_code == 404):
    print("The client made a request for a resource that does not exist")
elif(response.status_code == 409):
    print("The target resource is in a conflicted state (for example, an edit conflict where a resource is being edited by multiple users). Retrying the request later might succeed.")
elif(response.status_code == 415):
    print("The client sent a request body in a format that the server does not support")
elif(response.status_code == 500):
    print("The server could not fulfill the request")
elif(response.status_code == 501):
    print("The server has not implemented the functionality required to fulfill the request")
elif(response.status_code == 503):
    print("The server is (temporarily) unavailable")
elif(response.status_code == 504):
    print("The server did not respond inside time restrictions and timed-out")
else:
    print("Adding device is not fetched succefully please check request structure again")

我已经做过的尝试:

  • 核对过DNA Center的API文档,确认端点和请求格式
  • 调整过要发送的JSON数据结构,但还是收到400错误

备注:内容来源于stack exchange,提问作者umesh upadhyay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 13:49:38