3
## redhat-config-printer
6
## Copyright (C) 2002, 2003 Red Hat, Inc.
7
## Copyright (C) 2002, 2003 Tim Waugh <twaugh@redhat.com>
9
## This program is free software; you can redistribute it and/or modify
10
## it under the terms of the GNU General Public License as published by
11
## the Free Software Foundation; either version 2 of the License, or
12
## (at your option) any later version.
14
## This program is distributed in the hope that it will be useful,
15
## but WITHOUT ANY WARRANTY; without even the implied warranty of
16
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
## GNU General Public License for more details.
19
## You should have received a copy of the GNU General Public License
20
## along with this program; if not, write to the Free Software
21
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
## $Id: pysmb.py,v 1.2 2003/05/20 10:07:00 lordsutch Exp $
29
nmblookup = "/usr/bin/nmblookup"
30
smbclient = "/usr/bin/smbclient"
33
"""Return a dict, indexed by SMB name, of host dicts for this network.
35
A host dict has 'NAME' and 'IP' keys, and may have a 'GROUP' key."""
38
if not os.access (nmblookup, os.X_OK):
42
signal.signal (signal.SIGCHLD, signal.SIG_DFL)
43
for l in os.popen ("LANG=C %s '*'" % nmblookup, 'r').readlines ():
44
if l.endswith (" *<00>\n"):
45
ips.append (l.split (" ")[0])
50
signal.signal (signal.SIGCHLD, signal.SIG_DFL)
51
for l in os.popen ("LANG=C %s -A '%s'" % (nmblookup, ip),
54
if l.find (" <00> ") == -1:
58
val = l.split (" ")[0]
59
if val.find ("~") != -1:
62
if l.find (" <GROUP> ") != -1 and not dict.has_key ('GROUP'):
63
dict['GROUP'] = l.split (" ")[0]
64
elif not dict.has_key ('NAME'):
65
name = l.split (" ")[0]
77
def get_host_info (smbname):
78
"""Given an SMB name, returns a host dict for it."""
79
dict = { 'NAME': smbname, 'IP': '', 'GROUP': '' }
80
for l in os.popen ("LANG=C %s -S '%s'" % (nmblookup, smbname),
83
if l.endswith ("<00>"):
84
dict['IP'] = l.split (" ")[0]
87
if l.find (" <00> ") == -1:
90
if l.find (" <GROUP> ") != -1:
91
dict['GROUP'] = l.split (" ")[0]
93
name = l.split (" ")[0]
98
def get_printer_list (host):
99
"""Given a host dict, returns a dict of printer shares for that host.
100
The value for a printer share name is its comment."""
103
if not os.access (smbclient, os.X_OK):
106
str = "LANG=C %s -N -L '%s'" % (smbclient, host['NAME'])
107
if host.has_key ('IP'):
108
str += " -I '%s'" % host['IP']
110
if host.has_key ('GROUP'):
111
str += " -W '%s'" % host['GROUP']
113
signal.signal (signal.SIGCHLD, signal.SIG_DFL)
115
for l in os.popen (str, 'r'):
130
share = l[:l.find (" ")]
131
rest = l[len (share):].strip ()
132
end = rest.find (" ")
137
type = rest[:rest.find (" ")]
138
comment = rest[len (type):].strip ()
140
if type == "Printer":
141
printers[share] = comment
145
def printer_share_accessible (share, group = None, user = None, passwd = None):
146
"""Returns None if the share is inaccessible. Otherwise,
147
returns a dict with 'GROUP' associated with the workgroup name
150
if not os.access (smbclient, os.X_OK):
153
args = [ smbclient, share ]
158
args.extend (["-W", group])
160
args.extend (["-N", "-P", "-c", "quit"])
162
args.extend (["-U", user])
164
read, write = os.pipe ()
165
signal.signal (signal.SIGCHLD, signal.SIG_DFL)
172
os.environ['LANG'] = 'C'
173
os.execv (args[0], args)
177
dict = { 'GROUP': ''}
179
for l in os.fdopen (read, 'r').readlines ():
180
if l.startswith ("Domain=[") and l.find ("]") != -1:
181
dict['GROUP'] = l[len("Domain=["):].split ("]")[0]
184
pid, status = os.waitpid (pid, 0)
190
if __name__ == '__main__':
191
hosts = get_host_list ()
192
print get_printer_list (hosts[hosts.keys ()[0]])