system_profiler Question

In reference to a MacTech article where I talked about pattern matching with grep, Mike Galke asks:

Let's say I want to find some data *surrounding* the "regular expression". In other words, say I want to see the info that comes up in the whole "Network" section of System Profiler. I know I could do the following:

system_profiler | grep -i network

.... but all that reveals to me is just that one line:

$ system_profiler | grep -i network
Network:
      Current Wireless Network: wireless network not available

.... but what I really want to see is, as I mentioned, the whole section, such as:

Network:

    Built-in Ethernet:

      Type: Ethernet
      Hardware: Ethernet
      BSD Device Name: en0
      Has IP Assigned: Yes
      IPv4 Addresses: 192.168.0.39
      IPv4:
          Addresses: 192.168.0.39
          Configuration Method: Manual
          Router: 192.168.0.1
          Subnet Masks: 255.255.255.0
      IPv6:
          Configuration Method: Automatic
      AppleTalk:
          Configuration Method: Node
      DNS:
          Search Domains: example.com
          Server Addresses: 192.168.0.225
      Proxies:
          Proxy Configuration Method: Manual
          ExcludeSimpleHostnames: 0
          FTP Passive Mode: No
          Auto Discovery Enabled: No
      Ethernet:
          MAC Address: 00:1d:03:59:71:54
          Media Options:
          Media Subtype: none

I'm assuming it's some kind of flag (option) that should go with grep... right?

Answer:

Like many tasks in the shell, there's a hard way and an easy way.  The hard way involves grep.  In fact, this task is roughly impossible with grep alone as system_profiler will output a variable amount of text depending on the machine's configuration.  Typically, though, you'd be looking for grep's "-A", "-B" or "-C" switches.  Hit the man page for more info.

In this case, the easy way means exploiting the tool in question: system_profiler.  system_profiler is an incredibly handy command-line tool.  Typically, it's run all by its lonesome, defaulting to the 'basic' level of detail, which results in a ton of output - basically everything you get when you run the GUI version, but output all at once.  It also has an XML output mode which is really handy - you can even show that output in the GUI app.  Nice!  But you need not run system_profiler without any options.  You can specify which section you'd like to output.

To figure out the names of the sections, we need to run system_profiler -listdatatypes.  That gives us output like the following:

Available Datatypes:
SPHardwareDataType
SPNetworkDataType
SPSoftwareDataType
SPParallelATADataType
SPAudioDataType
SPBluetoothDataType
SPDiagnosticsDataType
SPDiscBurningDataType
SPFibreChannelDataType
SPFireWireDataType
SPDisplaysDataType
SPPCCardDataType
SPPCIDataType
SPParallelSCSIDataType
SPPowerDataType
SPPrintersDataType
SPUSBDataType
SPAirPortDataType
SPFirewallDataType
SPNetworkLocationDataType
SPModemDataType
SPNetworkVolumeDataType
SPApplicationsDataType
SPExtensionsDataType
SPFontsDataType
SPFrameworksDataType
SPPrefPaneDataType

From there, it's a simple task to output the sections we want:

# system_profiler SPPowerDataType SPAudioDataType

In your case, you're after the "SPNetworkDataType":

# system_profiler SPNetworkDataType                             
Network:

    bond0:

      Type: Ethernet
      Hardware: Ethernet
      BSD Device Name: bond0
      Has IP Assigned: Yes
      IPv4 Addresses: 192.168.70.9
      IPv4:
          Addresses: 192.168.70.9
          Configuration Method: Manual
          Interface Name: bond0
          Router: 192.168.70.1
          Subnet Masks: 255.255.255.0
      IPv6:
          Configuration Method: Automatic
      DNS:
          Server Addresses: 127.0.0.1
      Proxies:
          Proxy Configuration Method: Manual
          ExcludeSimpleHostnames: 0
          FTP Passive Mode: Yes
          Auto Discovery Enabled: No
      Ethernet:
          Media Options: Full Duplex, flow-control
          Media Subtype: 1000baseT

While Unix shell tools excel at being able to pump data from one tool to another to extract the exact information you want, sometimes, the original tool itself makes it simple.  The man page for system_profiler has all of the details you need.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Better Answer

Ed,

Thanks for the reply! While it is certainly enlightening (regarding 'system_profiler' in question), I suppose system_profiler was NOT the best example I could have used for the *real* purposes of my question (which was logs). However, as usual, nothing gets wasted, because your reply taught something else of significance. And as it turns out, you DID answer my initial question as well, with your suggesting that I hone in on the "-A, -B, and -C switches. You're right... that's what I was looking for, more-so for LOGS than anything, that I've wanted the answer to this for.

In otherwords, if I have a user who's box is suddenly intermittently crashing, I'd like to check the time-stamp of the crash in "/var/log/system.log", and then perhaps... the 2 or 3 lines that precede that crash's entry.

Thanks again Ed, for determining when it's appropriate to *give* the fish, and when it's time to *teach* to fish : ).

Mike

Happy to Help

Glad that helped broaden your view.