Wir haben hier einen Linux Printserver (SLES10) der die Windows-Clients bedient. In den Druckern wurde der Standort der Geräte eingetragen. Um nun nicht die Infos per Hand in CUPS und SAMBA nachtragen zu müssen habe ich mir folgendes Script einfallen lassen. Die Drucker werden mit snmp abgefragt und per lpadmin und rpcclient werden die Infos im CUPS und SAMBA eingetragen.
Ich habe auf dem Server nur python 2.4.2 zur Verfügung und das python-cups Paket ist auch nicht installiert. Deshalb hab ich das ganze mit os.popen gelöst.
Was will ich nun von euch? Nun, ich wollte das ganze mal objektorientiert angehen, denn hab da noch keinerlei Erfahrung.
Hab ich das richtig umgesetzt oder sieht man an hand vom code, dass ich es nicht kapiert hab?
Was kann ich verbessern bzw. was hab ich falsch gemacht? (Ich weiss, ich hab viel zu wenig Kommentare reingemacht)
Danke schonmal im Vorraus für die Kritik bzw. Anregungen.
Code: Alles auswählen
#!/usr/bin/python
import os
import re
import time
from optparse import OptionParser
parser = OptionParser("getloc.py [Option] <Printer>")
parser.add_option("-l", "--list", dest="list", action="store_true",
help="show info about Printer via snmp")
parser.add_option("-u", "--updatecups", dest="updcups",
action="store_true", help="update CUPS info about Printer via snmp")
parser.add_option("-s", "--updatesamba", dest="updsamba",
action="store_true", help="update SAMBA info about Printer via snmp")
(options, args) = parser.parse_args()
# Defaults
def_Loc = ["xxx/ / "]
def_Desc = [" "]
# Class definitions
class printer(object):
""" This class defines printer queues in CUPS and SAMBA.
Defined methods and propertis:
list() - lists information about the printers
getLoc() - gets the sysLocation from printer via snmpget
getDesc()- gets the hrDeviceDescr.1 from printer via snmpget
"""
def __init__(self, queue):
print "Initialize printer queue " + queue
self.Queue = queue
self.getLoc()
self.getDesc()
def list(self):
print " "
print "Printer Queue : " + self.Queue
print "sysLocation : " + self.Loc[0]
print "sysDescription: " + self.Desc[0]
def getLoc(self):
# get sysLocation.0 via snmpget
snmp_loc = os.popen("snmpget -v 1 -c public -O vq " + self.Queue
+ " sysLocation.0")
self.Loc = [zeile.strip() for zeile in snmp_loc]
rc = snmp_loc.close()
# rc = None -> snmpget was successful
if rc == None:
print "--- sysLoc snmpget " + self.Queue + " successful "
if self.Loc.count("") > 0:
self.Loc.remove("")
if len(self.Loc) == 0:
print "--- Printer doesn't have snmp Location"
self.Loc = def_Loc
else:
print "--- Error at sysLoc snmpget " + self.Queue
self.Loc = def_Loc
def getDesc(self):
snmp_desc = os.popen("snmpget -v 1 -c public -O vq " + self.Queue
+ " hrDeviceDescr.1")
self.Desc = [zeile.strip() for zeile in snmp_desc]
rc = snmp_desc.close()
if rc == None:
print "--- Descr snmpget " + self.Queue + " successful "
if self.Desc.count("") > 0:
self.Desc.remove("")
if len(self.Desc) == 0:
print "--- Printer doesn't have snmp Description"
self.Desc = def_Desc
else:
print "--- Error at Descr snmpget " + self.Queue
self.Desc = def_Desc
def updateCups(self):
upd_cups = "/usr/sbin/lpadmin -p " + self.Queue + " -D\"" +
self.Loc[0] + " " + self.Desc[0] + "\""
print upd_cups
rc = os.system(upd_cups)
if rc == 0:
print "--- Update CUPS successful"
else:
print "--- Error by update CUPS"
def updateSamba(self):
upd_smb = "/usr/bin/rpcclient -k -c 'setprinter " + self.Queue +
" \"" + self.Loc[0] + " " + self.Desc[0] + "\"' localhost "
print upd_smb
rc = os.system(upd_smb)
if rc == 0:
print "--- Update SAMBA successful"
else:
print "--- Error by update SAMBA"
# Main
if __name__ == '__main__':
printers = [printer(line) for line in args]
if options.list:
for i in range(len(args)):
printers[i].list()
if options.updcups:
for i in range(len(args)):
printers[i].list()
printers[i].updateCups()
if options.updsamba:
for i in range(len(args)):
printers[i].list()
printers[i].updateSamba()