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?

/H

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:

/api/v0/devices/?type=os&query=xxxx

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'
2

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

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

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

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 }'

Result:

{"hostname":"xxxx","sysname":"xxx.blah","ip":"1.1.1.1"}
{"hostname":"yyyy","sysname":"yyyy.blah","ip":"2.2.2.2"}
{"hostname":"zzzz","sysname":"zzzz.blah","ip":"3.3.3.3"}

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",,"1.1.1.1","","xxxx","authPriv","xxxx","xxxx","xxx","xxx","xxx","v3",161,"udp",,,0,,".1.3.6.1.4.1.14988.1","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:

"device_id","inserted","hostname","sysName","display","ip","overwrite_ip","community","authlevel","authname","authpass","authalgo","cryptopass","cryptoalgo","snmpver","port","transport","timeout","retries","snmp_disable","bgpLocalAs","sysObjectID","sysDescr","sysContact","version","hardware","features","location_id","os","status","status_reason","ignore","disabled","uptime","agent_uptime","last_polled","last_poll_attempted","last_polled_timetaken","last_discovered_timetaken","last_discovered","last_ping","last_ping_timetaken","purpose","type","serial","icon","poller_group","override_sysLocation","notes","port_association_mode","max_depth","disable_notify","dependency_parent_id","dependency_parent_hostname","location","lat","lng"

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.