Friday, February 26, 2010
7:10 PM

Useful command line little tricks (part 3)

On this third part of these series, I wanted to cover some system and hardware topics. My idea was to provide a bit of information around the interpretation of Linux system logs, system commands, hardware recognition, etc. On this specific blog entry, I'd like to cover the lshw command and how to get information from it.

HAVING FUN WITH LSHW

This command essentially allows for listing hardware, as recognized by your machine. It can be very useful for troubleshooting, in case a system device was not recognized or configured properly. In addition, it is an extremely thorough report, so you may find information about your machine you didn't even know yourself!

It is precisely because of that tendency from lshw to provide such amount of information that you may want to narrow it down in the long run. For the time being, let's just run lshw as is:

lshw

If you run this command under Ubuntu, you will receive a warning, asking you to run it as an administrator. This is a recommendation, you are still allowed to run it using your standard credentials. This is not the case in other distros, so you may be forced to sudo this command, or run it as root.

The output you will obtain is a full list of your hardware, which is hard to check in such command line output format. There are several things you can do to make it easier. For example, you can pipe this output to less, thus allowing for scrolling throughout.

lshw | less

You could also redirect the output from lshw to a file, like this:

lshw > hardware_list.txt

If you just want to take a quick look, but don't want to keep the file created, you can chain commands in one line, like this:

lshw > hardware_list.txt ; gedit hardware_list.txt ; rm hardware_list.txt

This way of sequencing commands is pretty convenient, right!?

FILTERING LSHW OUTPUT

As you have surely realized, lshw output is long and very thorough. In real life, you may only be interested in reading a portion of it. Let's see ways in which we can filter lswh output.

One of the most convenient ways to narrow down the information we get from lshw is taking advantage of its own classes. Here's a list of the classes available:

- Bus
- Memory
- Processor
- Bridge
- Display
- Multimedia
- Network
- Storage
- Communication


Now, to get information for a specific class, run lshw as follows:

lshw -C processor

In this example, I listed information for the "processor" class. Note that you should use lower case syntax for class groups.

As you probably know, we can combine all of the filtering tools we have seen so far in order to create a more complex command that provides more functionality:

lshw -C processor | tee processor_list.txt | grep -i 'Vendor' > vendor.txt ; gedit vendor.txt; rm vendor.txt

In this example, we are creating a file named 'processor_list.txt', which contains all info regarding the processor class. We can do this by using the tee command, which allows us to pipe the output from a command into a file and into the next pipe. Then we filter this output further and obtain the information about our CPU manufacturer, which we redirect to a file. We then display it on gedit, then remove the file after we close the text editor.

As we have seen in recent examples, there are better ways to display the information in the GUI than creating a file, opening it with a visual text editor and then closing and removing the file. The zenity command is perfect for this, here's how it would work with it:

zenity --info --text "Your CPU manufacturer is: $(lshw -C processor | grep vendor | cut -c 16-)"

As you can see from the screenshot below, this is a more elegant way of getting that kind of info. Also note that the string format manipulation I do with the cut command is just a "quick and dirty" solution. A script could easily work around this and customize zenity output according to user input.



Obviously, I am not covering here all of the options available for lshw, but I hope you found it interesting and useful. Linux is all about imagination and creativity. The options are there for you and it is up to you how to get the most out of them.

Good Luck & Enjoy!

0 comments:

Post a Comment