I need to get the output of ioreg in Mac OS X for further processing, however the tree like structure it uses is difficult to parse? Is there any way to get the output in XML or something easier to parse (like system_profiler)?
+-o Root <class IORegistryEntry, retain 12>
| {
| ...
| }
|
+-o MacBookPro5,1 <class IOPlatformExpertDevice, registered, matched, active, busy 0, retain 25>
| {
| ...
| }
|
+-o AppleACPIPlatformExpert <class AppleACPIPlatformExpert, registered, matched, active, busy 0, retain 51>
| | {
| | ...
A long road can be to grab the source code to ioreg and modify it...
https://opensource.apple.com/source/IOKitTools/IOKitTools-108/ioreg.tproj/
Download:
Change the #include <IOKit/IOKitLibPrivate.h> and #include <IOKit/IOKitKeysPrivate.h > to #include "IOKitLibPrivate.h" and #include "IOKitKeysPrivate.h" so you can compile when both files is in same directory, then compile with
cc -o ioreg ioreg.c -framework Foundation -framework IOKit -lcurses
According to the ioreg man page one can use the -a flag to generate output in XML.
If you're using python, you can simply create a plist element tree with:
import plistlib
import subprocess
output = subprocess.check_output(["ioreg", "-alw0", "-p", "IOUSB"])
pl = plistlib.loads(output, fmt=plistlib.FMT_XML)
After that you can filter it or manipulate it in the desired way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With