wbemcli: key/value pair output

610 views Asked by At

If I use wbemcli to enumerate all the instances I get something similar to this:

wbemcli -nl -t -noverify ei 'https://aaa/aaa:aaa' 
https://aaa/aaa:aaa.Version="",Vendor="",Name=""
-Version#=""
-Vendor#=""
-Name#=""
-Description=""

How can I call wbemcli to get only one item (i.e. the Name)? and not everything.

The -t option says:

-t
Append array ([]), reference (&) and key property (#) indicators to property names

but I wasn't able to utilize this in my favor.

Is there a way to retrieve this information in a key/value pair format?
Or maybe pipe the output into an array or something from which I can grab only what I need?

When I drop the output into an array all the data is stored in the first element ${a[0]}.

EDIT Here's an output example:

$ wbemcli -nl -t -noverify ei 'https://user:[email protected]:0000/root/aaa:AA_AaaAaaaAaaaa'
000.000.000.000:0000/root/aaa:AA_AaaAaaaAaaaa.ClassName="AA_AaaAaaaAaaaa",Name="123456a7ff890123"
-ClassName#="AA_AaaAaaaAaaaa"
-Name#="123456a7ff890123"
-Caption="aa aaa"
-Description="aa aa"
-ElementName="aa aaa aaaa"
-OperationalStatus[]=2
-HealthState=5
-CommunicationStatus=2
-DetailedStatus=1
-OperatingStatus=0
-PrimaryStatus=1
-EnabledState=5
-RequestedState=12
-EnabledDefault=2
-TransitioningToState=12
-PrimaryOwnerName="Uninitialized Contact"
-PrimaryOwnerContact="Uninitialized Contact"

The output is usually in this format.
If the query returns multiple objects they will be grouped and all will have the same members with their appropriate values.

1

There are 1 answers

0
tripleee On

http://linux.dell.com/files/whitepapers/WBEM_based_management_in_Linux.pdf has a number of examples which simply suggest to use grep to obtain the specific key and value you are looking for. There does not seem to be a way to directly query for a specific key within a result set.

Expanding on the comment by Etan Reisner, you could use something like

wbemcli <<query>> | grep -oP "^-$key=\K.*"

to obtain the value for the key named in $key, provided you have GNU grep which provides the -P option for Perl-compatible regular expressions (here, the \K "forget up through here" operator is useful). So for your specific example,

wbemcli -nl -t -noverify ei 'https://aaa/aaa:aaa' |
grep -oP '^-Name#=\K.*'

There is also a -dx option which produces XML output, which may be more robust if you are planning to write a major application on top of this protocol (but then perhaps you should be looking at a dedicated WBEM library such as the C or Java libraries listed in their wiki). It would not seem to be implausible to write a simple (e.g.) Python client to retrieve (part of?) the result tree and let you query or manipulate it locally, either.