[Python] I am trying to fetch ip address and hostnames from netbox using python and pushing...

Discussão em 'Python' iniciado por Stack, Janeiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am rying this code to and I am getting error 400

    import pynetbox
    import requests
    import json
    from gettokenremote import *

    #creating netbox url
    nb= pynetbox.api(
    'xx.xx.xx.xx',
    token="*****"
    )

    #fetching devices available on netbox
    hostnamelist = list(nb.dcim.devices.all())
    #print(hostnamelist)

    ##fetching ip adress from netbox




    # Fetch all IP addresses
    ip_addresses = nb.ipam.ip_addresses.all()

    # Extract IP addresses and store them in a list
    ip_list = [ip.address for ip in ip_addresses]

    # Print the list of IP addresses
    #print(ip_list)


    ## end of fetching ip address

    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()
    }

    #create json from 2 python list

    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)


    #iterating python list from
    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)
    #creating json object from python list
    host_json = json.dumps(new_json)

    #print(host_json)
    #adding netbox device hostname to dnac using rest api
    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")
    '''




    I am getting error 400. I checked with api documentation But could not find any clue to solve this. I am trying to get ip details from netbox using python, and i want to fetch these in dnac using python script. Please help

    I checked api and changing json data structure but these did not help.sorry for messy code

    Continue reading...

Compartilhe esta Página