badfsaadkl
Goto Top

Python - ausgabe formatieren

Hallo zusammen,

ich habe ein kleines Python Script geschrieben, welches als Ziel hat, mir alle Verfügbaren Lunix Updates anzuzeigen, und anhand dieser Liste gleich schaut, welche Version aktuell installiert ist.
Leider sieht die Ausgabe nicht gerade übersichtlich aus, bzw. ist zu verwenden, um diese in ein Outputfile zu schreiben.

Könnt Ihr mit helfen ?

Zielausgabe sollte wie folgt sein:

hostname ; packagename ; installierte version ; verfügbare version ;

aber irgendwie hab ich gerade einen hänger was die formatierung angeht.

Anbei der Code von meinem Script:

#!/usr/bin/env python
import os
import platform
import subprocess
import re

outputfile = "yum.output.txt"  

if platform.system() == "Linux":  

    if os.path.exists(outputfile):
        os.remove(outputfile)
    cmd = os.system("yum list updates >> yum.output.txt")  

    # ersten 3 Zeilen aus Header entfernen
    fobj = file(outputfile, 'r')  
    lines = fobj.readlines()
    fobj.close()

    del lines[0:3]

    fobj = file(outputfile, 'w')  
    for i in lines:
        fobj.write(i)
    fobj.close()
    ##################

    for line in file(outputfile):
        line2 = re.sub(r'\s+', ';', line)  
        arr = line2.split(";")  
        instpackage = arr
        instpackage2 = instpackage.split(".")  
        instpackage3 = ''.join(instpackage2)  
        updversion = arr[1]

        cmdhosts = os.system("hostname")  
        cmdhostname = ''.join(str(cmdhosts))  

        cmd2 = os.system("rpm -qa | grep " + instpackage3)  

        cmd3 = ''.join(str(cmd2))  

        print (cmdhostname + ";" + instpackage2 + ";" + updversion + ";" + cmd3 + ";\n")  

Aktuell sieht meine Ausgabe wie folgt aus:

c29-rs-pt-pr
perl-Net-SSLeay-1.35-10.el6.x86_64
0;perl-Net-SSLeay;1.35-10.el6_8.1;0;

c29-rs-pt-pr
policycoreutils-2.0.83-29.el6.x86_64
0;policycoreutils;2.0.83-30.1.el6_8;0;

c29-rs-pt-pr
python-dateutil-1.4.1-6.el6.noarch
python-pycurl-7.19.0-9.el6.x86_64
python-2.6.6-64.el6.x86_64
python-iniparse-0.3.1-2.1.el6.noarch
newt-python-0.52.11-3.el6.x86_64
python-lxml-2.2.3-1.1.el6.x86_64
python-ethtool-0.6-5.el6.x86_64
python-dmidecode-3.10.13-3.el6_4.x86_64
dbus-python-0.83.0-6.1.el6.x86_64
libxml2-python-2.7.6-21.el6_8.1.x86_64
rpm-python-4.8.0-55.el6.x86_64
redhat-support-lib-python-0.9.7-4.el6.noarch
python-urlgrabber-3.9.1-11.el6.noarch
python-libs-2.6.6-64.el6.x86_64
python-simplejson-2.0.9-3.1.el6.x86_64
python-gudev-147.1-4.el6_0.1.x86_64
python-magic-5.04-30.el6.x86_64
python-rhsm-1.16.6-1.el6.x86_64
0;python;2.6.6-66.el6_8;0;

Ich hoffe Ihr könnt mir helfen.

Gruß

Content-Key: 321841

Url: https://administrator.de/contentid/321841

Ausgedruckt am: 19.03.2024 um 07:03 Uhr