List all devices with specific os type

I have issues to make an API query and sort in diffrent kind of OS. My thought was to add something like this " /api/v0/devices/?os=junos" but as I read I see it’s not supported, so is it any other way to get my devices via API and sort them out by OS type?


Not quite what you asked for but this will do it

It utilises the api to produce its output

Looks like the doco is a bit out of whack - looking at the code:

… you’d do it as:


Examples from mine:

~$ export LNMSAPI='https://x.x.x.x/api/v0/devices'

~$ curl -s --insecure -H "X-Auth-Token: $LNMSKEY_RO" "${LNMSAPI}?type=os&query=APC" | jq '.count'

~$ curl -s --insecure -H "X-Auth-Token: $LNMSKEY_RO" "${LNMSAPI}?type=os&query=Moxa-AWK" | jq '.count'

~$ curl -s --insecure -H "X-Auth-Token: $LNMSKEY_RO" "${LNMSAPI}?type=os&query=Linux" | jq '.count'

~$ curl -s --insecure -H "X-Auth-Token: $LNMSKEY_RO" "${LNMSAPI}" | jq '.count'

Just tried your solution and that works great, now I can rewrite it with ‘jq’ and push it to my ansible inventory direct which saves a lot of time.

This has triggered the regular trauma and cold sweats I endure trying to remember what random combination of brackets, dots, and pipes got my jq query working … so to help future me, or anyone else …

You can get the entire device list and jq parse it for os like this - note the strings will all be lowercase compared to directly using the API as above:

jq '.devices[] | select( .os == "ping")'

You can output filtered json of what you want specifically after that, for example:

jq --compact-output '.devices[] | select( .os == "something") | {hostname: .hostname, sysname: .sysName, ip: .ip }'



Or to dump it all to CSV (there are multiple indecipherable methods to do this):

jq -r '.devices[] | to_entries|map(.value) | @csv'

Example result:

34,"2021-01-13 14:29:29","xxxx-SW01","xxxx-sw01",,"","","xxxx","authPriv","xxxx","xxxx","xxx","xxx","xxx","v3",161,"udp",,,0,,".","RouterOS CRS112-8G-4S","xxxx","xxxx","CRS112-8G-4S","Level 5",58,"routeros",1,"",0,0,13474284,0,"2023-01-13 10:22:51",,12.29,38.59,"2023-01-13 08:52:29","2021-10-17 00:12:16",18.1,"","network","xxxx","mikrotik.svg",0,1,,1,2,0,"42","xxxx","xxxx",-xx.xxx006,xx.xxx295

For header rows: (keys_unsorted, [.[]]) don’t have the way to make that output once inline right now - will update later when I get it:


Yes I acctually run Rundeck as ansible orcestrator and to get hte json file tight I you this jq command, that way I can push it straing into my library which is nice.

jq ‘.devices[]|{“nodename”:.hostname,“hostname”:.hostname,“osVersion”:.version,“osFamily”:.features,“osArch”:.hardware,“description”:.sysDescr,“osName”:.os,“username”:“XXX”,“ssh-authentication”:“password”,“ssh-password-option”:“option.password”}’ | jq -s | jq ‘map({(.hostname|tostring): .}) | add’

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.