cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
907
Views
10
Helpful
1
Replies

REST query of device names using address

richsun
Cisco Employee
Cisco Employee

Is there a way to get the device "name" using "address"? 

GET /api/running/devices/device?select=name;address

gives

{ "collection": { "tailf-ncs:device": [
   {"name": "DEVICE1","address": "1.1.1.1" },
   {"name": "DEVICE2","address": "2.2.2.2" }
] }}

Looking something like

GET /api/running/devices/device?select=name;address=2.2.2.2

or

GET /api/running/devices/device[address=2.2.2.2]

But no luck. Tried also RESTCONF using "fields=name;address", again, no way to specify a value for address.

1 Accepted Solution

Accepted Solutions

richsun
Cisco Employee
Cisco Employee

Thanks to an NSO expert, here is a great answer, in case anyone else needs the same:

curl -i 'http://admin:admin@localhost:8080/api/query'  -X POST -T q.json  -H "Content-Type: application/vnd.yang.data+json"

where file q.json is

{
    "immediate-query": {
        "foreach": "/devices/device[address = '2.2.2.2']",
        "select": [
            {
                "expression": "name",
                "result-type": ["string"]
            },
            {
                "expression": "address",
                "result-type": ["string"]
            }
        ],
        "sort-by": ["name"],
        "limit": 100,
        "offset": 1
    }
}

View solution in original post

1 Reply 1

richsun
Cisco Employee
Cisco Employee

Thanks to an NSO expert, here is a great answer, in case anyone else needs the same:

curl -i 'http://admin:admin@localhost:8080/api/query'  -X POST -T q.json  -H "Content-Type: application/vnd.yang.data+json"

where file q.json is

{
    "immediate-query": {
        "foreach": "/devices/device[address = '2.2.2.2']",
        "select": [
            {
                "expression": "name",
                "result-type": ["string"]
            },
            {
                "expression": "address",
                "result-type": ["string"]
            }
        ],
        "sort-by": ["name"],
        "limit": 100,
        "offset": 1
    }
}