def parsecli(): """Parse CLI arguments and return an object containing values for all of our options.""" if sys.version_info[0] < 3 and sys.version_info[1] < 7: parser = OptionParser() parser.add_option('-f', action='store', dest='csv', help='Path to a CSV file with names of the servers to update.') parser.add_option('-y', action='store_true', dest='yes', default=False, help='Auto answers \'yes\' to all questions.') parser.add_option('-g', action='store', dest='patching_group', help='Patching group to use. Should be one of the following: MSK.PROD1, MSK.PROD2, MSK.UAT1, MSK.UAT2') parser.add_option('-o', action='store_true', dest='report', default=False, help='Generate CSV with a report or prints to stdout otherwise.') parser.add_option('-r', action='store_true', dest='reboot', default=False, help='Reboot successfully updated systems.') parser.add_option('-s', action='callback', callback=vararg_callback, dest="servers_list", help='Space separated list of servers to update.') (options, args) = parser.parse_args() if options.servers_list and options.csv: print("\n-s and -f options are mutual exclusive.\n") parser.print_help() sys.exit(-1) if not options.servers_list and not options.csv: print("\nEither -s or -f options must be specified.\n") parser.print_help() sys.exit(-1) if options.csv and not options.patching_group: print("\nPatching group definition is missing.\n") parser.print_help() sys.exit(-1) return options else: parser = argparse.ArgumentParser(description='Update Linux servers using Spacewalk API.') parser.add_argument('-f', help='Path to a CSV file which contains names of the servers to update.') parser.add_argument('-y', action='store_const', dest='yes', const=0, help='Auto answers \'yes\' to all questions.') parser.add_argument('-g', action='store', dest='patching_group', help='Patching group to use. Should be one of the following: MSK.PROD1, MSK.PROD2, MSK.UAT1, MSK.UAT2') parser.add_argument('-s', help='Space separated list of servers to update.') parser.parse_args()
def _get_parserobj(self, option_list): """judged to parser type, return tp parser object :param option_list: parser option list :return: parser object, optparse.OptionParser or argparse.ArgumentParser :rtype: parser object class """ if '--version' in self.parselines[0]: if 'optparse' == self.parser_type: parser = OptionParser(version="dummy") else: parser = ArgumentParser( version='dummy', formatter_class=RawDescriptionHelpFormatter) else: if 'optparse' == self.parser_type: parser = OptionParser() else: parser = ArgumentParser( formatter_class=RawDescriptionHelpFormatter) for opt in option_list: if opt['short'] and self.parser_type is 'optparse': parser.add_option(opt['short'], opt['long'], metavar=opt['metavar'], help=opt['help'].strip()) elif not opt['short'] and self.parser_type is 'optparse': parser.add_option(opt['long'], metavar=opt['metavar'], help=opt['help'].strip()) elif opt['short'] and self.parser_type is 'argparse': parser.add_argument(opt['short'], opt['long'], metavar=opt['metavar'], help=opt['help'].strip()) elif not opt['short'] and self.parser_type is 'argparse': parser.add_argument(opt['long'], metavar=opt['metavar'], help=opt['help'].strip()) else: raise InvalidParserTypeError("Invalid paresr type.") return parser
def parse_args(CONFIG_PATH=''): """ Parse the arguments from the command line """ try: from argparse import ArgumentParser, SUPPRESS except ImportError: from optparse import OptionParser from optparse import SUPPRESS_HELP as SUPPRESS parser = OptionParser() parser.add_option("-f", "--file", dest="filename", default=path.join(CONFIG_PATH, 'poezio.cfg'), help="The config file you want to use", metavar="CONFIG_FILE") parser.add_option("-d", "--debug", dest="debug", help="The file where debug will be written", metavar="DEBUG_FILE") parser.add_option("-v", "--version", dest="version", help=SUPPRESS, metavar="VERSION", default="0.8-dev") (options, args) = parser.parse_args() else: parser = ArgumentParser() parser.add_argument("-f", "--file", dest="filename", default=path.join(CONFIG_PATH, 'poezio.cfg'), help="The config file you want to use", metavar="CONFIG_FILE") parser.add_argument("-d", "--debug", dest="debug", help="The file where debug will be written", metavar="DEBUG_FILE") parser.add_argument("-v", "--version", dest="version", help=SUPPRESS, metavar="VERSION", default="0.8-dev") options = parser.parse_args() return options
def parse_args(CONFIG_PATH=''): """ Parse the arguments from the command line """ try: from argparse import ArgumentParser, SUPPRESS except ImportError: from optparse import OptionParser from optparse import SUPPRESS_HELP as SUPPRESS parser = OptionParser() parser.add_option("-f", "--file", dest="filename", default=path.join(CONFIG_PATH, 'poezio.cfg'), help=_("The config file you want to use"), metavar="CONFIG_FILE") parser.add_option("-d", "--debug", dest="debug", help=_("The file where debug will be written"), metavar="DEBUG_FILE") parser.add_option("-v", "--version", dest="version", help=SUPPRESS, metavar="VERSION", default="0.9-dev") (options, __) = parser.parse_args() else: parser = ArgumentParser() parser.add_argument("-f", "--file", dest="filename", default=path.join(CONFIG_PATH, 'poezio.cfg'), help=_("The config file you want to use"), metavar="CONFIG_FILE") parser.add_argument("-d", "--debug", dest="debug", help=_("The file where debug will be written"), metavar="DEBUG_FILE") parser.add_argument("-v", "--version", dest="version", help=SUPPRESS, metavar="VERSION", default="0.9-dev") options = parser.parse_args() return options
def parseArgs(): ''' Parses the command line arguments given with the command. Currently the arguments it looks for are: -c and --config ''' confPath = expectedConf interval = 10 versionInfo = sys.version_info if versionInfo[0] <= 6 and versionInfo[0] == 2: #if version is 2.6.x use optparse from optparse import OptionParser parser = OptionParser() parser.add_option('-c','--config', dest='configPath',help='The FILE the config should be read from. By default reads from etc/ceph-influxDB-metricsCollector/ceph-influxDB-metricsCollector.conf',metavar='FILE') parser.add_option('-i','--interval', dest='interval',help='The length of time between the running of plugins, given in minutes. By default is set to 1 minute') options, args = parser.parse_args() options=options.__dict__ else: #if version is newer use argparse from argparse import ArgumentParser parser = ArgumentParser(description='Gather metrics from the ceph cluster and send them to influxDB via the HTTP API using the line protocol') parser.add_argument('-c','--config', metAvar='FILE',dest='configPath') parser.add_argument('-i,','--interval', dest='interval') options = parser.parse_args() try: if not (options['configPath'] == '' or options['configPath'] == None): confPath = options['configPath'] except: confPath = expectedConf try: if not (options['interval'] == '' or options['interval'] == None): interval = int(options['interval'])*1 except: interval=10 return confPath, interval
def main(): """ ------------------------------------------------------------------------------- """ ############################################################################### # # ARGUMENT PARSING # ############################################################################### # SETUP ARGUMENT PARSER if py_version < (2, 7): parser = OptionParser() parser.add_option('-c', '--config', metavar='FILE', dest='configfile', \ type=str, help='configuration file') parser.add_option('-g', '--generate', metavar='FILE', dest='examplecfg', \ type=str, help='generate example configuration file') parser.add_option('-p', '--preprocess', metavar='FILE', dest='preprocessfile', \ type=str, help='preprocess data before sending to main script (NB: NOT WORKING!)') (args, options) = parser.parse_args() else: parser = ArgumentParser(description='Post-processing for TIGER') parser.add_argument('-c', '--config', metavar='FILE', dest='configfile', \ type=str, help='configuration file') parser.add_argument('-g', '--generate', metavar='FILE', dest='examplecfg', \ type=str, help='generate example configuration file') parser.add_argument('-p', '--preprocess', metavar='FILE', dest='preprocessfile', \ type=str, help='preprocess data before sending to main script (NB: NOT WORKING!)') args = parser.parse_args() if (args.configfile == None) and (args.examplecfg == None) and (args.preprocessfile == None): exit("Specify either -c/--config, -g/--generate or -p/--preprocess") elif (args.configfile != None): stdout.write("****************************************\nTIGER post-process\n%s\n****************************************\n" % args.configfile) TigerPostProcess(args.configfile) elif (args.examplecfg != None): TigerCreateExampleConfigFile(args.examplecfg) elif (args.preprocessfile != None): TigerPreProcess(args.preprocessfile) else: exit('Unknown options - check input') exit(0)
def create_parser(self, prog_name, subcommand): """ Create and return the ``ArgumentParser`` which will be used to parse the arguments to this command. """ if not self.use_argparse: def store_as_int(option, opt_str, value, parser): setattr(parser.values, option.dest, int(value)) # Backwards compatibility: use deprecated optparse module warnings.warn( "OptionParser usage for Django management commands " "is deprecated, use ArgumentParser instead", RemovedInDjango110Warning) parser = OptionParser(prog=prog_name, usage=self.usage(subcommand), version=self.get_version()) parser.add_option( '-v', '--verbosity', action='callback', dest='verbosity', default=1, type='choice', choices=['0', '1', '2', '3'], callback=store_as_int, help= 'Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output' ) parser.add_option( '--settings', help=( 'The Python path to a settings module, e.g. ' '"myproject.settings.main". If this isn\'t provided, the ' 'DJANGO_SETTINGS_MODULE environment variable will be used.' ), ) parser.add_option( '--pythonpath', help= 'A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".' ), parser.add_option('--traceback', action='store_true', help='Raise on CommandError exceptions') parser.add_option('--no-color', action='store_true', dest='no_color', default=False, help="Don't colorize the command output.") for opt in self.option_list: parser.add_option(opt) else: parser = CommandParser(self, prog="%s %s" % (os.path.basename(prog_name), subcommand), description=self.help or None) parser.add_argument('--version', action='version', version=self.get_version()) parser.add_argument( '-v', '--verbosity', action='store', dest='verbosity', default='1', type=int, choices=[0, 1, 2, 3], help= 'Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output' ) parser.add_argument( '--settings', help=( 'The Python path to a settings module, e.g. ' '"myproject.settings.main". If this isn\'t provided, the ' 'DJANGO_SETTINGS_MODULE environment variable will be used.' ), ) parser.add_argument( '--pythonpath', help= 'A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".' ) parser.add_argument('--traceback', action='store_true', help='Raise on CommandError exceptions') parser.add_argument('--no-color', action='store_true', dest='no_color', default=False, help="Don't colorize the command output.") if self.args: # Keep compatibility and always accept positional arguments, like optparse when args is set parser.add_argument('args', nargs='*') self.add_arguments(parser) return parser
help = "specify compiler (dmd, ldc, or gdc)" ext_help = "specify we are building a python extension" default = 'dmd' if six.PY2: from optparse import OptionParser parser = OptionParser() parser.add_option("--compiler", dest="compiler", default=default, help=help) parser.add_option("--as-extension", dest="extension", default=False, help=ext_help) (options, args) = parser.parse_args() else: from argparse import ArgumentParser parser = ArgumentParser("""Generates dub configurations for the python instance that invokes this script. """) parser.add_argument("--compiler", default=default, help=help) parser.add_argument("--as-extension", dest="extension", action='store_true', default=False, help=ext_help) options = parser.parse_args() compiler = new_compiler(options.compiler) compiler.build_exe = not options.extension class MockExt: def __init__(self): self.libraries = [] ext = build_ext(Distribution()) libraries = ext.get_libraries(MockExt()) lib_file = compiler._lib_file([]) if lib_file: lib_file = os.path.basename(lib_file).replace(compiler.static_lib_extension, "")
parser.add_option('--minimum-frequency',dest='min_freq',action='store',help='Minimum occurences of a word included in the Venncloud data [defaults to 3].', default=3) parser.add_option('--stem',dest='stem',action='store_true',help='Stem word clouds.', default=False) parser.set_defaults(stem=False) (options,args) = parser.parse_args() input_locs = args output_loc = options.output idf_loc = options.idf num_examples = int(options.examples) example_window = int(options.window) minimum_frequency = int(options.min_freq) stem = args['stem'] else: parser = argparse.ArgumentParser(description='Create a Venncloud html file.') parser.add_argument('--output',action='store',help='Where the output html file should be written.',default='generated_wordcloud.html') parser.add_argument('--idf',action='store',help='Location of an idf vector to be used, as a JSON file of a python dictionary -- see `create_idf_vector.py` to make one. If this argument is omitted, we will generate the idf vector from the provided documents.',default=None) parser.add_argument('--examples',action='store',help='Number of examples of each word to store [defaults to 5].',default=5) parser.add_argument('--window',action='store',help='Window size on each side for each example, in number of tokens [defaults to 5].',default=5) parser.add_argument('--minimum-frequency',action='store',help='Minimum occurences of a word included in the venncloud data [defaults to 3].',default=3) parser.add_argument('--stem',action='store_true',help='Stem word clouds.', default=False) parser.set_defaults(stem=False) parser.add_argument('documents', metavar='N', nargs='+', help='Location of the documents for the datasets to be loaded -- plain text, 1 document per line.') args = vars(parser.parse_args()) input_locs = args['documents'] output_loc = args['output'] idf_loc = args['idf'] num_examples = int(args['examples'])
pass if __name__ == "__main__": args = None if sys.version_info < (2, 7): from optparse import OptionParser parser = OptionParser() parser.add_option("-u", "--user", help="Cato root db user.") parser.add_option("-p", "--password", help="Cato root db password.") (args, arglist) = parser.parse_args() else: import argparse parser = argparse.ArgumentParser() parser.add_argument("-u", "--user", help="Cato root db user.") parser.add_argument("-p", "--password", help="Cato root db password.") args = parser.parse_args() if not args: raise Exception( "Unable to continue - unable to parse command line arguments.") UID = args.user PWD = args.password if args.password else "" db = catodb.Db() db.connect_db( user=UID, password=PWD, server=catoconfig.CONFIG[ "server"], port=catoconfig.CONFIG["port"], database=catoconfig.CONFIG["database"])
f.write(pkikeys[pkikey]) f.close() print("Files have been written in %s." % tmpdir) return tmpdir, tmpsid if __name__ == "__main__": parser = OptionParser() parser.add_option("--sid", metavar="SID", dest="sid") parser.add_option("--masterkey", metavar="DIRECTORY", dest="masterkeydir") parser.add_option("--credhist", metavar="FILE", dest="credhist") parser.add_option("--password", metavar="PASSWORD", dest="password") parser.add_option("--hash", metavar="HASH", dest="h") parser.add_argument("--syskey", required=False, metavar="PASSWORD", dest="syskey", help="DPAPI_SYSTEM string. (01000000...)") parser.add_option("--private_keys", metavar="DIRECTORY", dest="privkeys") parser.add_option("--certificates", metavar="DIRECTORY", dest="certs") parser.add_option("--domainkey", metavar="FILE", dest="domkey") parser.add_option("--log", metavar="FILE", dest="log") parser.add_option("--rsaout", metavar="DIRECTORY", dest="rsaout") #output decdrypted blobs to comp. files parser.add_option("--pfxdir", metavar="DIRECTORY", dest="pfxdir") parser.add_option("--ldap-server", metavar="string", dest="ldaps", help="ldap server IP or domain name") parser.add_option("--ldap-connect", metavar="string",
# -*- coding: utf-8 -*-
"-n", "--min-files", dest="minfiles", default="5", help="Minimum number of logs per application to keep") parser.add_option("-t", "--min-age", dest="minage", default="7", help="Minimum time (days) to keep log files") (opts, args) = parser.parse_args() else: parser = ArgumentParser() parser.add_argument('-p', "--path", dest="logpath", default="/var/log/mythtv", help="Path where log files are stored") parser.add_argument( '-n', "--min-files", dest="minfiles", type=int, default=5, help="Minimum number of logs per application to keep") parser.add_argument("-t", "--min-age", dest="minage", type=int, default=7, help="Minimum time (days) to keep log files")
def main(): description = 'Helper script that gives you all the access tokens your account has.' if have_argparse: parser = argparse.ArgumentParser(description=description, formatter_class=argparse.ArgumentDefaultsHelpFormatter) else: parser = OptionParser('%prog [options] user_name', description=description) parser.add_argument('--url', default='https://api.signalfx.com/v1', help='SignalFX endpoint') parser.add_argument('--password', default=None, help='Optional command line password') parser.add_argument('--org', default=None, help='If set, change output to only the auth token of this org') parser.add_argument('--print_user_org', default=False, action='store_true', help='If set, change output to only the auth token of this org') parser.add_argument('--update', default=None, help='If set, will look for a collectd file and auto update to the auth token you select.') parser.add_argument('--print_token_only', default=False, action='store_true', help='If set, only print out tokens') parser.add_argument('--error_on_multiple', default=False, action ='store_true', help='If set then an error will be raised if the user is part of multiple organizations ' 'and --org is not specified') if have_argparse: parser.add_argument('user_name', help="User name to log in with") args = parser.parse_args() else: (args, leftover) = parser.parse_args() if not leftover: parser.error("User name to log in with must be specified.") if len(leftover) != 1: parser.error("Only one user name to log in with must be specified.") args.user_name = leftover[0] if args.update is not None: assert os.path.isfile(args.update), "Unable to find the file to update: " + args.update if args.password is None: args.password = getpass.getpass('SignalFX password: '******'content-type': 'application/json'} req = urllib2.Request(args.url + "/session", json.dumps(json_payload), headers) try: resp = urllib2.urlopen(req) except urllib2.HTTPError, e: if e.code == 201: resp = e else: sys.stderr.write("Invalid user name/password\n") sys.exit(1)
def main(): from optparse import OptionParser p = OptionParser(description="conda link tool used by installer") p.add_option('--file', action="store", help="path of a file containing distributions to link, " "by default all packages extracted in the cache are " "linked") p.add_option('--prefix', action="store", default=sys.prefix, help="prefix (defaults to %default)") p.add_option('-v', '--verbose', action="store_true") if sys.platform == "win32": p.add_argument( "--shortcuts", action="store_true", help="Install start menu shortcuts" ) opts, args = p.parse_args() if args: p.error('no arguments expected') logging.basicConfig() prefix = opts.prefix pkgs_dir = join(prefix, 'pkgs') pkgs_dirs[0] = [pkgs_dir] if opts.verbose: print("prefix: %r" % prefix) if opts.file: idists = list(yield_lines(join(prefix, opts.file))) else: idists = sorted(extracted()) linktype = (LINK_HARD if idists and try_hard_link(pkgs_dir, prefix, idists[0]) else LINK_COPY) if opts.verbose: print("linktype: %s" % link_name_map[linktype]) for dist in idists: if opts.verbose: print("linking: %s" % dist) link(prefix, dist, linktype, opts.shortcuts) messages(prefix) for dist in duplicates_to_remove(linked(prefix), idists): meta_path = join(prefix, 'conda-meta', dist + '.json') print("WARNING: unlinking: %s" % meta_path) try: os.rename(meta_path, meta_path + '.bak') except OSError: rm_rf(meta_path)
parser.add_option('-m', '--mapset', dest='mapset', help='The GRASS mapset') parser.add_option('-p', '--patchmap', dest='patchmap', help='The name of the GRASS raster to use for the patch map') parser.add_option('-z', '--zonemap', dest='zonemap', help='The name of the GRASS raster to use for the zone map') parser.add_option('-i', '--hillmap', dest='hillmap', help='The name of the GRASS raster to use for the hill map') parser.add_option('-c', '--coordinates', dest='coordinates', help='The path to comma separated file containing easting,northing coordinates in UTM18N NAD83 spatial reference system. Will skip the first line in the file.') (args, args_args) = parser.parse_args() else: # Python 2.7 or later import argparse parser = argparse.ArgumentParser(description='Read fully qualified RHESSys patch IDs (combination of patchID, zoneID, and hillID) for a list of UTM coordinates') parser.add_argument('-g', '--grassdbase', dest='grassdbase', required=True, help='The path to the GRASS database') parser.add_argument('-l', '--location', dest='location', required=True, help='The location of GRASS mapset') parser.add_argument('-m', '--mapset', dest='mapset', required=True, help='The GRASS mapset') parser.add_argument('-p', '--patchmap', dest='patchmap', required=True, help='The name of the GRASS raster to use for the patch map') parser.add_argument('-z', '--zonemap', dest='zonemap', required=True, help='The name of the GRASS raster to use for the zone map') parser.add_argument('-h', '--hillmap', dest='hillmap', required=True, help='The name of the GRASS raster to use for the hill map') parser.add_argument('-c', '--coordinates', dest='coordinates', required=True, help='The path to comma separated file containing easting,northing coordinates in UTM18N NAD83 spatial reference system. Will skip the first line in the file.') args = parser.parse_args() if not os.access(args.coordinates, os.R_OK):
""" info = self.schedule.get("info") date_string = todays_date() hbar_length = len(info) + len(date_string) + 4 message = "{} -- {}\n{}\n{}\n".format( self.schedule.get("info"), date_string, "=" * hbar_length, ", ".join(self.schedule.get("data2")[day_of_year() - 1])) sys.stdout.writelines(message) if __name__ == "__main__": from argparse import ArgumentParser prsr = ArgumentParser() prsr.add_argument("-j", "--json", dest="json_file", help="Reading plan; FILE in json format", metavar="FILE") prsr.add_argument("-p", "--pretty", action="store_true") args = prsr.parse_args() pasg = DailyReading(args.json_file) if args.pretty: pasg.print_passages() else: for x in pasg.get_passages(): print(x)
else: print("no submission") elif not os.path.isfile("{0}step5.log".format(New_File_Name)): os.system("qsub namd/{0}step5 >> submission.log".format( New_File_Name)) else: print("no submission") else: print("equilibration not done") sys.exit(0) JobActive = True while JobActive == True: fh = open("checkforactive") os.system("qstat > checkforactive") QueueStatus = fh.read() print("step5_{0}".format(New_File_Name) in QueueStatus) if "step5_{0}".format(New_File_Name) in QueueStatus: JobActive = True print("job is running") time.sleep(60) else: JobActive = False print("job is not running") """import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=str, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=, default=max, help='sum the integers (default: find the max)') args = parser.parse_args() print args.accumulate(args.integers)"""
def create_parser(self, prog_name, subcommand): """ Create and return the ``ArgumentParser`` which will be used to parse the arguments to this command. """ if not self.use_argparse: # Backwards compatibility: use deprecated optparse module warnings.warn( "OptionParser usage for Django management commands " "is deprecated, use ArgumentParser instead", RemovedInDjango20Warning, ) parser = OptionParser(prog=prog_name, usage=self.usage(subcommand), version=self.get_version()) parser.add_option( "-v", "--verbosity", action="store", dest="verbosity", default="1", type="choice", choices=["0", "1", "2", "3"], help="Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output", ) parser.add_option( "--settings", help='The Python path to a settings module, e.g. "myproject.settings.main". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.', ) parser.add_option( "--pythonpath", help='A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".' ), parser.add_option("--traceback", action="store_true", help="Raise on exception") parser.add_option( "--no-color", action="store_true", dest="no_color", default=False, help="Don't colorize the command output.", ) for opt in self.option_list: parser.add_option(opt) else: parser = CommandParser( self, prog="%s %s" % (os.path.basename(prog_name), subcommand), description=self.help or None ) parser.add_argument("--version", action="version", version=self.get_version()) parser.add_argument( "-v", "--verbosity", action="store", dest="verbosity", default="1", type=int, choices=[0, 1, 2, 3], help="Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output", ) parser.add_argument( "--settings", help='The Python path to a settings module, e.g. "myproject.settings.main". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.', ) parser.add_argument( "--pythonpath", help='A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".' ) parser.add_argument("--traceback", action="store_true", help="Raise on exception") parser.add_argument( "--no-color", action="store_true", dest="no_color", default=False, help="Don't colorize the command output.", ) if self.args: # Keep compatibility and always accept positional arguments, like optparse when args is set parser.add_argument("args", nargs="*") self.add_arguments(parser) return parser
def parsecli(): """Parse CLI arguments and return an object containing values for all of our options.""" if sys.version_info[0] < 3 and sys.version_info[1] < 7: parser = OptionParser() parser.add_option( '-f', action='store', dest='csv', help='Path to a CSV file with names of the servers to update.') parser.add_option('-y', action='store_true', dest='yes', default=False, help='Auto answers \'yes\' to all questions.') parser.add_option( '-g', action='store', dest='patching_group', help= 'Patching group to use. Should be one of the following: MSK.PROD1, MSK.PROD2, MSK.UAT1, MSK.UAT2' ) parser.add_option( '-o', action='store_true', dest='report', default=False, help='Generate CSV with a report or prints to stdout otherwise.') parser.add_option('-r', action='store_true', dest='reboot', default=False, help='Reboot successfully updated systems.') parser.add_option('-s', action='callback', callback=vararg_callback, dest="servers_list", help='Space separated list of servers to update.') (options, args) = parser.parse_args() if options.servers_list and options.csv: print("\n-s and -f options are mutual exclusive.\n") parser.print_help() sys.exit(-1) if not options.servers_list and not options.csv: print("\nEither -s or -f options must be specified.\n") parser.print_help() sys.exit(-1) if options.csv and not options.patching_group: print("\nPatching group definition is missing.\n") parser.print_help() sys.exit(-1) return options else: parser = argparse.ArgumentParser( description='Update Linux servers using Spacewalk API.') parser.add_argument( '-f', help= 'Path to a CSV file which contains names of the servers to update.' ) parser.add_argument('-y', action='store_const', dest='yes', const=0, help='Auto answers \'yes\' to all questions.') parser.add_argument( '-g', action='store', dest='patching_group', help= 'Patching group to use. Should be one of the following: MSK.PROD1, MSK.PROD2, MSK.UAT1, MSK.UAT2' ) parser.add_argument('-s', help='Space separated list of servers to update.') parser.parse_args()
PORTHELP = "Port to listen on" RESOLVEHELP = "Resolve system names to ipv4" PORTARG = "--port" RESOLVEARG = "--resolve" if sys.version < "2.7": from optparse import OptionParser parser = OptionParser() parser.add_option(PORTARG, default=PORT, help=PORTHELP) parser.add_option(RESOLVEARG, action="store_true", default=RESOLVE, help=RESOLVEHELP) (options, args) = parser.parse_args() else: import argparse parser = argparse.ArgumentParser(description="Provide /proc via bottle") parser.add_argument(PORTARG, default=PORT, help=PORTHELP) parser.add_argument(RESOLVEARG, default=RESOLVE, action="store_true", help=RESOLVEHELP) options = parser.parse_args() PORT = options.port RESOLVE = options.resolve PROCPATH = "/proc" PLAIN = "text/plaintext" def get_host_ip(hostname): """Resolve hostnames to ip""" if RESOLVE == True: return socket.gethostbyname(hostname) else: return hostname
for line in stdout: print line # wait until qsub is finished doing its magic p.wait() return pbs_script, stdout if __name__ == '__main__': # default_path = '/home/MET/STABCON/repositories/prepost' default_path = '/home/leob/bin/prepost' # parse the arguments, only relevant when using as a command line utility parser = OptionParser(usage = "%prog -f pythonfile") parser.add_argument = parser.add_option parser.add_argument('-f', '--file', type='string', dest='fname', action='store', default=None, help='python file name that should be run on cluster') parser.add_argument('-p', '--path', type='string', dest='fpath', action='store', default=default_path, help='path of the python file') parser.add_argument('--py_env', type='string', dest='py_env', help='name of the python environment', default='anaconda') # TODO: configure flags for default actions such post-process, plot, launch # in those cases the default folder layout is assumed # make sure a filename is given (options, args) = parser.parse_args()
def main(): description = 'Helper script that gives you all the access tokens your account has.' if have_argparse: parser = argparse.ArgumentParser(description=description, formatter_class=argparse.ArgumentDefaultsHelpFormatter) else: parser = OptionParser('%prog [options] user_name', description=description) parser.add_argument('--url', default='https://api.signalfx.com/v2', help='SignalFX endpoint') parser.add_argument('--password', default=None, help='Optional command line password') parser.add_argument('--org', default=None, help='If set, change output to only the auth token of this org') parser.add_argument('--print_user_org', default=False, action='store_true', help='If set, change output to only the auth token of this org') parser.add_argument('--update', default=None, help='If set, will look for a collectd file and auto update to the auth token you select.') parser.add_argument('--print_token_only', default=False, action='store_true', help='If set, only print out tokens') parser.add_argument('--error_on_multiple', default=False, action='store_true', help='If set then an error will be raised if the user is part of multiple organizations ' 'and --org is not specified') if have_argparse: parser.add_argument('user_name', help="User name to log in with") args = parser.parse_args() else: (args, leftover) = parser.parse_args() if not leftover: parser.error("User name to log in with must be specified.") if len(leftover) != 1: parser.error("Only one user name to log in with must be specified.") args.user_name = leftover[0] if args.update is not None: assert os.path.isfile(args.update), "Unable to find the file to update: " + args.update if args.password is None: args.password = getpass.getpass('SignalFX password: '******'User is part of more than one organization.\n') sys.exit(1) if args.print_token_only: for _, api_token in all_auth_tokens: print(api_token) sys.exit(1) for org_name, api_token in all_auth_tokens: if args.print_user_org or not org_name.startswith("per-user-org"): print("%40s%40s" % (org_name, api_token)) if args.update is None: sys.exit(0) assert len(all_auth_tokens) != 0 if len(all_auth_tokens) > 1: sys.stderr.write( "Multiple auth tokens associated with this account. Add an --org tag for the auth token you want to update to.\n") examples = ["get_all_auth_tokens.py --org=\"%s\"" % s[0] for s in all_auth_tokens] sys.stderr.write("\n".join(examples)+"\n") sys.exit(1) replace_in_file(args.update, 'APIToken "(.*)"', 'APIToken "%s"' % all_auth_tokens[0][1])
def startproject(): """ Starts a new django Organice project by first generating a Django project using ``django-admin.py``, and then modifying the project settings. """ usage_descr = 'django Organice setup. Start getting organiced!' if sys.version_info < (2, 7): from optparse import OptionParser # Deprecated since version 2.7 parser = OptionParser(description=usage_descr) (options, args) = parser.parse_args() if len(args) != 1: parser.error('Please specify a projectname') projectname = args[0] else: from argparse import ArgumentParser # New since version 2.7 parser = ArgumentParser(description=usage_descr) parser.add_argument('projectname', help='name of project to create') args = parser.parse_args() projectname = args.projectname mode0755 = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH profiles = ('develop', 'staging', 'production') filenames = ('__init__', 'common') + profiles print('Generating project %s ...' % projectname) code = call(['django-admin.py', 'startproject', projectname, '.']) if code != 0: return code os.chmod('manage.py', mode0755) print('Creating directories ...') os.mkdir('media') os.mkdir('static') os.mkdir('templates') os.mkdir(os.path.join(projectname, 'settings')) print('Converting settings to deployment profiles (%s) ...' % ', '.join(profiles)) os.rename(os.path.join(projectname, 'settings.py'), os.path.join(projectname, 'settings', 'common.py')) settings = DjangoSettingsManager(projectname, *filenames) settings.append_lines('__init__', '"""', 'Modularized settings generated by django Organice setup. http://organice.io', 'This solution follows the second recommendation from', 'http://www.sparklewise.com/django-settings-for-production-and-development-best-practices/', '"""', 'from .develop import *') for prof in profiles: settings.append_lines(prof, '# Django project settings for %s environment' % prof.capitalize(), '', 'from .common import *') # out-of-the-box Django values relevant for deployment settings.move_var('common', profiles, 'DEBUG') settings.move_var('common', profiles, 'TEMPLATE_DEBUG') settings.move_var('common', profiles, 'ALLOWED_HOSTS') settings.move_var('common', profiles, 'DATABASES') settings.move_var('common', profiles, 'SECRET_KEY') settings.move_var('common', profiles, 'WSGI_APPLICATION') settings.insert_lines('common', 'import os', 'PROJECT_PATH = os.sep.join(__file__.split(os.sep)[:-3])') settings.set_value('common', 'MEDIA_URL', "'/media/'") settings.set_value('common', 'MEDIA_ROOT', "os.path.join(PROJECT_PATH, 'media')") settings.set_value('common', 'STATIC_ROOT', "os.path.join(PROJECT_PATH, 'static')") settings.set_value('common', 'USE_I18N', False) settings.set_value('staging', 'DEBUG', False) settings.set_value('production', 'DEBUG', False) print('Configuring development database ...') DEV_DATABASES = """{ 'default': { 'ENGINE': 'django.db.backends.sqlite3', # 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': os.path.join(PROJECT_PATH, '%s.sqlite'), # path to database file if using sqlite3. # The following settings are not used with sqlite3: 'USER': '', 'PASSWORD': '', 'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. 'PORT': '', # Set to empty string for default. } }""" % projectname settings.set_value('develop', 'DATABASES', DEV_DATABASES) # configuration for included packages adding_settings_for = 'Adding settings for %s ...' print(adding_settings_for % 'installed apps') settings.delete_var('common', 'INSTALLED_APPS') settings.append_lines('common', 'INSTALLED_APPS = (', " 'django.contrib.auth',", " 'django.contrib.comments',", " 'django.contrib.contenttypes',", " 'django.contrib.sessions',", " 'django.contrib.sites',", " 'django.contrib.messages',", " 'django.contrib.staticfiles',", " 'django.contrib.admin',", " 'organice',", " 'cms',", " 'mptt',", " 'menus',", " 'south',", " 'sekizai',", " 'reversion',", " 'cms.plugins.text',", " 'cms.plugins.picture',", " 'cms.plugins.link',", " 'cms.plugins.teaser',", " 'cms.plugins.file',", " 'cms.plugins.video',", " 'cms.plugins.flash',", " 'cms.plugins.googlemap',", " 'cms.plugins.inherit',", " 'cmsplugin_contact',", " 'cmsplugin_zinnia',", " 'tagging',", " 'emencia.django.newsletter',", " 'tinymce',", " 'simple_links',", " 'zinnia',", ')') print(adding_settings_for % 'django CMS') settings.delete_var('common', 'MIDDLEWARE_CLASSES') settings.append_lines('common', 'MIDDLEWARE_CLASSES = (', " 'django.middleware.common.CommonMiddleware',", " 'django.middleware.doc.XViewMiddleware',", " 'solid_i18n.middleware.SolidLocaleMiddleware',", " 'django.middleware.csrf.CsrfViewMiddleware',", " 'django.contrib.sessions.middleware.SessionMiddleware',", " 'django.contrib.messages.middleware.MessageMiddleware',", " 'django.contrib.auth.middleware.AuthenticationMiddleware',", " 'cms.middleware.page.CurrentPageMiddleware',", " 'cms.middleware.user.CurrentUserMiddleware',", " 'cms.middleware.toolbar.ToolbarMiddleware',", " 'cms.middleware.language.LanguageCookieMiddleware',", ')') # must be set both in order to make solid_i18n work properly settings.set_value('common', 'LANGUAGE_CODE', """'en-us' LANGUAGES = ( ('en-us', 'English (United States)'), )""") settings.append_lines('common', 'CMS_TEMPLATES = (', " ('cms_article.html', 'Template for normal content pages'),", " ('cms_bookmarks.html', 'Template for the bookmarks page'),", ')', 'CMS_USE_TINYMCE = False') settings.delete_var('common', 'TEMPLATE_DIRS') settings.append_lines('common', 'TEMPLATE_DIRS = (', " # Don't forget to use absolute paths, not relative paths.", " os.path.join(PROJECT_PATH, 'templates'),", " os.path.join(PROJECT_PATH, 'templates', 'zinnia'),", ')') settings.append_lines('common', 'TEMPLATE_CONTEXT_PROCESSORS = (', " 'django.contrib.auth.context_processors.auth',", " 'django.core.context_processors.i18n',", " 'django.core.context_processors.request',", " 'django.core.context_processors.media',", " 'django.core.context_processors.static',", " 'cms.context_processors.media',", " 'sekizai.context_processors.sekizai',", " 'organice.context_processors.expose',", ')') print(adding_settings_for % 'Emencia Newsletter') settings.append_lines('common', "NEWSLETTER_DEFAULT_HEADER_SENDER = 'Your Organization <*****@*****.**>'", "NEWSLETTER_MEDIA_URL = '/media/' # emencia/django/newsletter/media/edn/ directory (alternative)", 'NEWSLETTER_USE_TINYMCE = True', 'TINYMCE_DEFAULT_CONFIG = {', " 'height': 450,", " 'width': 800,", " 'convert_urls': False,", " 'plugins': 'table,paste,searchreplace,template',", " 'theme': 'advanced',", " 'theme_advanced_toolbar_location': 'top',", " 'theme_advanced_buttons1': 'bold,italic,underline,forecolor,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,|,template',", " 'theme_advanced_buttons3_add': 'tablecontrols',", '}') print(adding_settings_for % 'Zinnia Blog') settings.append_lines('common', '# use plugin system of django-cms in blog entries', "ZINNIA_ENTRY_BASE_MODEL = 'cmsplugin_zinnia.placeholder.EntryPlaceholder'", "ZINNIA_WYSIWYG = 'wymeditor'") settings.append_lines('common', 'SOUTH_MIGRATION_MODULES = {', ' # integration of EntryPlaceholder (django CMS) into Zinnia', " 'zinnia': 'organice.migrations.zinnia',", '}') settings.save_files() print('Configuring project URLs ...') gen_by_comment = '# generated by django Organice' project = DjangoModuleManager(projectname) project.add_file('urls', lines=(gen_by_comment, 'from organice.urls import urlpatterns')) project.save_files() suggest_editing = ('ADMINS', 'TIME_ZONE', 'LANGUAGE_CODE', 'LANGUAGES') suggest_adding = ('SERVER_EMAIL', ) print('Done. Enjoy your organiced day!' + os.linesep) print('Please visit file `%s` and edit or add the variables: %s' % (settings.get_file('common').name, ', '.join(suggest_editing + suggest_adding))) print('Please visit file `%s` and configure your development database in: %s' % (settings.get_file('develop').name, 'DATABASES')) print('See https://docs.djangoproject.com/en/1.5/ref/settings/ for details.' + os.linesep) print('To initialize your development database run: `python manage.py syncdb --migrate`') print('You can then run your development server with: `python manage.py runserver`')
def main(): description = 'Helper script that gives you all the access tokens your account has.' if have_argparse: parser = argparse.ArgumentParser( description=description, formatter_class=argparse.ArgumentDefaultsHelpFormatter) else: parser = OptionParser('%prog [options] user_name', description=description) parser.add_argument('--url', default='https://api.signalfx.com', help='SignalFX endpoint') parser.add_argument('--password', default=None, help='Optional command line password') parser.add_argument( '--org', default=None, help='If set, change output to only the auth token of this org') parser.add_argument( '--print_user_org', default=False, action='store_true', help='If set, change output to only the auth token of this org') parser.add_argument( '--update', default=None, help= 'If set, will look for a collectd file and auto update to the auth token you select.' ) parser.add_argument( '--error_on_multiple', default=False, action='store_true', help= 'If set then an error will be raised if the user is part of multiple organizations ' 'and --org is not specified') if have_argparse: parser.add_argument('user_name', help="User name to log in with") args = parser.parse_args() else: (args, leftover) = parser.parse_args() if not leftover: parser.error("User name to log in with must be specified.") if len(leftover) != 1: parser.error( "Only one user name to log in with must be specified.") args.user_name = leftover[0] if args.update is not None: assert os.path.isfile( args.update), "Unable to find the file to update: " + args.update if args.password is None: args.password = getpass.getpass('SignalFX password: '******'content-type': 'application/json'} req = urllib2.Request(args.url + "/session", json.dumps(json_payload), headers) try: resp = urllib2.urlopen(req) except urllib2.HTTPError: sys.stderr.write("Invalid user name/password\n") sys.exit(1) res = resp.read() sf_accessToken = json.loads(res)['sf_accessToken'] sf_userID = json.loads(res)['sf_userID'] # Get the orgs orgs_url = args.url + "/organization?query=sf_organization:%s" % (args.org or '*') headers = { 'content-type': 'application/json', 'X-SF-TOKEN': sf_accessToken } req = urllib2.Request(orgs_url, headers=headers) resp = urllib2.urlopen(req) res = resp.read() all_res = json.loads(res) printed_org = False all_auth_tokens = [] for i in all_res['rs']: if args.org is not None: if args.org == i['sf_organization']: all_auth_tokens.append( (i['sf_organization'], i['sf_apiAccessToken'])) sys.stdout.write(i['sf_apiAccessToken']) printed_org = True else: if args.print_user_org or not i['sf_organization'].startswith( "per-user-org"): all_auth_tokens.append( (i['sf_organization'], i['sf_apiAccessToken'])) print("%40s%40s" % (i['sf_organization'], i['sf_apiAccessToken'])) if args.org is not None and not printed_org: sys.stderr.write("Unable to find the org you set.\n") sys.exit(1) if args.error_on_multiple and len(all_auth_tokens) > 1: sys.stderr.write('Users is part of more than one organization.\n') sys.exit(1) if args.update is None: sys.exit(0) assert len(all_auth_tokens) != 0 if len(all_auth_tokens) > 1: sys.stderr.write( "Multiple auth tokens associated with this account. Add an --org tag for the auth token you want to update to.\n" ) examples = [ "get_all_auth_tokens.py --org=\"%s\"" % s[0] for s in all_auth_tokens ] sys.stderr.write("\n".join(examples) + "\n") sys.exit(1) replace_in_file(args.update, 'APIToken "(.*)"', 'APIToken "%s"' % all_auth_tokens[0][1])
help="input log file", metavar="LOG_FILE") # parser.add_option("-d", "--directory", dest="dirname", help="input directory with log files", metavar="LOG_DIR") parser.add_option("-t", "--dbtype", dest="dbtype", help="database type", default="mongodb", metavar="DB_TYPE") (options, args) = parser.parse_args() else: import argparse parser = argparse.ArgumentParser(description="Log to database ingester") parser.add_argument("-f, --file", dest="filename", help="input log file", metavar="LOG_FILE") # parser.add_argument("-d, --directory", dest="dirname", help="input directory with log files", metavar="LOG_DIR") parser.add_argument("-t, --dbtype", dest="dbtype", help="database type", default="mongodb", metavar="DB_TYPE") options = parser.parse_args() print "file {0} ".format(options.filename) # print "dirname {0} ".format(options.dirname) print "dbtype {0}".format(options.dbtype) if options.dbtype == "mongodb": from DBDriver.MongoDBDriver import MongoDBDriver
def main(): country = False city = False asn = False if opt: usage = "usage: %prog [options] ip [ip ...]" parser = OptionParser(usage=usage) parser.add_option("-n", "--name", action="store_true", dest="name", default=False, help="Print country name instead of country code") parser.add_option("-t", "--country", dest="country", action="store_true", default=False, help="Do country lookup") parser.add_option("-d", "--database", dest="db", metavar="<db path>", help="Path to maxmind GeoIP database", default="/usr/local/share/GeoIP/GeoIP.dat") #City database parser.add_option("-c", "--city", dest="city", action="store_true", default=False, help="Do city lookup") parser.add_option("--city-database", dest="citydb", default="/usr/local/share/GeoIP/GeoLiteCity.dat", metavar="<city db path>", help="Path to maxmind city lite db") #ASN database parser.add_option("-a", "--asn", dest="asn", action="store_true", default=False, help="Do ASN lookup") parser.add_option("--asn-database", dest="asndb", default="/usr/local/share/GeoIP/GeoIPASNum.dat", metavar="<asn db path>", help="Path to maxmind asn lite db") options, args = parser.parse_args() if len(args) < 1: print "Need to provide ip address" parser.print_help() ips = args if options.city: city = True if checkFile(options.citydb): citydb = options.citydb if options.asn: asn = True if checkFile(options.asndb): asndb = options.asndb if options.country: country = True if checkFile(options.db): db = options.db if not city and not country and not asn: country = True if checkFile(options.db): db = options.db name = options.name else: parser = argparse.ArgumentParser() parser.add_argument('ips', metavar="ip", nargs="+", help="ip address/es to lookup") parser.add_argument("-co", "--country", dest="country", action="store_true", default=False, help="Do country lookup") parser.add_argument( "-n", "--name", dest="name", action="store_true", default=False, help="Print country names instead of country codes") parser.add_argument("-d", "--database", dest="db", default="/usr/local/share/GeoIP/GeoIP.dat", metavar="<db path>", help="Path to maxmind GeoIP database") parser.add_argument("-c", "--city", dest="city", action="store_true", default=False, help="Do city lookup") parser.add_argument("-dc", "--city-database", dest="citydb", default="/usr/local/share/GeoIP/GeoLiteCity.dat", metavar="<city db path>", help="Path to maxmind city lite db") parser.add_argument("-a", "--asn", dest="asn", action="store_true", default=False, help="Do ASN lookup") parser.add_argument("-da", "--asn-database", dest="asndb", default="/usr/local/share/GeoIP/GeoIPASNum.dat", metavar="<asn db path>", help="Path to maxmind asn lite db") args = parser.parse_args() name = args.name ips = args.ips if args.city: city = True if checkFile(args.citydb): citydb = args.citydb if args.asn: asn = True if checkFile(args.asndb): asndb = args.asndb if args.country: country = True if checkFile(args.db): db = args.db if not city and not country and not asn: country = True if checkFile(args.db): db = args.db ## Verify shiz ## IP addresses if not verifyIPs(ips): print "Error: Invalid ip provided" exit(1) for ip in ips: co = "" if country: country = pygeoip.GeoIP(db, pygeoip.MEMORY_CACHE) if name: co += ":" + country.country_name_by_addr(ip) else: co += ":" + country.country_code_by_addr(ip) if asn: asn = pygeoip.GeoIP(asndb, pygeoip.MEMORY_CACHE) co += ":" + asn.org_by_name(ip) if city: city = pygeoip.GeoIP(citydb, pygeoip.MEMORY_CACHE) r = city.record_by_addr(ip) co += ":%s, %s" % (r["city"], r["region_code"]) print "%s%s" % (ip, co)
matplotlib.pyplot.draw() fig.canvas.manager.window.after(100, drawDataCallback, baseline) #START OF MAIN: if __name__ == '__main__': from optparse import OptionParser p = OptionParser() p.set_usage('spectrometer.py <ROACH_HOSTNAME_or_IP> [options]') p.set_description(__doc__) p.add_argument('-n', '--nchannel', dest='nch', type=int, default=1024, help='The number of frequency channel. Default is 1024.') p.add_argument( '-c', '--coeff', dest='coeff', type=int, default=1000, help='Set the coefficients in quantisation (4bit quantisation scalar).' ) p.add_option( '-l', '--acc_len', dest='acc_len', type='int',
def main(): description = 'Helper script that gives you all the access tokens your account has.' if have_argparse: parser = argparse.ArgumentParser(description=description, formatter_class=argparse.ArgumentDefaultsHelpFormatter) else: parser = OptionParser('%prog [options] user_name', description=description) parser.add_argument('--url', default='https://api.signalfx.com', help='SignalFX endpoint') parser.add_argument('--password', default=None, help='Optional command line password') parser.add_argument('--org', default=None, help='If set, change output to only the auth token of this org') parser.add_argument('--print_user_org', default=False, action='store_true', help='If set, change output to only the auth token of this org') parser.add_argument('--update', default=None, help='If set, will look for a collectd file and auto update to the auth token you select.') parser.add_argument('--error_on_multiple', default=False, action ='store_true', help='If set then an error will be raised if the user is part of multiple organizations ' 'and --org is not specified') if have_argparse: parser.add_argument('user_name', help="User name to log in with") args = parser.parse_args() else: (args, leftover) = parser.parse_args() if not leftover: parser.error("User name to log in with must be specified.") if len(leftover) != 1: parser.error("Only one user name to log in with must be specified.") args.user_name = leftover[0] if args.update is not None: assert os.path.isfile(args.update), "Unable to find the file to update: " + args.update if args.password is None: args.password = getpass.getpass('SignalFX password: '******'content-type': 'application/json'} req = urllib2.Request(args.url + "/session", json.dumps(json_payload), headers) try: resp = urllib2.urlopen(req) except urllib2.HTTPError: sys.stderr.write("Invalid user name/password\n") sys.exit(1) res = resp.read() sf_accessToken = json.loads(res)['sf_accessToken'] sf_userID = json.loads(res)['sf_userID'] # Get the orgs orgs_url = args.url + "/organization?query=sf_organization:%s" % (args.org or '*') headers = {'content-type': 'application/json', 'X-SF-TOKEN': sf_accessToken} req = urllib2.Request(orgs_url, headers=headers) resp = urllib2.urlopen(req) res = resp.read() all_res = json.loads(res) printed_org = False all_auth_tokens = [] for i in all_res['rs']: if args.org is not None: if args.org == i['sf_organization']: all_auth_tokens.append((i['sf_organization'], i['sf_apiAccessToken'])) sys.stdout.write(i['sf_apiAccessToken']) printed_org = True else: if args.print_user_org or not i['sf_organization'].startswith("per-user-org"): all_auth_tokens.append((i['sf_organization'], i['sf_apiAccessToken'])) print ("%40s%40s" % (i['sf_organization'], i['sf_apiAccessToken'])) if args.org is not None and not printed_org: sys.stderr.write("Unable to find the org you set.\n") sys.exit(1) if args.error_on_multiple and len(all_auth_tokens) > 1: sys.stderr.write('Users is part of more than one organization.\n') sys.exit(1) if args.update is None: sys.exit(0) assert len(all_auth_tokens) != 0 if len(all_auth_tokens) > 1: sys.stderr.write( "Multiple auth tokens associated with this account. Add an --org tag for the auth token you want to update to.\n") examples = ["get_all_auth_tokens.py --org=\"%s\"" % s[0] for s in all_auth_tokens] sys.stderr.write("\n".join(examples)+"\n") sys.exit(1) replace_in_file(args.update, 'APIToken "(.*)"', 'APIToken "%s"' % all_auth_tokens[0][1])
def _evaluate_command_line(): global projectname global args usage_descr = 'django Organice setup. Start getting organiced! ' \ 'Your collaboration platform starts here.' help_account = 'Organice account name used as subdomain (default: projectname)' help_domain = 'optional domain name to enforce' help_engine = 'database engine (for profiles: staging, production)' help_database = 'database name (for profiles: staging, production)' help_username = '******' help_password = '******' help_manage = 'use default single manage.py or use multi-settings variant (default: %(default)s)' help_webserver = 'create appropriate web server configuration (default: %(default)s)' if sys.version_info < (2, 7): from optparse import OptionParser # Deprecated since version 2.7 parser = OptionParser(description=usage_descr) parser.add_option('--account', help=help_account) parser.add_option('--domain', help=help_domain) parser.add_option('--engine', choices=['postgresql_psycopg2', 'mysql', 'oracle'], help=help_engine) parser.add_option('--database', help=help_database) parser.add_option('--username', help=help_username) parser.add_option('--password', help=help_password) parser.add_option('--manage', choices=['single', 'multi'], default='single', help=help_manage) parser.add_option('--webserver', choices=['apache', 'lighttp'], default='apache', help=help_webserver) (options, args) = parser.parse_args() if len(args) != 1: parser.error('Please specify a projectname') projectname = args[0] args = options else: from argparse import ArgumentParser # New since version 2.7 parser = ArgumentParser(description=usage_descr) parser.add_argument('projectname', help='name of project to create') parser.add_argument('--account', help=help_account) parser.add_argument('--domain', help=help_domain) parser.add_argument('--engine', choices=['postgresql_psycopg2', 'mysql', 'oracle'], help=help_engine) parser.add_argument('--database', help=help_database) parser.add_argument('--username', help=help_username) parser.add_argument('--password', help=help_password) parser.add_argument('--manage', choices=['single', 'multi'], default='single', help=help_manage) parser.add_argument('--webserver', choices=['apache', 'lighttp'], default='apache', help=help_webserver) args = parser.parse_args() projectname = args.projectname
help = "specify compiler (dmd, ldc, or gdc)" ext_help = "specify we are building a python extension" default = 'dmd' if six.PY2: from optparse import OptionParser parser = OptionParser() parser.add_option("--compiler", dest="compiler", default=default, help=help) parser.add_option("--as-extension", dest="extension", default=False, help=ext_help) (options, args) = parser.parse_args() else: from argparse import ArgumentParser parser = ArgumentParser("""Generates dub configurations for the python instance that invokes this script. """) parser.add_argument("--compiler", default=default, help=help) parser.add_argument("--as-extension", dest="extension", action='store_true', default=False, help=ext_help) parser.add_argument("--arch", help=ext_help) options = parser.parse_args() compiler = new_compiler(options.compiler) compiler.build_exe = not options.extension compiler.arch = options.arch class MockExt: def __init__(self): self.libraries = [] ext = build_ext(Distribution()) libraries = ext.get_libraries(MockExt()) lib_file = compiler._lib_file([])
pass # This is where the magic happens if __name__ == '__main__': # Locale magic locale.setlocale(locale.LC_ALL, '') # Create the parser object if optparse: parser = OptionParser(description=my_description) parser_add_argument = parser.add_option else: parser = argparse.ArgumentParser(description=my_description) parser_add_argument = parser.add_argument parser.add_argument( 'work_dir', type=str, nargs='?', help='Root of the working directory (default: current working directory)') parser_add_argument( '--beta', action='store_true', help='Get the latest beta Chromium source') parser_add_argument( '--dev', action='store_true', help='Get the latest dev Chromium source') parser_add_argument( '--stable', action='store_true', help='Get the latest stable Chromium source') parser_add_argument( '--tests', action='store_true', help='Get the additional data for running tests') parser_add_argument( '--version',
# Return the object if we ever need to pull data out of the thread return httpd # Entry point if __name__ == '__main__': # General parsing for options / arguments and also for providing help details helpDesc = "Client/server program to send/receive data over HTTPS" if sys.version_info.major < 3 and sys.version_info.minor < 2: parser = OptionParser(description = helpDesc) else: parser = ArgumentParser(description = helpDesc) # Allow the user to choose to act only as a client (Default), act as a server, and choose destination IPs and ports # The port is also used for the server if it is being used parser.add_argument("-c", "--client", action = "store_true", default = True, help = "act as a client; connect to a destination server") parser.add_argument("-s", "--server", action = "store_true", default = False, help = "act as a server; listen for incoming HTTPS connections") parser.add_argument("-d", "--dest", dest = "dest", default = "127.0.0.1", help = "input a string variable for the destination IP; default 127.0.0.1") parser.add_argument("-p", "--port", dest = "port", default = 4203, help = "input a int or string variable for the destination port; default 4203") startOpts = parser.parse_args() # Do some basic parsing of the options passed from the user if startOpts.dest is not None: # Check if the destination address is valid try: ipaddress.ip_address(startOpts.dest) except: running = False exit("Please input a proper destination address.")
def main(): order = ['det', 'ran','Set','Lx','Ly','T','b','r', 'full','half', 'Ar', 'Ae'] parser = OptionParser() parser = argparse.ArgumentParser(description='Plot Raw MC Equilibration Data for Scalar Estimators.') parser.add_argument('fileNames', help='Scalar estimator files', nargs='+') parser.add_argument('--estimator','-e', help='A list of estimator names that \ are to be plotted.', type=str) parser.add_argument('--xl', help='Lower x-axis limit', type=float) parser.add_argument('--xh', help='Higher x-axis limit', type=float) parser.add_argument('--yl', help='Lower y-axis limit', type=float) parser.add_argument('--yh', help='Higher y-axis limit', type=float) args = parser.parse_args() fileNames = args.fileNames if not fileNames: print "No files detected" sys.exit() headers = ssexyhelp.ScalarReduce(fileNames[0]).getHeaders() if (not args.estimator) or not(args.estimator in headers): print headers print "Specify a correct estimator" sys.exit() rvariable = headers[0] #rcParams.update(mplrc.aps['params']) colors = ["#66CAAE", "#CF6BDD", "#E27844", "#7ACF57", "#92A1D6", "#E17597", "#C1B546",'b'] figure(1,(8,6)) connect('key_press_event',kevent.press) ax = subplot(111) for i,fileName in enumerate(fileNames): sReduce = ssexyhelp.ScalarReduce(fileName) sReduce.loadData() t,x = sReduce.getrParams() y,dy = sReduce.getAverages(args.estimator) #x = 1.0/np.array(x) print sReduce.paramMap if args.estimator=='SS': x = 1.0/np.array(x) print sReduce.getTupleIdstr(order) if i==0: y1 = unumpy.uarray(y,dy) else: y2 = unumpy.uarray(y,dy) errorbar(x, y, dy,\ marker='s', #mec=colors[i%len(colors)],mfc=colors[i%len(colors)],color=colors[i%len(colors)],\ ls='',capsize=4)#,label=r'$\mathrm{%s}$' %sReduce.getTupleIdstr(order)) #deltay = y2-y1 #y = np.absolute(unumpy.nominal_values(deltay), unumpy.std_devs(deltay)) #ax.plot(x, y, marker='s',mec=colors[i],mfc=colors[i],color=colors[i],\ # ls='',label=r'$\mathrm{%s}$' %sReduce.getTupleIdstr(order)) if args.estimator=='SS': plot([0,1],[2.0/np.pi*0,2.0/np.pi*1], label=r'$\mathrm{Magic \, line}$') xlabel(r'$\mathrm{%s}$' %rvariable) ylabel(r'$\mathrm{%s}$' %args.estimator) #ylabel(r'$\mathrm{|\triangle E}|/\sigma_{\triangle E}$') xmin, xmax = ax.get_xlim() ymin, ymax = ax.get_ylim() if args.xl: xmin = args.xl if args.xh: xmax = args.xh if args.yl: ymin = args.yl if args.yh: ymax = args.yh xlim(xmin,xmax) ylim(ymin,ymax) #legend(loc='best',frameon=False) legend(loc='upper right',frameon=False) tight_layout() show()
#importing necessary libraries import nmap from optparse import OptionParser #Usage Info Usage = 'Usage: python3 <program> --hosts <hosts range> [ 194.456.234.56/24; 194.456.234.56-256 ] --ports <port range> [ 1-100 ]' #Insatntiating parser object parser = OptionParser(Usage) #adding --hosts argument to store hosts range to be scanned parser.add_argument('--hosts', action="store", dest="hosts") #Adding --ports argument to provie Port / Port Range parser.add_argument('--ports', action="store", dest="ports") (options, args) = parser.parse_args() #storing host and port range ip_range = options.hosts port_range = options.ports #Instantiating an object for Scanning Port nm = nmap.PortScanner() #Scanning Port as provided in arguments nm.scan(ip_range, port_range) #Iterating all hosts with up status for host in nm.all_hosts(): state = nm[host].state()
#!/usr/bin/env python from couchdb import Server from optparse import OptionParser import os import sys import re import argparse from time import sleep import couchdb parser = OptionParser() parser = argparse.ArgumentParser(description='Replicate one server to another.') parser.add_argument('--source', type=str, help='Source address') parser.add_argument('--dest', type=str, help='Destination address') args = parser.parse_args() src = Server(args.source) dest = Server(args.dest) count = 0 for dbname in src : db = src[dbname] if (len(dbname) >= 4 and dbname[:4] == "mica" ) or dbname == "_users" : try : newdb = dest[dbname] except couchdb.http.ResourceNotFound, e : dest.create(dbname) newdb = dest[dbname]
def speedtest(): """Run the full speedtest.net test""" description = ( 'Light version of command line interface for testing internet bandwidth using ' 'speedtest.net.\n' '------------------------------------------------------------' '--------------\n' 'https://github.com/Delta-Sigma/speedtest-cli-light') parser = OptionParser(description=description) try: parser.add_argument = parser.add_option except AttributeError: pass parser.add_argument('-q', '--quiet', action='store_true', help='Suppress verbose output, only show basic ' 'information') options = parser.parse_args() if isinstance(options, tuple): args = options[0] else: args = options del options if not args.quiet: print('Retrieving speedtest.net configuration...') config = getConfig() if not args.quiet: print('Retrieving speedtest.net server list...') servers = closestServers(config['client']) if not args.quiet: print('Testing from %(isp)s (%(ip)s)...' % config['client']) if not args.quiet: print('Selecting best server based on ping...') best = getBestServer(servers) if not args.quiet: print('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: ' '%(latency)s ms' % best) else: print('Ping: %(latency)s ms' % best) sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000] urls = [] for size in sizes: for i in range(0, 4): urls.append('%s/random%sx%s.jpg' % (os.path.dirname(best['url']), size, size)) if not args.quiet: print('Testing download speed', end='') dlspeed = downloadSpeed(urls, args.quiet) if not args.quiet: print() print('Download: %0.2f Mbit/s' % ((dlspeed / 1000 / 1000) * 8)) sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)] sizes = [] for size in sizesizes: for i in range(0, 25): sizes.append(size) if not args.quiet: print('Testing upload speed', end='') ulspeed = uploadSpeed(best['url'], sizes, args.quiet) if not args.quiet: print() print('Upload: %0.2f Mbit/s' % ((ulspeed / 1000 / 1000) * 8))
(options, args) = parser.parse_args() input_locs = args output_loc = options.output idf_loc = options.idf num_examples = int(options.examples) example_window = int(options.window) minimum_frequency = int(options.min_freq) stem = args['stem'] else: parser = argparse.ArgumentParser( description='Create a Venncloud html file.') parser.add_argument( '--output', action='store', help='Where the output html file should be written.', default='generated_wordcloud.html') parser.add_argument( '--idf', action='store', help= 'Location of an idf vector to be used, as a JSON file of a python dictionary -- see `create_idf_vector.py` to make one. If this argument is omitted, we will generate the idf vector from the provided documents.', default=None) parser.add_argument( '--examples', action='store', help='Number of examples of each word to store [defaults to 5].', default=5) parser.add_argument( '--window',
def main(): description = 'Helper script that gives you all the access tokens your account has.' if have_argparse: parser = argparse.ArgumentParser( description=description, formatter_class=argparse.ArgumentDefaultsHelpFormatter) else: parser = OptionParser('%prog [options] user_name', description=description) parser.add_argument('--url', default='https://api.signalfx.com/v2', help='SignalFX endpoint') parser.add_argument('--password', default=None, help='Optional command line password') parser.add_argument( '--org', default=None, help='If set, change output to only the auth token of this org') parser.add_argument( '--print_user_org', default=False, action='store_true', help='If set, change output to only the auth token of this org') parser.add_argument( '--update', default=None, help= 'If set, will look for a collectd file and auto update to the auth token you select.' ) parser.add_argument('--print_token_only', default=False, action='store_true', help='If set, only print out tokens') parser.add_argument( '--error_on_multiple', default=False, action='store_true', help= 'If set then an error will be raised if the user is part of multiple organizations ' 'and --org is not specified') if have_argparse: parser.add_argument('user_name', help="User name to log in with") args = parser.parse_args() else: (args, leftover) = parser.parse_args() if not leftover: parser.error("User name to log in with must be specified.") if len(leftover) != 1: parser.error( "Only one user name to log in with must be specified.") args.user_name = leftover[0] if args.update is not None: assert os.path.isfile( args.update), "Unable to find the file to update: " + args.update if args.password is None: args.password = getpass.getpass('SignalFX password: '******'User is part of more than one organization.\n') sys.exit(1) if args.print_token_only: for _, api_token in all_auth_tokens: print(api_token) sys.exit(1) for org_name, api_token in all_auth_tokens: if args.print_user_org or not org_name.startswith("per-user-org"): print("%40s%40s" % (org_name, api_token)) if args.update is None: sys.exit(0) assert len(all_auth_tokens) != 0 if len(all_auth_tokens) > 1: sys.stderr.write( "Multiple auth tokens associated with this account. Add an --org tag for the auth token you want to update to.\n" ) examples = [ "get_all_auth_tokens.py --org=\"%s\"" % s[0] for s in all_auth_tokens ] sys.stderr.write("\n".join(examples) + "\n") sys.exit(1) replace_in_file(args.update, 'APIToken "(.*)"', 'APIToken "%s"' % all_auth_tokens[0][1])
def cmdLineParser(argv=None): """ This function parses the command line parameters and arguments """ if not argv: argv = sys.argv checkSystemEncoding() # Reference: https://stackoverflow.com/a/4012683 (Note: previously used "...sys.getfilesystemencoding() or UNICODE_ENCODING") _ = getUnicode(os.path.basename(argv[0]), encoding=sys.stdin.encoding) usage = "%s%s [options]" % ("%s " % os.path.basename(sys.executable) if not IS_WIN else "", "\"%s\"" % _ if " " in _ else _) parser = ArgumentParser(usage=usage) try: parser.add_argument("--hh", dest="advancedHelp", action="store_true", help="Show advanced help message and exit") parser.add_argument("--version", dest="showVersion", action="store_true", help="Show program's version number and exit") parser.add_argument("-v", dest="verbose", type=int, help="Verbosity level: 0-6 (default %d)" % defaults.verbose) # Target options target = parser.add_argument_group("Target", "At least one of these options has to be provided to define the target(s)") target.add_argument("-d", dest="direct", help="Connection string for direct database connection") target.add_argument("-u", "--url", dest="url", help="Target URL (e.g. \"http://www.site.com/vuln.php?id=1\")") target.add_argument("-l", dest="logFile", help="Parse target(s) from Burp or WebScarab proxy log file") target.add_argument("-m", dest="bulkFile", help="Scan multiple targets given in a textual file ") target.add_argument("-r", dest="requestFile", help="Load HTTP request from a file") target.add_argument("-g", dest="googleDork", help="Process Google dork results as target URLs") target.add_argument("-c", dest="configFile", help="Load options from a configuration INI file") # Request options request = parser.add_argument_group("Request", "These options can be used to specify how to connect to the target URL") request.add_argument("-A", "--user-agent", dest="agent", help="HTTP User-Agent header value") request.add_argument("-H", "--header", dest="header", help="Extra header (e.g. \"X-Forwarded-For: 127.0.0.1\")") request.add_argument("--method", dest="method", help="Force usage of given HTTP method (e.g. PUT)") request.add_argument("--data", dest="data", help="Data string to be sent through POST (e.g. \"id=1\")") request.add_argument("--param-del", dest="paramDel", help="Character used for splitting parameter values (e.g. &)") request.add_argument("--cookie", dest="cookie", help="HTTP Cookie header value (e.g. \"PHPSESSID=a8d127e..\")") request.add_argument("--cookie-del", dest="cookieDel", help="Character used for splitting cookie values (e.g. ;)") request.add_argument("--load-cookies", dest="loadCookies", help="File containing cookies in Netscape/wget format") request.add_argument("--drop-set-cookie", dest="dropSetCookie", action="store_true", help="Ignore Set-Cookie header from response") request.add_argument("--mobile", dest="mobile", action="store_true", help="Imitate smartphone through HTTP User-Agent header") request.add_argument("--random-agent", dest="randomAgent", action="store_true", help="Use randomly selected HTTP User-Agent header value") request.add_argument("--host", dest="host", help="HTTP Host header value") request.add_argument("--referer", dest="referer", help="HTTP Referer header value") request.add_argument("--headers", dest="headers", help="Extra headers (e.g. \"Accept-Language: fr\\nETag: 123\")") request.add_argument("--auth-type", dest="authType", help="HTTP authentication type (Basic, Digest, NTLM or PKI)") request.add_argument("--auth-cred", dest="authCred", help="HTTP authentication credentials (name:password)") request.add_argument("--auth-file", dest="authFile", help="HTTP authentication PEM cert/private key file") request.add_argument("--ignore-code", dest="ignoreCode", help="Ignore (problematic) HTTP error code (e.g. 401)") request.add_argument("--ignore-proxy", dest="ignoreProxy", action="store_true", help="Ignore system default proxy settings") request.add_argument("--ignore-redirects", dest="ignoreRedirects", action="store_true", help="Ignore redirection attempts") request.add_argument("--ignore-timeouts", dest="ignoreTimeouts", action="store_true", help="Ignore connection timeouts") request.add_argument("--proxy", dest="proxy", help="Use a proxy to connect to the target URL") request.add_argument("--proxy-cred", dest="proxyCred", help="Proxy authentication credentials (name:password)") request.add_argument("--proxy-file", dest="proxyFile", help="Load proxy list from a file") request.add_argument("--tor", dest="tor", action="store_true", help="Use Tor anonymity network") request.add_argument("--tor-port", dest="torPort", help="Set Tor proxy port other than default") request.add_argument("--tor-type", dest="torType", help="Set Tor proxy type (HTTP, SOCKS4 or SOCKS5 (default))") request.add_argument("--check-tor", dest="checkTor", action="store_true", help="Check to see if Tor is used properly") request.add_argument("--delay", dest="delay", type=float, help="Delay in seconds between each HTTP request") request.add_argument("--timeout", dest="timeout", type=float, help="Seconds to wait before timeout connection (default %d)" % defaults.timeout) request.add_argument("--retries", dest="retries", type=int, help="Retries when the connection timeouts (default %d)" % defaults.retries) request.add_argument("--randomize", dest="rParam", help="Randomly change value for given parameter(s)") request.add_argument("--safe-url", dest="safeUrl", help="URL address to visit frequently during testing") request.add_argument("--safe-post", dest="safePost", help="POST data to send to a safe URL") request.add_argument("--safe-req", dest="safeReqFile", help="Load safe HTTP request from a file") request.add_argument("--safe-freq", dest="safeFreq", type=int, help="Test requests between two visits to a given safe URL") request.add_argument("--skip-urlencode", dest="skipUrlEncode", action="store_true", help="Skip URL encoding of payload data") request.add_argument("--csrf-token", dest="csrfToken", help="Parameter used to hold anti-CSRF token") request.add_argument("--csrf-url", dest="csrfUrl", help="URL address to visit for extraction of anti-CSRF token") request.add_argument("--csrf-method", dest="csrfMethod", help="HTTP method to use during anti-CSRF token page visit") request.add_argument("--force-ssl", dest="forceSSL", action="store_true", help="Force usage of SSL/HTTPS") request.add_argument("--chunked", dest="chunked", action="store_true", help="Use HTTP chunked transfer encoded (POST) requests") request.add_argument("--hpp", dest="hpp", action="store_true", help="Use HTTP parameter pollution method") request.add_argument("--eval", dest="evalCode", help="Evaluate provided Python code before the request (e.g. \"import hashlib;id2=hashlib.md5(id).hexdigest()\")") # Optimization options optimization = parser.add_argument_group("Optimization", "These options can be used to optimize the performance of sqlmap") optimization.add_argument("-o", dest="optimize", action="store_true", help="Turn on all optimization switches") optimization.add_argument("--predict-output", dest="predictOutput", action="store_true", help="Predict common queries output") optimization.add_argument("--keep-alive", dest="keepAlive", action="store_true", help="Use persistent HTTP(s) connections") optimization.add_argument("--null-connection", dest="nullConnection", action="store_true", help="Retrieve page length without actual HTTP response body") optimization.add_argument("--threads", dest="threads", type=int, help="Max number of concurrent HTTP(s) requests (default %d)" % defaults.threads) # Injection options injection = parser.add_argument_group("Injection", "These options can be used to specify which parameters to test for, provide custom injection payloads and optional tampering scripts") injection.add_argument("-p", dest="testParameter", help="Testable parameter(s)") injection.add_argument("--skip", dest="skip", help="Skip testing for given parameter(s)") injection.add_argument("--skip-static", dest="skipStatic", action="store_true", help="Skip testing parameters that not appear to be dynamic") injection.add_argument("--param-exclude", dest="paramExclude", help="Regexp to exclude parameters from testing (e.g. \"ses\")") injection.add_argument("--param-filter", dest="paramFilter", help="Select testable parameter(s) by place (e.g. \"POST\")") injection.add_argument("--dbms", dest="dbms", help="Force back-end DBMS to provided value") injection.add_argument("--dbms-cred", dest="dbmsCred", help="DBMS authentication credentials (user:password)") injection.add_argument("--os", dest="os", help="Force back-end DBMS operating system to provided value") injection.add_argument("--invalid-bignum", dest="invalidBignum", action="store_true", help="Use big numbers for invalidating values") injection.add_argument("--invalid-logical", dest="invalidLogical", action="store_true", help="Use logical operations for invalidating values") injection.add_argument("--invalid-string", dest="invalidString", action="store_true", help="Use random strings for invalidating values") injection.add_argument("--no-cast", dest="noCast", action="store_true", help="Turn off payload casting mechanism") injection.add_argument("--no-escape", dest="noEscape", action="store_true", help="Turn off string escaping mechanism") injection.add_argument("--prefix", dest="prefix", help="Injection payload prefix string") injection.add_argument("--suffix", dest="suffix", help="Injection payload suffix string") injection.add_argument("--tamper", dest="tamper", help="Use given script(s) for tampering injection data") # Detection options detection = parser.add_argument_group("Detection", "These options can be used to customize the detection phase") detection.add_argument("--level", dest="level", type=int, help="Level of tests to perform (1-5, default %d)" % defaults.level) detection.add_argument("--risk", dest="risk", type=int, help="Risk of tests to perform (1-3, default %d)" % defaults.risk) detection.add_argument("--string", dest="string", help="String to match when query is evaluated to True") detection.add_argument("--not-string", dest="notString", help="String to match when query is evaluated to False") detection.add_argument("--regexp", dest="regexp", help="Regexp to match when query is evaluated to True") detection.add_argument("--code", dest="code", type=int, help="HTTP code to match when query is evaluated to True") detection.add_argument("--smart", dest="smart", action="store_true", help="Perform thorough tests only if positive heuristic(s)") detection.add_argument("--text-only", dest="textOnly", action="store_true", help="Compare pages based only on the textual content") detection.add_argument("--titles", dest="titles", action="store_true", help="Compare pages based only on their titles") # Techniques options techniques = parser.add_argument_group("Techniques", "These options can be used to tweak testing of specific SQL injection techniques") techniques.add_argument("--technique", dest="technique", help="SQL injection techniques to use (default \"%s\")" % defaults.technique) techniques.add_argument("--time-sec", dest="timeSec", type=int, help="Seconds to delay the DBMS response (default %d)" % defaults.timeSec) techniques.add_argument("--union-cols", dest="uCols", help="Range of columns to test for UNION query SQL injection") techniques.add_argument("--union-char", dest="uChar", help="Character to use for bruteforcing number of columns") techniques.add_argument("--union-from", dest="uFrom", help="Table to use in FROM part of UNION query SQL injection") techniques.add_argument("--dns-domain", dest="dnsDomain", help="Domain name used for DNS exfiltration attack") techniques.add_argument("--second-url", dest="secondUrl", help="Resulting page URL searched for second-order response") techniques.add_argument("--second-req", dest="secondReq", help="Load second-order HTTP request from file") # Fingerprint options fingerprint = parser.add_argument_group("Fingerprint") fingerprint.add_argument("-f", "--fingerprint", dest="extensiveFp", action="store_true", help="Perform an extensive DBMS version fingerprint") # Enumeration options enumeration = parser.add_argument_group("Enumeration", "These options can be used to enumerate the back-end database management system information, structure and data contained in the tables") enumeration.add_argument("-a", "--all", dest="getAll", action="store_true", help="Retrieve everything") enumeration.add_argument("-b", "--banner", dest="getBanner", action="store_true", help="Retrieve DBMS banner") enumeration.add_argument("--current-user", dest="getCurrentUser", action="store_true", help="Retrieve DBMS current user") enumeration.add_argument("--current-db", dest="getCurrentDb", action="store_true", help="Retrieve DBMS current database") enumeration.add_argument("--hostname", dest="getHostname", action="store_true", help="Retrieve DBMS server hostname") enumeration.add_argument("--is-dba", dest="isDba", action="store_true", help="Detect if the DBMS current user is DBA") enumeration.add_argument("--users", dest="getUsers", action="store_true", help="Enumerate DBMS users") enumeration.add_argument("--passwords", dest="getPasswordHashes", action="store_true", help="Enumerate DBMS users password hashes") enumeration.add_argument("--privileges", dest="getPrivileges", action="store_true", help="Enumerate DBMS users privileges") enumeration.add_argument("--roles", dest="getRoles", action="store_true", help="Enumerate DBMS users roles") enumeration.add_argument("--dbs", dest="getDbs", action="store_true", help="Enumerate DBMS databases") enumeration.add_argument("--tables", dest="getTables", action="store_true", help="Enumerate DBMS database tables") enumeration.add_argument("--columns", dest="getColumns", action="store_true", help="Enumerate DBMS database table columns") enumeration.add_argument("--schema", dest="getSchema", action="store_true", help="Enumerate DBMS schema") enumeration.add_argument("--count", dest="getCount", action="store_true", help="Retrieve number of entries for table(s)") enumeration.add_argument("--dump", dest="dumpTable", action="store_true", help="Dump DBMS database table entries") enumeration.add_argument("--dump-all", dest="dumpAll", action="store_true", help="Dump all DBMS databases tables entries") enumeration.add_argument("--search", dest="search", action="store_true", help="Search column(s), table(s) and/or database name(s)") enumeration.add_argument("--comments", dest="getComments", action="store_true", help="Check for DBMS comments during enumeration") enumeration.add_argument("--statements", dest="getStatements", action="store_true", help="Retrieve SQL statements being run on DBMS") enumeration.add_argument("-D", dest="db", help="DBMS database to enumerate") enumeration.add_argument("-T", dest="tbl", help="DBMS database table(s) to enumerate") enumeration.add_argument("-C", dest="col", help="DBMS database table column(s) to enumerate") enumeration.add_argument("-X", dest="exclude", help="DBMS database identifier(s) to not enumerate") enumeration.add_argument("-U", dest="user", help="DBMS user to enumerate") enumeration.add_argument("--exclude-sysdbs", dest="excludeSysDbs", action="store_true", help="Exclude DBMS system databases when enumerating tables") enumeration.add_argument("--pivot-column", dest="pivotColumn", help="Pivot column name") enumeration.add_argument("--where", dest="dumpWhere", help="Use WHERE condition while table dumping") enumeration.add_argument("--start", dest="limitStart", type=int, help="First dump table entry to retrieve") enumeration.add_argument("--stop", dest="limitStop", type=int, help="Last dump table entry to retrieve") enumeration.add_argument("--first", dest="firstChar", type=int, help="First query output word character to retrieve") enumeration.add_argument("--last", dest="lastChar", type=int, help="Last query output word character to retrieve") enumeration.add_argument("--sql-query", dest="sqlQuery", help="SQL statement to be executed") enumeration.add_argument("--sql-shell", dest="sqlShell", action="store_true", help="Prompt for an interactive SQL shell") enumeration.add_argument("--sql-file", dest="sqlFile", help="Execute SQL statements from given file(s)") # Brute force options brute = parser.add_argument_group("Brute force", "These options can be used to run brute force checks") brute.add_argument("--common-tables", dest="commonTables", action="store_true", help="Check existence of common tables") brute.add_argument("--common-columns", dest="commonColumns", action="store_true", help="Check existence of common columns") brute.add_argument("--common-files", dest="commonFiles", action="store_true", help="Check existence of common files") # User-defined function options udf = parser.add_argument_group("User-defined function injection", "These options can be used to create custom user-defined functions") udf.add_argument("--udf-inject", dest="udfInject", action="store_true", help="Inject custom user-defined functions") udf.add_argument("--shared-lib", dest="shLib", help="Local path of the shared library") # File system options filesystem = parser.add_argument_group("File system access", "These options can be used to access the back-end database management system underlying file system") filesystem.add_argument("--file-read", dest="fileRead", help="Read a file from the back-end DBMS file system") filesystem.add_argument("--file-write", dest="fileWrite", help="Write a local file on the back-end DBMS file system") filesystem.add_argument("--file-dest", dest="fileDest", help="Back-end DBMS absolute filepath to write to") # Takeover options takeover = parser.add_argument_group("Operating system access", "These options can be used to access the back-end database management system underlying operating system") takeover.add_argument("--os-cmd", dest="osCmd", help="Execute an operating system command") takeover.add_argument("--os-shell", dest="osShell", action="store_true", help="Prompt for an interactive operating system shell") takeover.add_argument("--os-pwn", dest="osPwn", action="store_true", help="Prompt for an OOB shell, Meterpreter or VNC") takeover.add_argument("--os-smbrelay", dest="osSmb", action="store_true", help="One click prompt for an OOB shell, Meterpreter or VNC") takeover.add_argument("--os-bof", dest="osBof", action="store_true", help="Stored procedure buffer overflow " "exploitation") takeover.add_argument("--priv-esc", dest="privEsc", action="store_true", help="Database process user privilege escalation") takeover.add_argument("--msf-path", dest="msfPath", help="Local path where Metasploit Framework is installed") takeover.add_argument("--tmp-path", dest="tmpPath", help="Remote absolute path of temporary files directory") # Windows registry options windows = parser.add_argument_group("Windows registry access", "These options can be used to access the back-end database management system Windows registry") windows.add_argument("--reg-read", dest="regRead", action="store_true", help="Read a Windows registry key value") windows.add_argument("--reg-add", dest="regAdd", action="store_true", help="Write a Windows registry key value data") windows.add_argument("--reg-del", dest="regDel", action="store_true", help="Delete a Windows registry key value") windows.add_argument("--reg-key", dest="regKey", help="Windows registry key") windows.add_argument("--reg-value", dest="regVal", help="Windows registry key value") windows.add_argument("--reg-data", dest="regData", help="Windows registry key value data") windows.add_argument("--reg-type", dest="regType", help="Windows registry key value type") # General options general = parser.add_argument_group("General", "These options can be used to set some general working parameters") general.add_argument("-s", dest="sessionFile", help="Load session from a stored (.sqlite) file") general.add_argument("-t", dest="trafficFile", help="Log all HTTP traffic into a textual file") general.add_argument("--answers", dest="answers", help="Set predefined answers (e.g. \"quit=N,follow=N\")") general.add_argument("--batch", dest="batch", action="store_true", help="Never ask for user input, use the default behavior") general.add_argument("--binary-fields", dest="binaryFields", help="Result fields having binary values (e.g. \"digest\")") general.add_argument("--check-internet", dest="checkInternet", action="store_true", help="Check Internet connection before assessing the target") general.add_argument("--cleanup", dest="cleanup", action="store_true", help="Clean up the DBMS from sqlmap specific UDF and tables") general.add_argument("--crawl", dest="crawlDepth", type=int, help="Crawl the website starting from the target URL") general.add_argument("--crawl-exclude", dest="crawlExclude", help="Regexp to exclude pages from crawling (e.g. \"logout\")") general.add_argument("--csv-del", dest="csvDel", help="Delimiting character used in CSV output (default \"%s\")" % defaults.csvDel) general.add_argument("--charset", dest="charset", help="Blind SQL injection charset (e.g. \"0123456789abcdef\")") general.add_argument("--dump-format", dest="dumpFormat", help="Format of dumped data (CSV (default), HTML or SQLITE)") general.add_argument("--encoding", dest="encoding", help="Character encoding used for data retrieval (e.g. GBK)") general.add_argument("--eta", dest="eta", action="store_true", help="Display for each output the estimated time of arrival") general.add_argument("--flush-session", dest="flushSession", action="store_true", help="Flush session files for current target") general.add_argument("--forms", dest="forms", action="store_true", help="Parse and test forms on target URL") general.add_argument("--fresh-queries", dest="freshQueries", action="store_true", help="Ignore query results stored in session file") general.add_argument("--gpage", dest="googlePage", type=int, help="Use Google dork results from specified page number") general.add_argument("--har", dest="harFile", help="Log all HTTP traffic into a HAR file") general.add_argument("--hex", dest="hexConvert", action="store_true", help="Use hex conversion during data retrieval") general.add_argument("--output-dir", dest="outputDir", action="store", help="Custom output directory path") general.add_argument("--parse-errors", dest="parseErrors", action="store_true", help="Parse and display DBMS error messages from responses") general.add_argument("--preprocess", dest="preprocess", help="Use given script(s) for preprocessing of response data") general.add_argument("--repair", dest="repair", action="store_true", help="Redump entries having unknown character marker (%s)" % INFERENCE_UNKNOWN_CHAR) general.add_argument("--save", dest="saveConfig", help="Save options to a configuration INI file") general.add_argument("--scope", dest="scope", help="Regexp to filter targets from provided proxy log") general.add_argument("--skip-waf", dest="skipWaf", action="store_true", help="Skip heuristic detection of WAF/IPS protection") general.add_argument("--table-prefix", dest="tablePrefix", help="Prefix used for temporary tables (default: \"%s\")" % defaults.tablePrefix) general.add_argument("--test-filter", dest="testFilter", help="Select tests by payloads and/or titles (e.g. ROW)") general.add_argument("--test-skip", dest="testSkip", help="Skip tests by payloads and/or titles (e.g. BENCHMARK)") general.add_argument("--web-root", dest="webRoot", help="Web server document root directory (e.g. \"/var/www\")") # Miscellaneous options miscellaneous = parser.add_argument_group("Miscellaneous", "These options do not fit into any other category") miscellaneous.add_argument("-z", dest="mnemonics", help="Use short mnemonics (e.g. \"flu,bat,ban,tec=EU\")") miscellaneous.add_argument("--alert", dest="alert", help="Run host OS command(s) when SQL injection is found") miscellaneous.add_argument("--beep", dest="beep", action="store_true", help="Beep on question and/or when SQL injection is found") miscellaneous.add_argument("--dependencies", dest="dependencies", action="store_true", help="Check for missing (optional) sqlmap dependencies") miscellaneous.add_argument("--disable-coloring", dest="disableColoring", action="store_true", help="Disable console output coloring") miscellaneous.add_argument("--list-tampers", dest="listTampers", action="store_true", help="Display list of available tamper scripts") miscellaneous.add_argument("--offline", dest="offline", action="store_true", help="Work in offline mode (only use session data)") miscellaneous.add_argument("--purge", dest="purge", action="store_true", help="Safely remove all content from sqlmap data directory") miscellaneous.add_argument("--results-file", dest="resultsFile", help="Location of CSV results file in multiple targets mode") miscellaneous.add_argument("--sqlmap-shell", dest="sqlmapShell", action="store_true", help="Prompt for an interactive sqlmap shell") miscellaneous.add_argument("--tmp-dir", dest="tmpDir", help="Local directory for storing temporary files") miscellaneous.add_argument("--unstable", dest="unstable", action="store_true", help="Adjust options for unstable connections") miscellaneous.add_argument("--update", dest="updateAll", action="store_true", help="Update sqlmap") miscellaneous.add_argument("--wizard", dest="wizard", action="store_true", help="Simple wizard interface for beginner users") # Hidden and/or experimental options parser.add_argument("--base64", dest="base64Parameter", help=SUPPRESS) # "Parameter(s) containing Base64 encoded values" parser.add_argument("--crack", dest="hashFile", help=SUPPRESS) # "Load and crack hashes from a file (standalone)" parser.add_argument("--dummy", dest="dummy", action="store_true", help=SUPPRESS) parser.add_argument("--murphy-rate", dest="murphyRate", type=int, help=SUPPRESS) parser.add_argument("--debug", dest="debug", action="store_true", help=SUPPRESS) parser.add_argument("--disable-precon", dest="disablePrecon", action="store_true", help=SUPPRESS) parser.add_argument("--disable-stats", dest="disableStats", action="store_true", help=SUPPRESS) parser.add_argument("--profile", dest="profile", action="store_true", help=SUPPRESS) parser.add_argument("--force-dbms", dest="forceDbms", help=SUPPRESS) parser.add_argument("--force-dns", dest="forceDns", action="store_true", help=SUPPRESS) parser.add_argument("--force-partial", dest="forcePartial", action="store_true", help=SUPPRESS) parser.add_argument("--force-pivoting", dest="forcePivoting", action="store_true", help=SUPPRESS) parser.add_argument("--gui", dest="gui", action="store_true", help=SUPPRESS) parser.add_argument("--smoke-test", dest="smokeTest", action="store_true", help=SUPPRESS) parser.add_argument("--live-test", dest="liveTest", action="store_true", help=SUPPRESS) parser.add_argument("--vuln-test", dest="vulnTest", action="store_true", help=SUPPRESS) parser.add_argument("--stop-fail", dest="stopFail", action="store_true", help=SUPPRESS) parser.add_argument("--run-case", dest="runCase", help=SUPPRESS) # API options parser.add_argument("--api", dest="api", action="store_true", help=SUPPRESS) parser.add_argument("--taskid", dest="taskid", help=SUPPRESS) parser.add_argument("--database", dest="database", help=SUPPRESS) # Dirty hack to display longer options without breaking into two lines if hasattr(parser, "formatter"): def _(self, *args): retVal = parser.formatter._format_option_strings(*args) if len(retVal) > MAX_HELP_OPTION_LENGTH: retVal = ("%%.%ds.." % (MAX_HELP_OPTION_LENGTH - parser.formatter.indent_increment)) % retVal return retVal parser.formatter._format_option_strings = parser.formatter.format_option_strings parser.formatter.format_option_strings = type(parser.formatter.format_option_strings)(_, parser) else: def _format_action_invocation(self, action): retVal = self.__format_action_invocation(action) if len(retVal) > MAX_HELP_OPTION_LENGTH: retVal = ("%%.%ds.." % (MAX_HELP_OPTION_LENGTH - self._indent_increment)) % retVal return retVal parser.formatter_class.__format_action_invocation = parser.formatter_class._format_action_invocation parser.formatter_class._format_action_invocation = _format_action_invocation # Dirty hack for making a short option '-hh' if hasattr(parser, "get_option"): option = parser.get_option("--hh") option._short_opts = ["-hh"] option._long_opts = [] else: for action in get_actions(parser): if action.option_strings == ["--hh"]: action.option_strings = ["-hh"] break # Dirty hack for inherent help message of switch '-h' if hasattr(parser, "get_option"): option = parser.get_option("-h") option.help = option.help.capitalize().replace("this help", "basic help") else: for action in get_actions(parser): if action.option_strings == ["-h", "--help"]: action.help = action.help.capitalize().replace("this help", "basic help") break _ = [] advancedHelp = True extraHeaders = [] tamperIndex = None # Reference: https://stackoverflow.com/a/4012683 (Note: previously used "...sys.getfilesystemencoding() or UNICODE_ENCODING") for arg in argv: _.append(getUnicode(arg, encoding=sys.stdin.encoding)) argv = _ checkOldOptions(argv) if "--gui" in argv: runGui(parser) elif "--sqlmap-shell" in argv: _createHomeDirectories() parser.usage = "" cmdLineOptions.sqlmapShell = True commands = set(("x", "q", "exit", "quit", "clear")) commands.update(get_all_options(parser)) autoCompletion(AUTOCOMPLETE_TYPE.SQLMAP, commands=commands) while True: command = None try: # Note: in Python2 command should not be converted to Unicode before passing to shlex (Reference: https://bugs.python.org/issue1170) command = _input("sqlmap-shell> ").strip() except (KeyboardInterrupt, EOFError): print() raise SqlmapShellQuitException if not command: continue elif command.lower() == "clear": clearHistory() dataToStdout("[i] history cleared\n") saveHistory(AUTOCOMPLETE_TYPE.SQLMAP) elif command.lower() in ("x", "q", "exit", "quit"): raise SqlmapShellQuitException elif command[0] != '-': dataToStdout("[!] invalid option(s) provided\n") dataToStdout("[i] proper example: '-u http://www.site.com/vuln.php?id=1 --banner'\n") else: saveHistory(AUTOCOMPLETE_TYPE.SQLMAP) loadHistory(AUTOCOMPLETE_TYPE.SQLMAP) break try: for arg in shlex.split(command): argv.append(getUnicode(arg, encoding=sys.stdin.encoding)) except ValueError as ex: raise SqlmapSyntaxException("something went wrong during command line parsing ('%s')" % getSafeExString(ex)) for i in xrange(len(argv)): longOptions = set(re.findall(r"\-\-([^= ]+?)=", parser.format_help())) if argv[i] == "-hh": argv[i] = "-h" elif i == 1 and re.search(r"\A(http|www\.|\w[\w.-]+\.\w{2,})", argv[i]) is not None: argv[i] = "--url=%s" % argv[i] elif len(argv[i]) > 1 and all(ord(_) in xrange(0x2018, 0x2020) for _ in ((argv[i].split('=', 1)[-1].strip() or ' ')[0], argv[i][-1])): dataToStdout("[!] copy-pasting illegal (non-console) quote characters from Internet is, well, illegal (%s)\n" % argv[i]) raise SystemExit elif len(argv[i]) > 1 and u"\uff0c" in argv[i].split('=', 1)[-1]: dataToStdout("[!] copy-pasting illegal (non-console) comma characters from Internet is, well, illegal (%s)\n" % argv[i]) raise SystemExit elif re.search(r"\A-\w=.+", argv[i]): dataToStdout("[!] potentially miswritten (illegal '=') short option detected ('%s')\n" % argv[i]) raise SystemExit elif argv[i] in DEPRECATED_OPTIONS: argv[i] = "" elif argv[i].startswith("--tamper"): if tamperIndex is None: tamperIndex = i if '=' in argv[i] else (i + 1 if i + 1 < len(argv) and not argv[i + 1].startswith('-') else None) else: argv[tamperIndex] = "%s,%s" % (argv[tamperIndex], argv[i].split('=')[1] if '=' in argv[i] else (argv[i + 1] if i + 1 < len(argv) and not argv[i + 1].startswith('-') else "")) argv[i] = "" elif argv[i] == "-H": if i + 1 < len(argv): extraHeaders.append(argv[i + 1]) elif argv[i] == "-r": for j in xrange(i + 2, len(argv)): value = argv[j] if os.path.isfile(value): argv[i + 1] += ",%s" % value argv[j] = '' else: break elif re.match(r"\A\d+!\Z", argv[i]) and argv[max(0, i - 1)] == "--threads" or re.match(r"\A--threads.+\d+!\Z", argv[i]): argv[i] = argv[i][:-1] conf.skipThreadCheck = True elif argv[i] == "--version": print(VERSION_STRING.split('/')[-1]) raise SystemExit elif argv[i] in ("-h", "--help"): advancedHelp = False for group in get_groups(parser)[:]: found = False for option in get_actions(group): if option.dest not in BASIC_HELP_ITEMS: option.help = SUPPRESS else: found = True if not found: get_groups(parser).remove(group) elif '=' in argv[i] and not argv[i].startswith('-') and argv[i].split('=')[0] in longOptions and re.search(r"\A-\w\Z", argv[i - 1]) is None: dataToStdout("[!] detected usage of long-option without a starting hyphen ('%s')\n" % argv[i]) raise SystemExit for verbosity in (_ for _ in argv if re.search(r"\A\-v+\Z", _)): try: if argv.index(verbosity) == len(argv) - 1 or not argv[argv.index(verbosity) + 1].isdigit(): conf.verbose = verbosity.count('v') + 1 del argv[argv.index(verbosity)] except (IndexError, ValueError): pass try: (args, _) = parser.parse_known_args(argv) if hasattr(parser, "parse_known_args") else parser.parse_args(argv) except UnicodeEncodeError as ex: dataToStdout("\n[!] %s\n" % getUnicode(ex.object.encode("unicode-escape"))) raise SystemExit except SystemExit: if "-h" in argv and not advancedHelp: dataToStdout("\n[!] to see full list of options run with '-hh'\n") raise if extraHeaders: if not args.headers: args.headers = "" delimiter = "\\n" if "\\n" in args.headers else "\n" args.headers += delimiter + delimiter.join(extraHeaders) # Expand given mnemonic options (e.g. -z "ign,flu,bat") for i in xrange(len(argv) - 1): if argv[i] == "-z": expandMnemonics(argv[i + 1], parser, args) if args.dummy: args.url = args.url or DUMMY_URL if not any((args.direct, args.url, args.logFile, args.bulkFile, args.googleDork, args.configFile, args.requestFile, args.updateAll, args.smokeTest, args.vulnTest, args.liveTest, args.wizard, args.dependencies, args.purge, args.listTampers, args.hashFile)): errMsg = "missing a mandatory option (-d, -u, -l, -m, -r, -g, -c, --list-tampers, --wizard, --update, --purge or --dependencies). " errMsg += "Use -h for basic and -hh for advanced help\n" parser.error(errMsg) return args except (ArgumentError, TypeError) as ex: parser.error(ex) except SystemExit: # Protection against Windows dummy double clicking if IS_WIN: dataToStdout("\nPress Enter to continue...") _input() raise debugMsg = "parsing command line" logger.debug(debugMsg)
import sys ver_info = sys.version_info # parse commandlines if ver_info[0] < 3 and ver_info[1] < 7: from optparse import OptionParser parser = OptionParser() parser.add_option("-f", "--file", dest="filename", help="input log file", metavar="LOG_FILE") # parser.add_option("-d", "--directory", dest="dirname", help="input directory with log files", metavar="LOG_DIR") parser.add_option("-t", "--dbtype", dest="dbtype", help="database type", default="mongodb", metavar="DB_TYPE") (options, args) = parser.parse_args(); else: import argparse parser = argparse.ArgumentParser(description="Log to database ingester") parser.add_argument("-f, --file", dest="filename", help="input log file", metavar="LOG_FILE") # parser.add_argument("-d, --directory", dest="dirname", help="input directory with log files", metavar="LOG_DIR") parser.add_argument("-t, --dbtype", dest="dbtype", help="database type", default="mongodb", metavar="DB_TYPE") options = parser.parse_args() print "file {0} ".format(options.filename) # print "dirname {0} ".format(options.dirname) print "dbtype {0}".format(options.dbtype) if options.dbtype == "mongodb": from DBDriver.MongoDBDriver import MongoDBDriver dbingester = MongoDBDriver(); elif options.dbtype == "cassandra": from DBDriver.CassandraDBDriver import CassandraDBDriver
def create_parser(self, prog_name, subcommand): """ Create and return the ``ArgumentParser`` which will be used to parse the arguments to this command. """ if not self.use_argparse: # Backwards compatibility: use deprecated optparse module warnings.warn("OptionParser usage for Django management commands " "is deprecated, use ArgumentParser instead", RemovedInDjango20Warning) parser = OptionParser(prog=prog_name, usage=self.usage(subcommand), version=self.get_version()) parser.add_option('-v', '--verbosity', action='store', dest='verbosity', default='1', type='choice', choices=['0', '1', '2', '3'], help='Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output') parser.add_option('--settings', help=( 'The Python path to a settings module, e.g. ' '"myproject.settings.main". If this isn\'t provided, the ' 'DJANGO_SETTINGS_MODULE environment variable will be used.' ), ) parser.add_option('--pythonpath', help='A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".'), parser.add_option('--traceback', action='store_true', help='Raise on CommandError exceptions') parser.add_option('--no-color', action='store_true', dest='no_color', default=False, help="Don't colorize the command output.") for opt in self.option_list: parser.add_option(opt) else: parser = CommandParser(self, prog="%s %s" % (os.path.basename(prog_name), subcommand), description=self.help or None) parser.add_argument('--version', action='version', version=self.get_version()) parser.add_argument('-v', '--verbosity', action='store', dest='verbosity', default='1', type=int, choices=[0, 1, 2, 3], help='Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output') parser.add_argument('--settings', help=( 'The Python path to a settings module, e.g. ' '"myproject.settings.main". If this isn\'t provided, the ' 'DJANGO_SETTINGS_MODULE environment variable will be used.' ), ) parser.add_argument('--pythonpath', help='A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".') parser.add_argument('--traceback', action='store_true', help='Raise on CommandError exceptions') parser.add_argument('--no-color', action='store_true', dest='no_color', default=False, help="Don't colorize the command output.") if self.args: # Keep compatibility and always accept positional arguments, like optparse when args is set parser.add_argument('args', nargs='*') self.add_arguments(parser) return parser
def main(): levels = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL } usage = "usage:runtest.py [-l debug_level] " parser = OptionParser(usage=usage) parser.add_option( "-l", "--log", dest="log", help="logging level, i.e. debug, info, warning, error, critical") (options, _) = parser.parse_args() if options.log: level_name = options.log level = levels.get(level_name, logging.NOTSET) logging.basicConfig(level=level) # test server ts = None # test suit basicsuite = unittest.TestSuite() profilesuite = unittest.TestSuite() settingssuite1 = unittest.TestSuite() serversuite1 = unittest.TestSuite() settingssuite1b = unittest.TestSuite() serversuite1b = unittest.TestSuite() settingssuite2 = unittest.TestSuite() serversuite2 = unittest.TestSuite() settingssuite2b = unittest.TestSuite() serversuite2b = unittest.TestSuite() basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(SelectionTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(Selector2Test)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(SelectorTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(TangoDSItemTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(CheckerItemTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(DSItemTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(ExDSItemTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(ExDSDictTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(DescriberTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(UtilsTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(StreamSetTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(ConverterTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(ConverterXtoYTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(Converter1to2Test)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(Converter2to1Test)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(Converter3to2Test)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(Converter2to3Test)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(CheckerThreadTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(MacroServerPoolsTest)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(MacroServerPools2Test)) basicsuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(DynamicComponentTest)) profilesuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(ProfileManagerTest)) profilesuite.addTests( unittest.defaultTestLoader.loadTestsFromModule(ProfileManager2Test)) settingssuite1.addTests( unittest.defaultTestLoader.loadTestsFromModule(BasicSettingsTest)) settingssuite1b.addTests( unittest.defaultTestLoader.loadTestsFromModule(BasicSettings2Test)) settingssuite2.addTests( unittest.defaultTestLoader.loadTestsFromModule(ExtraSettingsTest)) settingssuite2b.addTests( unittest.defaultTestLoader.loadTestsFromModule(ExtraSettings2Test)) serversuite1.addTests( unittest.defaultTestLoader.loadTestsFromModule( BasicNXSRecSelectorTest)) serversuite1b.addTests( unittest.defaultTestLoader.loadTestsFromModule( BasicNXSRecSelector2Test)) serversuite2.addTests( unittest.defaultTestLoader.loadTestsFromModule( ExtraNXSRecSelectorTest)) serversuite2b.addTests( unittest.defaultTestLoader.loadTestsFromModule( ExtraNXSRecSelector2Test)) # test runner runner = unittest.TextTestRunner() import argparse parser = argparse.ArgumentParser() parser.add_argument('args', metavar='name', type=str, nargs='*', help='suite names: all, basic, ' 'basicsettings, basicserver, ' 'extrasettings, extraserver ') options = parser.parse_args() namesuite = { "basic": [basicsuite, profilesuite], "basicsettings": [settingssuite1, settingssuite1b], "basicserver": [serversuite1, serversuite1b], "extrasettings": [settingssuite2, settingssuite2b], "extraserver": [serversuite2, serversuite2b], } print options.args if not options.args or 'all' in options.args: options.args = namesuite.keys() ts = [] for nm in options.args: if nm in namesuite.keys(): ts.extend(namesuite[nm]) suite = unittest.TestSuite(ts) # test result result = runner.run(suite).wasSuccessful() sys.exit(not result)
# This is where the magic happens if __name__ == '__main__': # Locale magic locale.setlocale(locale.LC_ALL, '') # Create the parser object if optparse: parser = OptionParser(description=my_description) parser_add_argument = parser.add_option else: parser = argparse.ArgumentParser(description=my_description) parser_add_argument = parser.add_argument parser.add_argument( 'work_dir', type=str, nargs='?', help='Root of the working directory (default: current working directory)') parser_add_argument( '--ffmpegarm', action='store_true', help='Leave arm sources when cleaning ffmpeg') parser_add_argument( '--beta', action='store_true', help='Get the latest beta Chromium source') parser_add_argument( '--clean', action='store_true', help='Re-download all previously downloaded sources') parser_add_argument( '--cleansources', action='store_true', help='Get the latest Chromium release from given channel and clean various directories to from unnecessary or unwanted stuff') parser_add_argument( '--dev', action='store_true',
def main(): country = False city = False asn = False if opt: usage = "usage: %prog [options] ip [ip ...]" parser = OptionParser(usage=usage) parser.add_option("-n", "--name", action="store_true", dest="name", default=False, help="Print country name instead of country code") parser.add_option("-t", "--country", dest="country", action="store_true", default=False, help="Do country lookup") parser.add_option("-d", "--database", dest="db", metavar="<db path>", help="Path to maxmind GeoIP database", default="/usr/local/share/GeoIP/GeoIP.dat") #City database parser.add_option("-c", "--city", dest="city", action="store_true", default=False, help="Do city lookup") parser.add_option("--city-database", dest="citydb", default="/usr/local/share/GeoIP/GeoLiteCity.dat", metavar="<city db path>", help="Path to maxmind city lite db") #ASN database parser.add_option("-a", "--asn", dest="asn", action="store_true", default=False, help="Do ASN lookup") parser.add_option("--asn-database", dest="asndb", default="/usr/local/share/GeoIP/GeoIPASNum.dat", metavar="<asn db path>", help="Path to maxmind asn lite db") options,args = parser.parse_args() if len(args) < 1: print "Need to provide ip address" parser.print_help() ips = args if options.city: city = True if checkFile(options.citydb): citydb = options.citydb if options.asn: asn = True if checkFile(options.asndb): asndb = options.asndb if options.country: country = True if checkFile(options.db): db = options.db if not city and not country and not asn: country = True if checkFile(options.db): db = options.db name = options.name else: parser = argparse.ArgumentParser() parser.add_argument('ips', metavar="ip", nargs="+", help="ip address/es to lookup") parser.add_argument("-co", "--country", dest="country", action="store_true", default=False, help="Do country lookup") parser.add_argument("-n", "--name", dest="name", action="store_true", default=False, help="Print country names instead of country codes") parser.add_argument("-d", "--database", dest="db", default="/usr/local/share/GeoIP/GeoIP.dat", metavar="<db path>", help="Path to maxmind GeoIP database") parser.add_argument("-c", "--city", dest="city", action="store_true", default=False, help="Do city lookup") parser.add_argument("-dc", "--city-database", dest="citydb", default="/usr/local/share/GeoIP/GeoLiteCity.dat", metavar="<city db path>", help="Path to maxmind city lite db") parser.add_argument("-a", "--asn", dest="asn", action="store_true", default=False, help="Do ASN lookup") parser.add_argument("-da", "--asn-database", dest="asndb", default="/usr/local/share/GeoIP/GeoIPASNum.dat", metavar="<asn db path>", help="Path to maxmind asn lite db") args = parser.parse_args() name = args.name ips = args.ips if args.city: city = True if checkFile(args.citydb): citydb = args.citydb if args.asn: asn = True if checkFile(args.asndb): asndb = args.asndb if args.country: country = True if checkFile(args.db): db = args.db if not city and not country and not asn: country = True if checkFile(args.db): db = args.db ## Verify shiz ## IP addresses if not verifyIPs(ips): print "Error: Invalid ip provided" exit(1) for ip in ips: co = "" if country: country = pygeoip.GeoIP(db, pygeoip.MEMORY_CACHE) if name: co += ":" + country.country_name_by_addr(ip) else: co += ":" + country.country_code_by_addr(ip) if asn: asn = pygeoip.GeoIP(asndb, pygeoip.MEMORY_CACHE) co += ":" + asn.org_by_name(ip) if city: city = pygeoip.GeoIP(citydb, pygeoip.MEMORY_CACHE) r = city.record_by_addr(ip) co += ":%s, %s" % (r["city"], r["region_code"]) print "%s%s" %(ip, co)