Operating System - HP-UX
1833151 Members
3250 Online
110051 Solutions
New Discussion

Extracting useful info from swlist

 
SOLVED
Go to solution
Simon Hargrave
Honored Contributor

Extracting useful info from swlist

I'm writing some script to execute an swlist against all HP servers in our organisation (about 60, ranging from 10.20 -> 11.23), so I can populate our asset register database automatically with installed software details.

I'm basically using...

/usr/sbin/swlist -a os_release -a machine_type -a title @$HOST

...to get information off each host in a for loop. The problem is, I need to extract each field into a variable for processing by the shell. Problem is the output from swlist is whitespace delimited (both spaces and tabs in various formats), and sometimes fields (eg os_release) can be blank.

So, I can't use awk to split based on white-spaces, because some fields shift left if any other fields are blank. I can't use a position-based cut because they appear to be differently spaced on different OS releases.

Question is, what can I do? I can't see that there's an option in swlist to eg give a comma-delimited output, which would be ideal. I can't really think of a way to use awk to figure it out, because either of os_release and machine_type can and sometimes are both blank!

Ideas?

Cheers, Sy
4 REPLIES 4
Mark Grant
Honored Contributor

Re: Extracting useful info from swlist

personally, I'd use perl and the \s to split

something like

field1=`/usr/sbin/swlist -a os_release | perl -ne '@a=split /\s+/;print "$a[1]\n"'`

This will give you field 1 (change the 1 to a 2 for the second field).

@a should be an array that contains all the fields as different elements and you can print any one you like.
Never preceed any demonstration with anything more predictive than "watch this"
Simon Hargrave
Honored Contributor

Re: Extracting useful info from swlist

Problem with this is, if you take the following subset of output from swlist: -

100BaseT-01 ?.11.11 * HP-PB 100BaseT;Supptd HW=A3495A;SW=J2759BA
A3495A ?.10.* 9000/[68]?? HP-PB 100BT LAN/9000
A5230A ?.11.00 * 100BT/9000 PCI

FibrChanl-01 ?.11.11 *:*64 PCI-X FibreChannel;Supptd HW=A6826A,A9782A,A9784A

Firmware_Check Checks system firmware
ITOAgent ?.11.* 9000/[78]??:* VPO Agents
Ignite-UX-11-11 HP-UX Installation Utilities for Installing 11.11 Systems
LSOF HPUX 11i LSOF


Probably not easy to see from this, but eg LSOD and Ignite-UX-11-11 have no os_release or machine_types set, so if splitting on white spaces, field 2 would become "HPUX" (from the start of the name field), instead of B.11.11 eg.
Dietmar Konermann
Honored Contributor
Solution

Re: Extracting useful info from swlist

What about using swlist's -v option? It prints each attribute on its own line...

Idea:

/usr/sbin/swlist -v -a os_release -a machine_type -a title |
awk '
/^os_release/ {os_release=$2};
/^machine_type/ {machine_type=$2};
/^title/ {sub(/^title */,""); title=$0};
{
if (title != "") {
printf ("%s|%s|%s\n", os_release, machine_type, title);
title="";
}
}'

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Simon Hargrave
Honored Contributor

Re: Extracting useful info from swlist

Cracking idea, cheers! Needed a little tweaking to work fully (it would display "vendor"'s as software items in the output. The below modified works a charm!

/usr/sbin/swlist -v -a tag -a os_release -a machine_type -a title | awk '
/^bundle/ || /^product/ {interesting=1 ; title=""};
/^tag/ {tag=$2};
/^os_release/ {os_release=$2};
/^machine_type/ {machine_type=$2};
/^title/ {sub(/^title */,""); title=$0};
{if(title != "" && interesting == 1) {
printf("%s,%s,%s,%s\n",tag,os_release,machine_type,title);
interesting=0
}
}'



Cheers