You are browsing a read-only backup copy of Wikitech. The primary site can be found at wikitech.wikimedia.org

User:Nskaggs: Difference between revisions

From Wikitech-static
Jump to navigation Jump to search
imported>Nskaggs
(Add wmcs-netbox-list.py)
 
imported>Nskaggs
No edit summary
Line 1: Line 1:
Dropping an enhance fork of [[User:Arturo Borrero Gonzalez#wmcs-netbox-list.py|Arturo's netbox-list.py]]
An enhanced fork of [[User:Arturo Borrero Gonzalez#wmcs-netbox-list.py|Arturo's netbox-list.py]] with more fields gathered.


===wmcs-netbox-list.py===
===wmcs-netbox-list.py===


I use this script to fetch & list WMCS server info from [[Netbox]] and generate a CSV (to later import into a spreadsheet):
I use this script to fetch & list WMCS server info from [[Netbox]] and generate a CSV (to later import into a spreadsheet):


{{Collapse top|wmcs-netbox-list.py}}
{{Collapse top|wmcs-netbox-list.py}}
Line 78: Line 79:
     request_headers = {"Authorization": "Token {}".format(config.api_token)}
     request_headers = {"Authorization": "Token {}".format(config.api_token)}
     ip_result = requests.get(url=request_url, headers=request_headers)
     ip_result = requests.get(url=request_url, headers=request_headers)
 
   
    if not ip_result.json()["assigned_object_id"]:
        return "None"
     interface = ip_result.json()["assigned_object_id"]
     interface = ip_result.json()["assigned_object_id"]
     request_url = "{}/api/dcim/interfaces/{}".format(config.netbox_url, interface)
     request_url = "{}/api/dcim/interfaces/{}".format(config.netbox_url, interface)
     request_headers = {"Authorization": "Token {}".format(config.api_token)}
     request_headers = {"Authorization": "Token {}".format(config.api_token)}
     interface_result = requests.get(url=request_url, headers=request_headers)
     interface_result = requests.get(url=request_url, headers=request_headers)
 
   
    if not interface_result.json()["connected_endpoint"]:
        return "None"
     endpoint = interface_result.json()["connected_endpoint"]["id"]
     endpoint = interface_result.json()["connected_endpoint"]["id"]
     request_url = "{}/api/dcim/interfaces/{}".format(config.netbox_url, endpoint)
     request_url = "{}/api/dcim/interfaces/{}".format(config.netbox_url, endpoint)
Line 89: Line 94:
     endpoint_result = requests.get(url=request_url, headers=request_headers)
     endpoint_result = requests.get(url=request_url, headers=request_headers)


     if not endpoint_result.json()["untagged_vlan"]:
     if endpoint_result.json()["untagged_vlan"]:
        return endpoint_result.json()["untagged_vlan"]["name"]
    else:
         return "None"
         return "None"
    else:
        return endpoint_result.json()["untagged_vlan"]["name"]




Line 121: Line 126:
if __name__ == "__main__":
if __name__ == "__main__":
     main()
     main()


</syntaxhighlight>
</syntaxhighlight>
{{Collapse bottom}}
{{Collapse bottom}}

Revision as of 18:13, 10 January 2022

An enhanced fork of Arturo's netbox-list.py with more fields gathered.

wmcs-netbox-list.py

I use this script to fetch & list WMCS server info from Netbox and generate a CSV (to later import into a spreadsheet):