Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the output of ioreg in a more easily parse-able format?

Tags:

macos

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>
    | | {
    | |   ...
like image 442
Abdullah Jibaly Avatar asked Dec 09 '25 13:12

Abdullah Jibaly


2 Answers

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:

  • ioreg.c
  • IOKitLibPrivate.h
  • IOKitKeysPrivate.h

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
like image 59
epatel Avatar answered Dec 11 '25 02:12

epatel


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.

like image 24
Eugen_R Avatar answered Dec 11 '25 02:12

Eugen_R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!