root/trunk/source/hes/hes

Revision 119, 1.3 kB (checked in by broder, 16 years ago)

hes...Now With More Python!

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env python
2
3 """hes does Hesiod lookups for the most common types of Hesiod entries."""
4
5 import os
6 import sys
7
8 keys = ["passwd", "filsys", "pobox", "gid", "uid", "grplist", "sloc", "cluster",
9 "group", "pcap", "service", "lpralias", "printinfo", "palladium", "tloc"]
10
11 usage = """
12 Usage: hes thing_you_want_info_about  [next_thing ...]  [type]
13
14 Where 'type' is: %s
15 """ % ", ".join(keys)
16
17 def query(thing, type):
18         input, output, err = os.popen3(('hesinfo', thing, type))
19         # hesinfo prints errors (like queries not existing) to stderr, so if it's
20         # empty, we're probably good
21         if err.read() == '':
22                 return output.read().strip()
23
24 def printQuery(thing, type):
25         """printQuery takes the output of a Hesiod query, formats it, and prints it
26         out"""
27         info = query(thing, type)
28         if info != None:
29                 print "%s: %s" % (type.upper().rjust(10), info)
30
31 def main():
32         # Print the usage information if there were no arguments
33         if len(sys.argv) <= 1:
34                 print usage
35                 return
36        
37         # If a specific type of query was asked for, do that for each thing
38         if sys.argv[-1] in keys:
39                 type = sys.argv[-1]
40                 for thing in sys.argv[1:-1]:
41                         printQuery(thing, type)
42         # Otherwise LOOKUP EVERYTHING!
43         else:
44                 for thing in sys.argv[1:]:
45                         for type in keys:
46                                 printQuery(thing, type)
47                         # ...with a blank line between each thing being looked up
48                         print ""
49
50 if __name__ == "__main__":
51         main()
Note: See TracBrowser for help on using the browser.