python2.6获取如何从Solaris 11命令逐行输出变量

时间:2014-09-08 21:32:55

标签: python-2.7

我是python的新手。

想知道如何逐行将Solaris11命令的输出转换为变量。 目的是一次提取一行输出。

Solaris 11命令pkg list | pkg info,提供服务器上所有已安装软件包的详细列表。我只对提取名称,描述,版本,大小感兴趣。

到目前为止,我有以下代码:

#! /usr/bin/python

import os
import commands

'''
PCI package compliance

The purpose of this program is to pull package information from
Solaris11 and place that information into a comma separated file that can be read by excel.
the information in question is Name, Description, Version, size
'''

pkg_list = commands.getoutput('pkg list | pkg info')

pkgname = pkg_list[0]

print pkgname

然而,pkgname = pkg_list [0]并没有给我以name开头的整行。 pkgname = pkg_list [10]将只打印N.所以我相信我的问题是弄清楚如何格式化输入,这样我就可以输出每个记录4个字段。

根据要求,以下是“pkg list | pkg info”命令的一些示例输出。

       Name: x11/xmag
       Summary: xmag - magnify parts of the screen
   Description: The xmag program allows you to capture a static image of a
                portion of an X screen, and display it magnified.
      Category: System/X11
         State: Installed
     Publisher: solaris
       Version: 1.0.5
 Build Release: 5.11
        Branch: 0.175.2.0.0.42.1406
Packaging Date: Mon Jun 23 21:58:12 2014
          Size: 115.81 kB
          FMRI: pkg://solaris/x11/xmag@1.0.5,5.11-0.175.2.0.0.42.1406:20140623T215812Z

          Name: x11/xvidtune
       Summary: xvidtune - video mode tuner for Xorg servers
   Description: Xvidtune is a client interface to the X server video mode
                extension (XFree86-VidModeExtension), allowing a user to
                interactively adjust existing video modes.
      Category: System/X11
         State: Installed
     Publisher: solaris
       Version: 1.0.3
 Build Release: 5.11
        Branch: 0.175.2.0.0.42.1406
Packaging Date: Mon Jun 23 21:58:16 2014
          Size: 65.90 kB
          FMRI: pkg://solaris/x11/xvidtune@1.0.3,5.11-0.175.2.0.0.42.1406:20140623T215816Z

更新:以下命令:

print pkg_list.split('\n')[0]+","+pkg_list.split('\n')[1]+','+pkg_list.split('\n')[4]+','+pkg_list.split('\n')[5]

产生输出:

Name: archiver/gnu-tar,       Summary: GNU version of the tar archiving utility,      Category: Development/GNU,         State: Installed

现在我需要遍历这些项目。有什么建议吗?

9月9日,6:41更新

好吧,我快到了。以下代码循环遍历系统上的所有包:

#! /usr/bin/python


import os 
import commands

'''
PCI package compliance

The purpose of this program is to pull package information from
Solaris11 and place that information into a comma separated file that can be read by excel.
the information in question is Name, Description, Version, size
'''




for list in (commands.getoutput('pkg list | awk -F" " \'{print $1}\'').split('\n')):
        #command1 = "pkg info "+list
        pkg_list = commands.getoutput("pkg info %s" %list)
        print pkg_list.split('\n')[0]+","+pkg_list.split('\n')[1]+','+pkg_list.split('\n')[2] #+','+pkg_list.split('\n')[5]

提供以下输出:

Name: compress/pixz,       Summary: parallel indexing version of XZ,   Description: pixz compresses and decompresses files using multiple

然而,我不得不砍下最后一块,即元素[5]。添加代码后,我收到错误:

Traceback (most recent call last):
  File "/root/Documents/python/pci3", line 22, in <module>
    ver = pkg_list.split('\n')[5]
IndexError: list index out of range

有关如何获取最终元素的任何建议?

9月9日晚7点更新

我能够从输出中删除空格。见下文。但是,我仍然需要打印最后一个元素即元素5(#+ pkg_list.split('\ n')[5])

#! /usr/bin/python


import os 
import commands

'''
PCI package compliance

The purpose of this program is to pull package information from
Solaris11 and place that information into a comma separated file that can be read by excel.
the information in question is Name, Description, Version, size
'''




for list in (commands.getoutput('pkg list | awk -F" " \'{print $1}\'').split('\n')):
        #command1 = "pkg info "+list
        pkg_list = commands.getoutput("pkg info %s" %list)
        #print pkg_list.split('\n')[0]+","+pkg_list.split('\n')[1]+','+pkg_list.split('\n')[2] #+','+pkg_list.split('\n')[5]
        output = []
        output = pkg_list.split('\n')[0]+","+pkg_list.split('\n')[1]+','+pkg_list.split('\n')[2]
        print output.replace(" ","")

0 个答案:

没有答案