Is it possible to export list of devices in csv or similar

Hey guys,

Short question:
Is there an easy way to export a list of devices including ip,hostname,os as a csv or list from librenms?

I’m not sure about the webui, but you could always do select ip,hostname,os from devices or use the API.

I went the api->powershell way, thanks for your suggestions

1 Like

Nice, you should post your command here for others :slight_smile:

well a very short example is this:

        # define the url/adress of your librenms server ( leave /api/v0/ intact)
        $baseurl = "http://librenms.lol/api/v0/"

        #fill in api token which you can retrieve from  web interface
        $token = "your api token"

    # generating header(do not touch)
        $header = @{
        "X-Auth-Token" = $token}

    #defining url to get devices
        $gdevices = $baseurl + "devices?type=network"

    #invoking api request and showing results in grid view
        Invoke-RestMethod -uri $gdevices -Headers $header |select  -ExpandProperty devices -ExcludeProperty sysDescr |ogv

As Aauvinet found out below: the invoke-restmethod command is not available in powershell 2 or below. please installl windows Management framework 5 (which includes a newer version of powershell) or above to work around this .

3 Likes

@telskamp,

I try you scrip in poweshell… but i don’t work to me

I do :
I open powershell
I put in cli

$baseurl = "http://librenms.my/api/v0/"

$token = “removed”
$header = @{
“X-Auth-Token” = $token}
$gdevices = $baseurl + "devices?type=network"
Invoke-RestMethod -uri $gdevices -Headers $header |select -ExpandProperty devices -ExcludeProperty sysDescr |ogv

But Invoke-RestMethod isn’t know by powershell

did you change the url to reflect your url?

Invoke-RestMethod has been added in powershell v3.0 you are probably trying to execute the command on a windows 7 or server 2008 machine. please try on newer windows version or install windows management framework 5 or later

https://www.microsoft.com/en-us/download/details.aspx?id=54616

if you want to see the powershell version of your machine please type $psversiontable in your powershell window

Hello,
Unburying this thread, I try to obtain a list of all my network devices.
So far, it seems that ?type=network does not work anymore.

My request is:

curl -H 'X-Auth-Token: MyFavoriteAPIToken' 'http://localhost/api/v0/devices/?type=network'

This returns a list of all equipments, firewalls, servers etc, not limited to network.
Having read the documentation at https://docs.librenms.org/API/Devices/#get_device, I can’t find the answer.

So far, I have mostly HP switches so I workaround the issue by doing :

curl -H 'X-Auth-Token: MyFavoriteAPIToken' 'http://localhost/api/v0/devices/?type=os&query=procurve'

Is there a ‘good’ way to get only network devices from an API query ?

Best regards.

I created a Python-script to recursively crawl all network devices for relevant information. In my case I specifically wanted all serial numbers of all devices (and parts) in LibreNMS.

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 22 13:41:25 2020
Crawl libreNMS inventory to receive list of all devices with serial number.
"""

import csv
import requests

#edit base url below
base_url = 'http://librenms.me/api/v0/'
#edit auth-token below
auth_token = 'INSERT Auth-Token here'
header = { 'x-auth-token' : auth_token }
file_path = 'LibreNMSInventory.csv'

#General GET helper function, pass sub-url to append to base URL for request
def GET(url):
    return requests.get('{0}{1}'.format(base_url,url), headers = header)
                        
#GET call to get all devices registered in libreNMS
#Return dictionary of all deviceIDs + hostname
def getDeviceDict():
    
    device_list = GET('devices/').json()['devices']
    device_dict = {}
    for device in device_list:
        device_dict[device['device_id']] = device['hostname']
    
    
    return device_dict


def inventoryList(deviceID, hostname, physical_id = None):
    if physical_id == None:
        postString =""
    else:
        postString="?entPhysicalContainedIn={}".format(physical_id)
        
    inventory = GET('inventory/{0}{1}'.format(deviceID,postString)).json()['inventory']
    
    for device in inventory:

        entPhysicalClass = device['entPhysicalClass']
        entPhysicalName = device['entPhysicalName']
        entPhysicalDescr = device['entPhysicalDescr']
        entPhysicalModelName = device['entPhysicalModelName']
        entPhysicalSerialNum = device['entPhysicalSerialNum']
        if device['entPhysicalSerialNum'] != '':
            csv_row = [hostname, entPhysicalClass, entPhysicalName, entPhysicalDescr, entPhysicalModelName, entPhysicalSerialNum]
            print(csv_row)
            with open(file_path,'a', newline='') as file:
                writer = csv.writer(file)
                writer.writerow(csv_row)
#            print('{0},{1},{2},{3},{4},{5}'.format(hostname,entPhysicalClass,entPhysicalName,entPhysicalDescr,entPhysicalModelName,entPhysicalSerialNum))
            
        inventoryList(deviceID,hostname,device['entPhysicalIndex'])
    
    
    
def main():
    
    device_dict = getDeviceDict()
    with open(file_path,'w+', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(['Hostname','Class','Name','Description','Model Name','Serial'])
        
    for deviceID, hostname in device_dict.items():
        inventoryList(deviceID,hostname)

main()
5 Likes