#!/usr/bin/python # -*- coding: utf8 -*- from os.path import exists as path_exists from optparse import OptionParser from sys import stdout, stderr, exit from checkdesc.gen_desc import IPADDR, ROUTE, IFCONFIG, writeDescXML DEFAULT_VERSION = "1.3" DEFAULT_FILENAME = "desc.xml" def parseOptions(): parser = OptionParser(usage="%prog [options]") parser.add_option("-o", "--output", help="output filename (default: %s), '-' means stdout" % DEFAULT_FILENAME, action="store", type="str", default=DEFAULT_FILENAME) parser.add_option("--ipaddr", help="Filename of interfaces ('%s' command output)" % " ".join(IPADDR), action="store", type="str") parser.add_option("--ifconfig", help="Filename of interfaces ('%s' command output)" % " ".join(IFCONFIG), action="store", type="str") parser.add_option("--routes", help="Filename of routes ('%s' command output)" % " ".join(ROUTE), action="store", type="str") parser.add_option("--version", help="desc.xml format version (default: %s)" % DEFAULT_VERSION, action="store", type="str", default=DEFAULT_VERSION) parser.add_option("--ignore", help="Ignore specified interface", action="append", type="str", default=[]) options, arguments = parser.parse_args() if arguments: parser.print_help() exit(1) return options def main(): # Parse command line options options = parseOptions() desc_version = options.version filename = options.output # Output already exists? if filename != "-": if path_exists(filename): print >>stderr, "Error: File already exists: %s" % filename exit(1) output = open(filename, 'w') else: output = stdout ifconfig = None ifconfig_format = 'ip addr' if options.ipaddr: ifconfig = open(options.ipaddr) elif options.ifconfig: ifconfig = open(options.ifconfig) ifconfig_format = 'ifconfig' routes = None if options.routes: routes = open(options.routes) writeDescXML( desc_version, output, filename, ifconfig_format, ifconfig, routes, options.ignore) if filename != '-': print "File wrote into: %s" % filename if __name__ == "__main__": main()