Пример #1
0
def processCommandLine(argv):
	parser = OptionParser("iTunesToRhythm [options] <inputfile>|itunes|mysql|wmp <outputfile>|mysql|itunes|wmp")
	parser.add_option("-c", "--confirm", action="store_true", dest="confirm", default = False, help="confirm every match")
	parser.add_option("-w", "--writechanges", action="store_true", dest="writeChanges", default = False, help="write changes to destination file")
	parser.add_option("-a", "--disambiguate", action="store_true", dest="promptForDisambiguate", default = False, help="prompt user to resolve ambiguities")
	parser.add_option("-l",  "--fastandloose", action="store_true", dest= "fastAndLoose",  default = False,  help = "ignore differences in files name when a file size match is made against  a single song.   Will not resolve multiple matches")
	parser.add_option("--noplaycounts", action="store_true", dest= "noplaycounts",  default = False,  help = "do not update play counts")
	parser.add_option("--noratings", action="store_true", dest= "noratings",  default = False,  help = "do not update ratings")
	parser.add_option("--twoway", action="store_true", dest= "twoway",  default = False,  help = "sync up the two files, giving precedence to the items with the higher playcount")
	parser.add_option("--dateadded", action="store_true", dest= "dateadded",  default = False,  help = "update dates (only iTunes to Rhythmbox on Linux)")
	parser.add_option("--useSongTitle", action="store_true", dest= "useSongTitle",  default = False,  help = "use song titles instead of file sizes to match songs")

	amarokGroup = OptionGroup(parser,  "Amarok options",  "Options for connecting to an Amarok MySQL remote database")
	amarokGroup.add_option("-s",  "--server",  dest="servername",  help = "host name of the MySQL database server")
	amarokGroup.add_option("-d",  "--database",  dest="database",  help = "database name of the amarok database")
	amarokGroup.add_option("-u",  "--username",  dest="username",  help = "login name of the amarok database")
	amarokGroup.add_option("-p",  "--password",  dest="password",  help = "password of the user")

	parser.add_option_group(amarokGroup)
	# parse options
	options, args = parser.parse_args()

	# check that files are specified
	if len(args) != 2:
			parser.print_help()
			parser.error("you must supply a source and destination")

	# make surce source & destination are not the same
	if args[0] == args[1]:
		parser.error("source and destination cannot be the same")

	# we're ok
	return options, args
Пример #2
0
def parse_options(argv):

    """Parses options from the command line """

    from optparse import OptionParser, OptionGroup

    parser = OptionParser()
    required = OptionGroup(parser, 'REQUIRED')
    required.add_option('-b', '--best_score', dest='best_scores', metavar='FILE', help='file to store the best scoring parameters', default='-')
    required.add_option('-m', '--matrix', dest='matrix', metavar='FILE', help='file to store the full performance matrix', default='-')
    required.add_option('-f', '--features', dest='features', metavar='FILE', help='alignment intron features', default='-')
    required.add_option('-i', '--annotation_introns', dest='anno_int', metavar='FILE', help='annotation intron list', default='-')
    optional = OptionGroup(parser, 'OPTIONAL')
    optional.add_option('-E', '--exclude_introns', dest='exclude_introns', metavar='STRINGLIST', help='list of comma separated intron files to exclude from submitted features', default='-')
    optional.add_option('-I', '--max_intron_len', dest='max_intron_len', metavar='INT', type='int', help='maximal intron length [10000000]', default=10000000)
    optional.add_option('-s', '--ignore_strand', dest='ignore_strand', action='store_true', help='ignore strand information present in annotation', default=False)
    optional.add_option('-X', '--max_feat_mismatches', dest='max_feat_mismatches', metavar='INT', type='int', help='max number of mismatches for feat generation [80] (do only change, if you are absolutely sure!)', default=80)
    optional.add_option('-v', '--verbose', dest='verbose', action='store_true', help='verbosity', default=False)
    parser.add_option_group(required)
    parser.add_option_group(optional)

    (options, args) = parser.parse_args()
    
    if len(argv) < 2:
        parser.print_help()
        sys.exit(2)

    return options
Пример #3
0
def get_command_line_options(executable_name, arguments):
    parser = OptionParser(usage="%s options" % executable_name)
    parser.set_defaults(port = 9876)
    parser.add_option("-x", "--xmlrpc", action="store_true", dest="xml", help="Start XMLRPC StatServer")
    parser.add_option("-n", "--npipe", action="store_true", dest="pipe", help="Start named pipe StatServer")
    parser.add_option("-o", "--out", type="string", dest="output", help="Output DB File")
    
    xml_group = OptionGroup(parser, "Options for XMLRPC Server")
    xml_group.add_option("-p", "--port", type="string", dest="port", help="port to run the server")

    parser.add_option_group(xml_group)

    pipe_group = OptionGroup(parser, "Options for named pipe Server")
    pipe_group.add_option("-i", "--input", type="string", dest="pipe_name", help="Filename of the named pipe")

    parser.add_option_group(pipe_group)
    
    (options, args) = parser.parse_args()

    if (options.xml and options.pipe) or (options.xml==None and options.pipe==None):
        parser.print_help()
        parser.error("You need to provide one of the following options, --xmlrpc or --npipe")

    if not options.output:
        parser.print_help()
        parser.error("You need to provide following options, --out=OutputDB.db")

    if options.pipe and not options.pipe_name:
        parser.print_help()
        parser.error("You need to provide following options, --input=named_pipe")
        
    return options
Пример #4
0
class TestOptionGroup(BaseTest):
    def setUp(self):
        self.parser = OptionParser(usage=SUPPRESS_USAGE)

    def test_option_group_create_instance(self):
        group = OptionGroup(self.parser, "Spam")
        self.parser.add_option_group(group)
        group.add_option("--spam", action="store_true", help="spam spam spam spam")
        self.assertParseOK(["--spam"], {"spam": 1}, [])

    def test_add_group_no_group(self):
        self.assertTypeError(self.parser.add_option_group, "not an OptionGroup instance: None", None)

    def test_add_group_invalid_arguments(self):
        self.assertTypeError(self.parser.add_option_group, "invalid arguments", None, None)

    def test_add_group_wrong_parser(self):
        group = OptionGroup(self.parser, "Spam")
        group.parser = OptionParser()
        self.assertRaises(
            self.parser.add_option_group, (group,), None, ValueError, "invalid OptionGroup (wrong parser)"
        )

    def test_group_manipulate(self):
        group = self.parser.add_option_group("Group 2", description="Some more options")
        group.set_title("Bacon")
        group.add_option("--bacon", type="int")
        self.assert_(self.parser.get_option_group("--bacon"), group)
Пример #5
0
 def setup_parser(self):
     """ Init the option parser. If derived classes need to add options,
     override this and call the parent function. """
     parser = OptionParser(add_help_option=False)
     parser.usage = '%prog ' + self.name + ' [options] <PATTERN> \n' + \
                    ' Call "%prog ' + self.name + '" without any options to run it interactively.'
     ogroup = OptionGroup(parser, "General options")
     ogroup.add_option("-h", "--help", action="help", help="Displays this help message.")
     ogroup.add_option("-d", "--directory", type="string", default=".",
             help="Base directory of the module. Defaults to the cwd.")
     ogroup.add_option("-n", "--module-name", type="string", default=None,
             help="Use this to override the current module's name (is normally autodetected).")
     ogroup.add_option("-N", "--block-name", type="string", default=None,
             help="Name of the block, where applicable.")
     ogroup.add_option("--skip-lib", action="store_true", default=False,
             help="Don't do anything in the lib/ subdirectory.")
     ogroup.add_option("--skip-swig", action="store_true", default=False,
             help="Don't do anything in the swig/ subdirectory.")
     ogroup.add_option("--skip-python", action="store_true", default=False,
             help="Don't do anything in the python/ subdirectory.")
     ogroup.add_option("--skip-occ", action="store_true", default=False,
             help="Don't do anything in the occ/ subdirectory.")
     ogroup.add_option("-y", "--yes", action="store_true", default=False,
             help="Answer all questions with 'yes'. This can overwrite and delete your files, so be careful.")
     parser.add_option_group(ogroup)
     return parser
Пример #6
0
def parse_options(args):
    from optparse import OptionParser, OptionGroup

    usage = "%prog [OPTION]... FASTA_FILENAME"
    version = "%%prog %s" % __version__
    description = ("pre-calculate nucleotide frequences for genome")

    parser = OptionParser(usage=usage, version=version,
                          description=description)

    group = OptionGroup(parser, "Variables")

    group.add_option("--region-size-minimum", action="store", type='int',
        default=1,
        help="minimum region size "
        " (default: %default)")

    group.add_option("--region-size-maximum", action="store", type='int',
        default=1,
        help="maximum region size "
        " (default: %default)")

    group.add_option("-v", "--verbose", action="store_true",
        default=False, help="verbose output (default: %default)")

    parser.add_option_group(group)

    options, args = parser.parse_args(args)

    if len(args) != 1:
        parser.error("specify FASTA")

    return options, args
Пример #7
0
def parse_options():

    description = \
    """This example script connects with the Message Server service and sends
    a message to another client.

    """

    epilog = \
    """pysap - http://corelabs.coresecurity.com/index.php?module=Wiki&action=view&type=tool&name=pysap"""

    usage = "Usage: %prog [options] -d <remote host> -t <target server> -m <message>"

    parser = OptionParser(usage=usage, description=description, epilog=epilog)

    target = OptionGroup(parser, "Target")
    target.add_option("-d", "--remote-host", dest="remote_host", help="Remote host")
    target.add_option("-p", "--remote-port", dest="remote_port", type="int", help="Remote port [%default]", default=3900)
    parser.add_option_group(target)

    misc = OptionGroup(parser, "Misc options")
    misc.add_option("-c", "--client", dest="client", default="pysap's-messager", help="Client name [%default]")
    misc.add_option("-m", "--message", dest="message", default="Message", help="Message to send to the target client [%default]")
    misc.add_option("-t", "--target", dest="target", default="pysap's-listener", help="Target client name to send the message [%default]")
    misc.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Verbose output [%default]")
    parser.add_option_group(misc)

    (options, _) = parser.parse_args()

    if not options.remote_host:
        parser.error("Remote host is required")
    if not options.message or not options.target:
        parser.error("Target server and message are required !")

    return options
Пример #8
0
def parser(mp=None):
	if mp==None: mp = OptionParser()
	mg1 = OptionGroup(mp,cyan+"dependencyFactory settings"+plain)
	mg1.add_option('--bmap',help='Calculate btagmap JetMon/QCD.',default=False,action='store_true')
	mg1.add_option('--kfac',help='Calculate k-factor.',default=False,action='store_true')
	mp.add_option_group(mg1)
	return mp
Пример #9
0
def generate_parser():
    parser = OptionParser()
    parser.add_option("-f","--hostname-file",dest="hostfile",
        help="File containing Hostnames's used in the test, one per line.")
    parser.add_option("-d","--dry-run",action="store_true",dest="dryrun",
        default=False, help="Don't actually issue the DGI startup commands, "
                            "just show the procedure.")
    parser.add_option("-n","--hostname",dest="hostnames",action="append",
        help="UUIDs to use for this experiment set.")
    parser.add_option("-t","--time",dest="time",default=None,
        help="The option that will be provided to the unix command"
             "timeout to terminate the run. Fomrat is of an argument to the"
             "UNIX timeout command. e.g. 10m for a 10 minute run.")
    parser.add_option("-c","--line-client",dest="lineclient",
        help="If specified, indicates the hostname which should be connected"
             "to in order to start the line server")
    parser.add_option("-e","--experiment",dest="experiment",action="store_true",
        help="Enable experiment mode, which can be used to observe the affects"
             "of network link reliability on the system")
    expgroup = OptionGroup(parser, "Network Experiments", 
        "These options will only be used in network experiment mode (-e). In"
        "this mode, the program will run for a specified period of time with"
        "incrementing network settings (from 0 up to full reliability")
    expgroup.add_option("-o","--output-file",dest="outputfile",default="exp.dat",
        help="File to write the experiment table to, if not provided, "
             "defaults to exp.dat")
    expgroup.add_option("-g","--granularity",dest="granularity",default=5,type="int",
        help="The simulator will run experiments with a step up or down in the"
             "granularity for each network connection.")
    parser.add_option_group(expgroup)
    return parser
Пример #10
0
    def run(self, *args):
        """Create a new site."""
        parser = OptionParser(usage=self.usage)
        group = OptionGroup(parser, "Site Options")
        group.add_option(
            "--empty", action="store_true", dest='empty', default=True,
            help="Create an empty site with only a config.")
        group.add_option("--demo", action="store_false", dest='empty',
                         help="Create a site filled with example data.")
        parser.add_option_group(group)
        (options, args) = parser.parse_args(list(args))

        if not args:
            print("Usage: nikola init folder [options]")
            return
        target = args[0]
        if target is None:
            print(self.usage)
        else:
            if options.empty:
                self.create_empty_site(target)
                print('Created empty site at %s.' % target)
            else:
                self.copy_sample_site(target)
                print("A new site with example data has been created at %s."
                      % target)
                print("See README.txt in that folder for more information.")

            self.create_configuration(target)
Пример #11
0
def getCmdlineOptions():
    """Commandline parser, see -- long description for each option"""
    parser = OptionParser()

    group1 = OptionGroup(parser, "Required input options")
    group1.add_option("-o", "--outputfile", dest = "outputfile",
                      help = "R-Kappa output file", default = "rkappagen.dat")
    group1.add_option("-t", "--timestep", dest = "timestep",
                      help = "timestep in ps", default = 10, type = "float")
    group1.add_option("-s", "--samples", dest = "samples",
                      help = "number of samples", default = 10000, type = "int")
    group1.add_option("-r", "--distance", dest = "distexpr",
                      help = "expression to calculate distance, use t for time", default = "5.4;R", type = "string")
    group1.add_option("-c", "--kappacorr", dest = "kappacorr",
                      help = "correlation of kappa^2 and distance, e.g. kappa2*(sin)", default = "kappa2", type = "string")


    group2 = OptionGroup(parser, "Optional input")
    group2.add_option("-d", "--decay", dest = "decay",
                      help = "orientation decay per timestep", default = 1, type = "float")

    parser.add_option_group(group1)
    parser.add_option_group(group2)

    return parser.parse_args()
Пример #12
0
def parse_parameters(args):
    """returns an option object with the values of the optional parameters and the mandatory arguments in the list"""
    from optparse import OptionParser, OptionGroup
    
    usage = "usage: %prog [options]  cathcode\n"
    usage+= "Example: get data for the 1.10.10.180 cath superfamily\n"
    usage+= "./getDomainsFromSP.py  1.10.10.180\n"
    usage+= "Example:get data for the 1.10.10.180 cath superfamily with all posible conf parameters \n"
    usage+= "./getDomainsFromSP.py  1.10.10.180 -w . -m localhost -u sflexfit -p sflexfit -d sflexfit\n"
    parser = OptionParser(usage=usage)
    
    parser.add_option("-t","--tar",dest="tar",action="store_true",default=False,help="generate a compressed tar file with the results")
    parser.add_option("-w","--workingDirectory", dest="workingDirectory",default=".",help="directory where the resulting files will be stored") 
    parser.add_option("-v","--verbose",action="store_true", dest="verbose",default=True,help="display program status")   
    parser.add_option("-j","--justTar",dest="justTar",action="store_true",default=False,help="remove all the generated files leaving just the tar")   
    
    group = OptionGroup(parser, "DataBase options", "DataBase connection parameters")
    group.add_option("-m","--DBServer", dest="host", default="okazaki.cnb.csic.es",help="database server")
    group.add_option("-u","--user", dest="user", default="poli",help="database user name")
    group.add_option("-p","--passwd", dest="passwd", default="R7xvgAKK",help="user password")
    group.add_option("-d","--db", dest="db", default="sflexfit",help="database name")
    parser.add_option_group(group)
    
    (options, args)= parser.parse_args(args)

    if len(args) != 1: #cathcode is the single mandatory parameter
        print parser.get_usage()
        sys.exit(2)
    
    return (options,args)
Пример #13
0
def parser(mp=None):
	if not mp: mp = OptionParser()
#	mp.add_option('','',help=colours[5]+''+colours[0],default='',type='',dest='')
#
#	mg1 = OptionGroup(mp,'GroupTitle')
#	mg1.add_option('','',help=colours[5]+''+colours[0],default='',type='',dest='')
#	mp.add_option_group(mg1)
#
	mp.add_option('--workdir',help=colours[5]+'Case workdir.'+colours[0],default='case0',type='str')
	mp.add_option('--long',help=colours[5]+'Long filenames.'+colours[0],default=False,action='store_true')
	mp.add_option('-v','--verbosity',help=colours[5]+'Verbosity.'+colours[0],default=0,action='count')
	mp.add_option('-q','--quiet',help=colours[5]+'Quiet.'+colours[0],default=True,action='store_false')
#
	mg1 = OptionGroup(mp,'Selection setup')
	mg1.add_option('--SELCATs',help=colours[5]+'Selection/Category setup: "NOM;NOM;-0.6#0.0#0.7#0.84#1.0,VBF;PRK;-0.1#0.4#0.8#1.0,...".'+colours[0],default='NOM;NOM;-0.6#0.0#0.7#0.84#1.0,VBF;PRK;-0.1#0.4#0.8#1.0',type='str',action='callback',callback=SELsetup,dest='SC')
	mp.add_option_group(mg1)
#
	mg2 = OptionGroup(mp,'Data template setup')
	mg2.add_option('--bounds',help=colours[5]+'Template boundaries: 80,200 (min,max)'+colours[0],default=[80.,200.],type='str',action='callback',callback=optsplitfloat,dest='X')
	mg2.add_option('--binwidth',help=colours[5]+'Template bin width: 0.1,0.1 (NOM,VBF)'+colours[0],default=[0.1,0.1],type='str',action='callback',callback=optsplitfloat,dest='dX')
	mg2.add_option('--lumi',help=colours[5]+'Luminosity: 19784.,18281. (NOM,VBF)'+colours[0],default=[19784.,18281.],type='str',action='callback',callback=optsplitfloat,dest='LUMI')
	mg2.add_option('--TF',help=colours[5]+'Transfer function label: POL1,POL2 (NOM,VBF)'+colours[0],default=['POL1','POL2'],type='str',action='callback',callback=optsplit)
	mg2.add_option('--BRN',help=colours[5]+'Bernstein order: 5,4 (NOM,VBF)'+colours[0],default=[5,4],type='str',action='callback',callback=optsplitint)
	mg2.add_option('--MASS',help=colours[5]+'Signal masspoint: 125'+colours[0],default=125,type='int')
	mg2.add_option('--forfit',help=colours[5]+'Freeze/free parameters for fit or not (for toys)'+colours[0],default=True,action='store_false')
	mp.add_option_group(mg2)
#
	return mp
Пример #14
0
def parse_options():

    description = "This example script gather information provided by a SAP Netweaver Application Server during the " \
                  "login process. This information includes generally hostname, instance, database name, etc."

    epilog = "pysap %(version)s - %(url)s - %(repo)s" % {"version": pysap.__version__,
                                                         "url": pysap.__url__,
                                                         "repo": pysap.__repo__}

    usage = "Usage: %prog [options] -H <remote host>"

    parser = OptionParser(usage=usage, description=description, epilog=epilog)

    target = OptionGroup(parser, "Target")
    target.add_option("-d", "--remote-host", dest="remote_host", help="SAP remote host")
    target.add_option("-p", "--remote-port", dest="remote_port", type="int", help="SAP remote port [%default]", default=3200)
    target.add_option("--route-string", dest="route_string", help="Route string for connecting through a SAP Router")
    parser.add_option_group(target)

    misc = OptionGroup(parser, "Misc options")
    misc.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Verbose output [%default]")
    misc.add_option("--terminal", dest="terminal", default=None, help="Terminal name")
    parser.add_option_group(misc)

    (options, _) = parser.parse_args()

    if not options.remote_host:
        parser.error("Remote host is required")

    return options
Пример #15
0
def parse_options():
    parser = OptionParser()
    parser.add_option(
        "-a", "--ampfile", dest="ampsname", metavar="AMP", help="single input .amp file (default: %default)"
    )
    parser.add_option(
        "-s", "--segfile", dest="segsname", metavar="SEG", help="single input .seg file (default: %default)"
    )
    parser.add_option(
        "-w", "--wavfile", dest="wavname", metavar="WAV", help="single input .wav file (default: %default)"
    )

    group = OptionGroup(parser, "Batch processing")
    group.add_option(
        "-i",
        "--input-dir",
        dest="indir",
        help="input directory; must contain wav/ and amps/ subdirectories" " with matching filesets.",
    )
    group.add_option("-o", "--output-dir", dest="outdir", help="output directory (will be created if it doesn't exist)")
    parser.add_option_group(group)

    parser.set_defaults(ampsname="test.amp", segsname="test.seg", wavname="test.wav")

    return parser.parse_args()
Пример #16
0
def get_parser_instance(defaults=None):
    p = OptionParser()
    unimp_group=OptionGroup(p, "Unimplimented options",
        "These options do NOT work yet, they will be accepted and ignored.")

    p.add_option("--outputdir", "-d", dest="outputdir", type="string",
                help="Directory to put the encoded file in")
    p.add_option("--config", "-c", dest="configfile", type="string")
    p.add_option("--audiobitrate", "-a", dest="abr", type="string")
    unimp_group.add_option("--hostname", "-H", dest="hostname", type="string",
                help="hostname for the backend to connect to. This is"+
                     " being ignored for the moment")
    unimp_group.add_option("--port", "-p", dest="portnumber", type="int",
                help="Port to connect or listen on")
    unimp_group.add_option("--debug", "-D", dest="debug", action="count")
    #long only options
    #p.add_option("--ignore-defaults", dest="config", type="string")
    p.add_option("--crf", dest="crf", type="string",
                 help="which x264 CRF number to use. default:22")
    unimp_group.add_option("--threads", dest="threads", type="string",
                           help="number of threads to use. default auto")
    p.add_option_group(unimp_group)
    if not defaults is None:
        if type(defaults) == type(dict()):
            p.set_defaults(**defaults)
    return p
Пример #17
0
    def _getCommandLineParser(cls):
        """
        """
        parser = OptionParser(usage=cls._usage)

        workflowSettings = OptionGroup(parser, 'General workflow settings')
        workflowSettings.add_option('--jobId', '-j', dest='jobId', type='str',
                default=None, help='Job identifier. An optional value identyfying this particular workflow. If ommited, the jobId will be generated automatically.')
        workflowSettings.add_option('--workDir', '-d', dest='workdir', type='str',
                default=None, help='Sets the working directory of the process. Overrides the "--disableSharedMemory" switch.')
        workflowSettings.add_option('--loglevel', dest='loglevel', type='str',
                default='WARNING', help='Loglevel: CRITICAL | ERROR | WARNING | INFO | DEBUG')
        workflowSettings.add_option('--logFilename', dest='logFilename',
                default=None, action='store', type='str',
                help='Sets dumping the execution log file instead stderr')
        workflowSettings.add_option('--disableSharedMemory', default=False,
            dest='disableSharedMemory', action='store_const', const=True,
            help='Forces script to use hard drive to store the worklfow data instaed of using RAM disk.')
        workflowSettings.add_option('--specimenId', default=None,
                dest='specimenId', action='store', type='str',
                help='Identifier of the specimen. Providing the ID is obligatory. Script will not run without providing specimen ID.')
        workflowSettings.add_option('--dryRun', default=False,
                action='store_const', const=True, dest='dryRun',
                help='Prints the commands to stdout instead of executing them')
        workflowSettings.add_option('--cpuNo', '-n', default=None,
                type='int', dest='cpuNo',
                help='Set a number of CPUs for parallel processing. If skipped, the number of CPUs will be automatically detected.')
        workflowSettings.add_option('--archiveWorkDir',default=None,
                type='str', dest='archiveWorkDir',
                help='Compresses (.tgz) and moves workdir to a given directory')
        workflowSettings.add_option('--cleanup', default=False,
                dest='cleanup', action='store_const', const=True,
                help='Remove the worklfow directory after calculations. Use when you are sure that the workflow will execute correctly.')
        parser.add_option_group(workflowSettings)
        return parser
Пример #18
0
def parser():
    """
    Create an ``optparse.OptionParser`` object to read command line arguments.
    """
    from optparse import OptionParser, OptionGroup
    from main.help import usage
    parser = OptionParser(usage=usage())
    group = OptionGroup(parser, "Logs")
    group.add_option('-d', '--debug',
                     dest='debug',
                     type='int',
                     help='set debug level',)
    group.add_option('-q', '--quite',
                     dest='quite',
                     action='store_true',
                     help='quite mode, do not print any messages to stdout',)
    group.add_option('-v', '--verbose',
                     dest='verbose',
                     action='store_true',
                     help='verbose mode, print more messages',)
    group.add_option('-n', '--no-logs',
                     dest='no_logs',
                     action='store_true',
                     help='do not use logs',)
    group.add_option('-o', '--output',
                     dest='output',
                     type='string',
                     help='print log messages to the file',)
    group.add_option('--twisted',
                     dest='twisted',
                     action='store_true',
                     help='show twisted log messages too',)
    parser.add_option_group(group)
    return parser
Пример #19
0
    def run(self, argv):
        parser = OptionParser(usage="%prog [options] [mode] [project]",
                              description='The jscc is a tool that helps you compile js code using google closure compiler.')

        mode_group = OptionGroup(parser, "Mode options (only specify one)")

        mode_group.add_option('-c', '--create', action='store_const', dest='mode', const='create', help='Create a new project.')
        mode_group.add_option('-u', '--update', action='store_const', dest='mode', const='update', help='Update the project. This is default mode.')
        mode_group.add_option('-w', '--watch', action='store_const', dest='mode', const='watch', help='Monitor the project for changes and update.')

        parser.add_option_group(mode_group)

        parser.add_option('--compiler', default='closure-compiler', help='Path to the google closure compiler. By default used: closure-compiler (see INSTALL for more details)')
        parser.add_option('-f', '--force', dest='force', action='store_true', help='Force recompile the project.')
        parser.add_option('-D', '--debug-mode', dest='debug_mode', action='store_true', help='Do not compile files. Just concatinate them.')
        parser.add_option('-d', '--debug', action='store_true', help='Display debug output')

        (options, args) = parser.parse_args(argv)
        if len(args) > 2:
            parser.error("incorrect number of arguments")

        self.compiler = options.compiler
        self.debug_mode = options.debug_mode or False
        force = options.force or False

        if options.debug:
            logging.basicConfig(level=logging.DEBUG)
        else:
            logging.basicConfig(level=logging.INFO)

        mode = options.mode or 'update'
        project = args[1] if len(args) > 1 else DEFAULT_PROJECT_FILENAME
        getattr(self, mode)(project, force=force)
Пример #20
0
def parse_options():

    description = \
    """This script is an example implementation of SAP's Enqueue Server Monitor
    program (ens_mon). It allows the monitoring of a Enqueue Server service and
    allows sending different admin commands and opcodes. Includes some commands
    not available on the ensmon program.
    """

    epilog = \
    """pysap - http://corelabs.coresecurity.com/index.php?module=Wiki&action=view&type=tool&name=pysap"""

    usage = "Usage: %prog [options] -d <remote host>"

    parser = OptionParser(usage=usage, description=description, epilog=epilog)

    target = OptionGroup(parser, "Target")
    target.add_option("-d", "--remote-host", dest="remote_host", help="Remote host")
    target.add_option("-p", "--remote-port", dest="remote_port", type="int", help="Remote port [%default]", default=3200)
    parser.add_option_group(target)

    misc = OptionGroup(parser, "Misc options")
    misc.add_option("-c", "--client", dest="client", default="pysap's-monitor", help="Client name [%default]")
    misc.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Verbose output [%default]")
    misc.add_option("--log-file", dest="logfile", help="Log file")
    misc.add_option("--console-log", dest="consolelog", help="Console log file")
    misc.add_option("--script", dest="script", help="Script file to run")
    parser.add_option_group(misc)

    (options, _) = parser.parse_args()

    if not options.remote_host:
        parser.error("Remote host is required")

    return options
Пример #21
0
def parseArgs( req , opt ):
        tome = {}
        usage = "usage: %prog -H hostname -U user -P password [-D database] [-q query_sql]"
        parser = OptionParser(usage=usage)
        # Declare Required Options
        required = OptionGroup(parser, "Required Options")
        for arg in req:
                required.add_option( arg[0] , '--' + arg[1] , help = arg[2] , default = arg[3] )
        parser.add_option_group(required)
        # Declare Optional Options
        optional = OptionGroup(parser, "Optional Options of Redundancy")
        for arg in opt:
                optional.add_option( arg[0] , '--' + arg[1], help = arg[2] , default = arg[3] )
        parser.add_option_group(optional)
        # Parse all args in options.args, convert to iterable dictionary
        ( options, args ) = parser.parse_args()
        for arg in required.option_list:
                tome[ arg.dest ] = getattr(options, arg.dest)
                if not tome[ arg.dest ]:
                        print "All arguments listed in Required Options must have a value."
                        parser.print_help()
                        sys.exit(3)
        for arg in optional.option_list:
                tome [ arg.dest ] = getattr(options, arg.dest)
        return tome
Пример #22
0
Файл: dx.py Проект: swails/spam
def test(args):
   from optparse import OptionParser, OptionGroup
   import sys

   parser = OptionParser()
   parser.add_option('-O', '--overwrite', dest='owrite', default=False,
                     action='store_true', help='Allow overwriting files')
   group = OptionGroup(parser, 'ThreeDGrid', 'Test the ThreeDGrid Class')
   group.add_option('-d', '--dx', dest='indx', default=None,
                    help='Input DX file.', metavar='FILE')
   group.add_option('-o', '--out-dx', dest='outdx', default=None,
                    help='Output DX file. Will just be a copy of the input.',
                    metavar='FILE')
   parser.add_option_group(group)
   group = OptionGroup(parser, 'Density Queries', 'Test some of the querying ' +
                       'functionality of the ThreeDGrid class')
   group.add_option('-x', '--x', dest='x', metavar='X_COORDINATE', default=None,
                    type='float', help='X-coordinate to find the density from')
   group.add_option('-y', '--y', dest='y', metavar='Y_COORDINATE', default=None,
                    type='float', help='Y-coordinate to find the density from')
   group.add_option('-z', '--z', dest='z', metavar='Z_COORDINATE', default=None,
                    type='float', help='Z-coordinate to find the density from')
   parser.add_option_group(group)

   opt, arg = parser.parse_args(args=args)

   global overwrite
   overwrite = opt.owrite

   if opt.outdx and not opt.indx:
      print("I cannot output a DX file without an input!")
      sys.exit(1)

   if opt.indx:
      test_dx = read_dx(opt.indx)
      print "%s read successfully" % opt.indx
      print "Grid Statistics:"
      print "  Origin:     (%g, %g, %g)" % (test_dx.xorigin, test_dx.yorigin,
                                        test_dx.zorigin)
      print "  Resolution:  %g [%g, %g]" % (test_dx.xres, test_dx.yres, 
                                           test_dx.zres)
      print "  Grid points: %d x %d x %d" % test_dx.shape
      print "  Grid desc: [ %s ]" % test_dx.description

      if opt.outdx:
         print "\nWriting output DX file %s" % opt.outdx
         test_dx.write_dx(opt.outdx)

   if (opt.x is None or opt.y is None or opt.z is None) and (
       opt.x is not None or opt.y is not None or opt.z is not None):
      print 'You must specify -x, -y, and -z all together!'
      sys.exit(1)

   if opt.x is not None and opt.indx is None:
      print 'You must give me an input DX file to analyze...'
      sys.exit(1)

   if opt.x is not None:
      print "The density at {%f, %f, %f} is %g" % (opt.x, opt.y, opt.z,
         test_dx.get_density_cartesian(opt.x, opt.y, opt.z))
def parse_options(argv):
    '''
    option parser
    '''
    ### required input
    parser = OptionParser()
    required  = OptionGroup(parser, 'Input')
    required.add_option('-s', '--star', dest = 'fn_star', metavar = 'FILE', help = 'STAR count file') 
    required.add_option('-t', '--tophat', dest = 'fn_tophat', metavar = 'FILE', help = 'Tophat count file') 


    ### optional arguments
    optional = OptionGroup(parser, 'optional')
    
    ## outdir
    outdir_hstr = 'Output direcory for all files'
    outdir = os.path.realpath(__file__).rsplit('/',1)[0]
    optional.add_option('-o', '--out_dir', dest = 'out_dir', help = outdir_hstr, default = outdir)

    ## genelength buffe
    fnl_hstr = 'Location of file with gene length'
    fnlfile =  os.path.join(os.path.realpath(__file__).rsplit('/',1)[0], 'data','geneLength.tsv')
    optional.add_option('-g', '--genelength', dest = 'fn_length',metavar = 'FILE', help = fnl_hstr, default = fnlfile)

    ## annotation file
    fna_hstr = 'Annotation file [gtf]'
    fnafile = os.path.join(os.path.realpath(__file__).rsplit('/',1)[0], 'data','gencode.v19.annotation.hs37d5_chr.gtf')
    optional.add_option('-a', '--anno', dest = 'fn_anno', metavar = 'FILE', help = fna_hstr, default = fnafile)

    
    parser.add_option_group(required)
    parser.add_option_group(optional)
    (options, args) = parser.parse_args()
    
    return options
Пример #24
0
def transform(writer=None, part=None):
    p = OptionParser(add_help_option=False)
    
    # Collect all the command line options
    docutils_parser = DocutilsOptionParser(components=(writer, Parser()))
    for group in docutils_parser.option_groups:
        p.add_option_group(group.title, None).add_options(group.option_list)
    
    p.add_option('--part', default=part)
    
    opts, args = p.parse_args()
    
    settings = dict({
        'file_insertion_enabled': False,
        'raw_enabled': False,
    }, **opts.__dict__)
    
    if len(args) == 1:
        try:
            content = open(args[0], 'rb').read()
        except IOError:
            content = args[0]
    else:
        content = sys.stdin.read()
    
    parts = publish_parts(
        source=content,
        settings_overrides=settings,
        writer=writer,
    )
    
    if opts.part in parts:
        return parts[opts.part]
    return u''
Пример #25
0
def _get_option_parser():
    usage = """%prog [options] <command> <files>

Available commands:
    add             Add a torrent or add a seed bank to a torrent
    remove          Remove a torrent or remove a seed bank from a torrent4
    list            List torrents or list seed banks for a given torrent
    stats           Stats functions: push stats to terasaur, capture data"""

    #update          Update information for a torrent

    parser = OptionParser(usage=usage,
                          version='%prog 1.0',
                          description='Terasaur tracker MQ CLI')
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
                      default=False, help="Enable verbose output")
    parser.add_option("--debug", action="store_true", dest="debug",
                      default=False, help="Enable debug output and stacktraces")
    parser.add_option('--infohash', dest='info_hash', type='string', metavar="SHA1", help='Specify info hash', default=None)
    parser.add_option('--seedbank', dest='seedbank', type='string', metavar="IP_PORT", help='Seed bank ip address and port (addr:port)', default=None)
    parser.add_option("--usemq", action="store_true", dest="usemq",
                      default=False, help="Use the MQ instead of direct-to-mongodb")

    stats_group = OptionGroup(parser, 'Stats options', '')
    stats_group.add_option("--full", action="store_true", dest="stats_full", default=False, help="Send updates for all torrents")
    stats_group.add_option("--incremental", action="store_true", dest="stats_incremental", default=False, help="Send updates for torrents changed since the last run")
    stats_group.add_option("--capture", action="store_true", dest="stats_capture", default=False, help="Take a stats snapshot")
    stats_group.add_option("--init", action="store_true", dest="stats_init", default=False, help="Initialize stats database")
    parser.add_option_group(stats_group)

    #update_group = OptionGroup(parser, 'Update options', '')
    #update_group.add_option('--published', dest='published', type='string', metavar="DATE", help='Published datetime (yyyy-mm-ddThh:mm:ss.mmmZ)', default=None)
    #parser.add_option_group(update_group)

    return parser
Пример #26
0
def parse_options():

    description = "This example script connects with the Message Server service and listen for messages coming from " \
                  "the server."

    epilog = "pysap %(version)s - %(url)s - %(repo)s" % {"version": pysap.__version__,
                                                         "url": pysap.__url__,
                                                         "repo": pysap.__repo__}

    usage = "Usage: %prog [options] -d <remote host>"

    parser = OptionParser(usage=usage, description=description, epilog=epilog)

    target = OptionGroup(parser, "Target")
    target.add_option("-d", "--remote-host", dest="remote_host", help="Remote host")
    target.add_option("-p", "--remote-port", dest="remote_port", type="int", help="Remote port [%default]", default=3900)
    target.add_option("--route-string", dest="route_string", help="Route string for connecting through a SAP Router")
    parser.add_option_group(target)

    misc = OptionGroup(parser, "Misc options")
    misc.add_option("-c", "--client", dest="client", default="pysap's-listener", help="Client name [%default]")
    misc.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Verbose output [%default]")
    parser.add_option_group(misc)

    (options, _) = parser.parse_args()

    if not options.remote_host:
        parser.error("Remote host is required")

    return options
Пример #27
0
def nib_parse_args(args):
    '''
    Displays custom dialogs from NIBs.

    nib usage:
    "$DIALOG" nib --load <nib file> [<options>]
    "$DIALOG" nib --update <token> [<options>]
    "$DIALOG" nib --wait <token>
    "$DIALOG" nib --dispose <token>
    "$DIALOG" nib --list

    Options:
        --center
        --model <plist>
        --prototypes <plist>
    '''
    parser = OptionParser()
    parser.add_option('--load', action = 'store', metavar="file",
                        help = 'Load nib file [options].')
    parser.add_option('--update', action = 'store', metavar="token",
                        help = 'Update dialog [options].')
    parser.add_option('--wait', action = 'store', metavar="token")
    parser.add_option('--dispose', action = 'store', metavar="token")
    parser.add_option('--list', action = 'store_true', default = False)
    
    #Options
    group = OptionGroup(parser, "Options")
    group.add_option('--center', action = 'store_true', default = False,
                          help = 'Center the window on screen.')
    group.add_option('--model', action = 'store', dest="plist")
    group.add_option('--prototypes', action = 'store', dest="plist")
    parser.add_option_group(group)
    
    options, args = parser.parse_args(args)
    return options, args
Пример #28
0
def SetupOptionsParser(): #{
  description_string = ("") #TODO
  args = [ "LIB", "BARNACLE_FILE", ] #TODO
  usage_string = "%prog " + " ".join(args) + " [ OPTIONS ]"
  parser = OptionParser(description=description_string,
                        version="%prog " + VERSION,
                        usage=usage_string)
  parser.num_args = len(args)
  misc_group = OptionGroup(parser, "Miscellaneous Options")
  misc_group.add_option("-f", "--force",
                    action="store_true",
                    help="Force filtering to take place, even if the output "
                         "directory already exists.")
  misc_group.add_option("-d", "--debug",
                    action="store_true",
                    help="Print debug information while the program runs.")
  misc_group.add_option("--extreme-debug",
                    action="store_true", dest="extreme_debug",
                    help="Print extremely in-depth debug information while "
                      "the program runs. Not recommended for large jobs.")
  parser.add_option_group(misc_group)
  parser.set_defaults(force=False,
                      debug=False,
                      extreme_debug=False)
  return parser
Пример #29
0
def parser(mp=None):
	if not mp: mp = OptionParser()
#	mp.add_option('','',help=colours[5]+''+colours[0],default='',type='',dest='')
#
#	mg1 = OptionGroup(mp,'GroupTitle')
#	mg1.add_option('','',help=colours[5]+''+colours[0],default='',type='',dest='')
#	mp.add_option_group(mg1)
#
	mp.add_option('--workdir',help=colours[5]+'Case workdir.'+colours[0],default='case0',type='str')
	mp.add_option('--long',help=colours[5]+'Long filenames.'+colours[0],default=False,action='store_true')
	mp.add_option('-v','--verbosity',help=colours[5]+'Verbosity.'+colours[0],default=0,action='count')
	mp.add_option('-q','--quiet',help=colours[5]+'Quiet.'+colours[0],default=True,action='store_false')
#
	mg1 = OptionGroup(mp,'Selection setup')
	mg1.add_option('--SELCATs',help=colours[5]+'Selection/Category setup: "NOM;NOM;-0.6#0.0#0.7#0.84#1.0,VBF;PRK;-0.1#0.4#0.8#1.0,...".'+colours[0],default='NOM;NOM;-0.6#0.0#0.7#0.84#1.0,VBF;PRK;-0.1#0.4#0.8#1.0',type='str',action='callback',callback=SELsetup,dest='SC')
	mp.add_option_group(mg1)
#
	mg2 = OptionGroup(mp,'Sig template setup')
	mg2.add_option('--bounds',help=colours[5]+'Template boundaries: 80,200 (min,max)'+colours[0],default=[80.,200.],type='str',action='callback',callback=optsplitfloat,dest='X')
	#mg2.add_option('--binsize',help=colours[5]+'Template NBINS (XMAX-XMIN)/dX: 1200,1200 (NOM,VBF)'+colours[0],default=[1200,1200],type='str',action='callback',callback=optsplitint,dest='NBINS')
	mg2.add_option('--binwidth',help=colours[5]+'Binwidth: 0.1,0.1 (NOM,VBF)'+colours[0],default=[0.1,0.1],type='str',action='callback',callback=optsplitfloat,dest='dX')
	mg2.add_option('--lumi',help=colours[5]+'Luminosity: 19784.,18281. (NOM,VBF)'+colours[0],default=[19784.,18281.],type='str',action='callback',callback=optsplitfloat,dest='LUMI')
	mg2.add_option('--mass',help=colours[5]+'Mass: 115,120,125,...'+colours[0],default=[115,120,125,130,135],type='str',action='callback',callback=optsplitint,dest='MASS')
        mg2.add_option('--massextra',help=colours[5]+'Mass: 100,105,...'+colours[0],default=[0],type='str',action='callback',callback=optsplitint,dest='MASSEXTRA')
	mp.add_option_group(mg2)

#
	return mp
Пример #30
0
    def optParse():
        """
        Parses command line options.

        Generally we're supporting all the command line options that doxypy.py
        supports in an analogous way to make it easy to switch back and forth.
        We additionally support a top-level namespace argument that is used
        to trim away excess path information.
        """

        parser = OptionParser(prog=basename(argv[0]))

        parser.set_usage("%prog [options] filename")
        parser.add_option(
            "-a", "--autobrief",
            action="store_true", dest="autobrief",
            help="parse the docstring for @brief description and other information"
        )
        parser.add_option(
            "-c", "--autocode",
            action="store_true", dest="autocode",
            help="parse the docstring for code samples"
        )
        parser.add_option(
            "-n", "--ns",
            action="store", type="string", dest="topLevelNamespace",
            help="specify a top-level namespace that will be used to trim paths"
        )
        parser.add_option(
            "-t", "--tablength",
            action="store", type="int", dest="tablength", default=4,
            help="specify a tab length in spaces; only needed if tabs are used"
        )
        group = OptionGroup(parser, "Debug Options")
        group.add_option(
            "-d", "--debug",
            action="store_true", dest="debug",
            help="enable debug output on stderr"
        )
        parser.add_option_group(group)

        ## Parse options based on our definition.
        (options, filename) = parser.parse_args()

        # Just abort immediately if we are don't have an input file.
        if not filename:
            stderr.write("No filename given." + linesep)
            sysExit(-1)

        # Turn the full path filename into a full path module location.
        fullPathNamespace = filename[0].replace(sep, '.')[:-3]
        # Use any provided top-level namespace argument to trim off excess.
        realNamespace = fullPathNamespace
        if options.topLevelNamespace:
            namespaceStart = fullPathNamespace.find(options.topLevelNamespace)
            if namespaceStart >= 0:
                realNamespace = fullPathNamespace[namespaceStart:]
        options.fullPathNamespace = realNamespace

        return options, filename[0]
Пример #31
0
def parse_args(sys_args):
    usage = """%prog [-h | --help] [options] [cmdoptions | <filename> | -]

    Where <filename> is a file containing a NETCONF XML command session.
    If <filename> is not given, one of the Command Options must be given.
    Filename '-' means standard input.
    """

    parser = OptionParser(usage, formatter=HelpFormatterWithLineBreaks())
    parser.add_option("-v",
                      "--version",
                      dest="version",
                      help="force NETCONF version 1.0 or 1.1")
    parser.add_option("-u",
                      "--user",
                      dest="username",
                      default="admin",
                      help="username")
    parser.add_option("-p",
                      "--password",
                      dest="password",
                      default="admin",
                      help="password")
    parser.add_option(
        "--proto",
        dest="proto",
        default="ssh",
        help="Which transport protocol to use, one of ssh or tcp")
    parser.add_option("--host",
                      dest="host",
                      default="localhost",
                      help="NETCONF agent hostname")
    parser.add_option("--port",
                      dest="port",
                      default=2022,
                      type="int",
                      help="NETCONF agent SSH port")
    parser.add_option("--iter", dest="iter", default=1, type="int",
                      help="Sends the same request ITER times.  Useful only in" \
                      " test scripts")
    parser.add_option("--ssh-output-trace",
                      dest="ssh_trace",
                      default="",
                      help="Write a copy of all SSH data sent in a file")
    parser.add_option("-i",
                      "--interactive",
                      dest="interactive",
                      action="store_true")

    styleopts = parser.add_option_group("Style Options")
    styleopts.description = "raw:    print bytes received on the wire,"\
                           " framing is removed\n" \
                           "plain:  skip the <hello> reply, print one reply as" \
                           " raw, skip the rest\n" \
                           "pretty: skip the <hello> reply, pretty-print one" \
                           " reply, skip the rest\n" \
                           "all:    skip the <hello> reply, pretty-print the rest" \
                           " of the replies\n" \
                           "noaaa:  as pretty, but remove Tail-f AAA and IETF" \
                           " NACM from the output"
    styleopts.add_option("-s", "--outputStyle", dest="style", default="default",
                      help="Display output in: raw, plain, pretty, all, " \
                           "or noaaa format")

    sshopts = parser.add_option_group("SSH Options")
    sshopts.add_option("--privKeyType",
                       dest="privKeyType",
                       default="",
                       help="type of private key, rsa or dss")
    sshopts.add_option("--privKeyFile",
                       dest="privKeyFile",
                       default="",
                       help="file which contains the private key")

    tcpopts = parser.add_option_group("TCP Options")
    tcpopts.add_option(
        "-g",
        "--groups",
        dest="groups",
        default="",
        help="Set group membership for user - comma separated string")
    tcpopts.add_option("-G", "--sup-groups", dest="supgroups", default="",
                       help="Set supplementary UNIX group ids for user - comma " \
                       "separated string of integers")

    cmdopts = parser.add_option_group("NETCONF Command Options")
    cmdopts.add_option("--hello",
                       dest="hello",
                       action="store_true",
                       help="Connect to the server and print its capabilities")
    cmdopts.add_option(
        "--get",
        dest="get",
        action="store_true",
        help="Takes an optional -x argument for XPath filtering")
    cmdopts.add_option("--get-config", action="callback", callback=get_config_opt,
                       help="Takes an optional --db argument, default is 'running'"\
                       ", and an optional -x argument for XPath filtering")
    cmdopts.add_option(
        "--db",
        dest="db",
        default="running",
        help="Database for commands that operate on a database."),
    cmdopts.add_option("--with-defaults", dest="wdefaults", default="",
                       help="One of 'explicit', 'trim', 'report-all' or "\
                       "'report-all-tagged'.  Use with --get, --get-config, or "\
                       "--copy-config.")
    cmdopts.add_option("--with-inactive", dest="winactive", action="store_true",
                       help="Send with-inactive parameter.  Use with --get, "\
                       "--get-config, --copy-config, or --edit-config.")
    cmdopts.add_option("-x", "--xpath", dest="xpath", default="",
                       action="callback", callback=opt_xpath,
                       help="XPath filter to be used with --get, --get-config, " \
                       "and --create-subscription")
    cmdopts.add_option("--subtree", dest="subtree", default="",
                       action="callback", callback=opt_xpath,
                       help="Subtree filter to be used with --get and " \
                       "--get-config. Only useful with devices that do not " \
                       "support --xpath filters. Specify elements to "\
                       "include as XML (with or without namespaces).")
    cmdopts.add_option("--kill-session",
                       dest="kill_session",
                       default="",
                       help="Takes a session-id as argument.")
    cmdopts.add_option("--discard-changes",
                       dest="discard_changes",
                       action="store_true")
    cmdopts.add_option("--commit", dest="commit", action="store_true")
    cmdopts.add_option(
        "--validate",
        dest="validate",
        action="store_true",
        help="Takes an optional --db argument, default is 'running'.")
    cmdopts.add_option("--copy-running-to-startup",
                       dest="copy_running_to_startup",
                       action="store_true")
    cmdopts.add_option("--copy-config", dest="copy", default="",
                       help="Takes a filename (or '-' for standard input) as"\
                       " argument. The contents of the file"\
                       " is data for a single NETCONF copy-config operation"\
                       " (put into the <config> XML element)."\
                       " Takes an optional --db argument, default is 'running'.")
    cmdopts.add_option("--edit-config", dest="edit", default="",
                       help="Takes a filename (or '-' for standard input) as "\
                       " argument. The contents of the file"\
                       " is data for a single NETCONF edit-config operation"\
                       " (put into the <config> XML element)."\
                       " Takes an optional --db argument, default is 'running'.")
    cmdopts.add_option("--get-schema", dest="get_schema",
                       help="Takes an identifier (typically YANG module name) "\
                       "as parameter")
    cmdopts.add_option("--create-subscription", dest="create_subscription",
                       help="Takes a stream name as parameter, and an optional " \
                       "-x for XPath filtering")
    cmdopts.add_option("--get-schemata",
                       dest="get_schemata",
                       help="Takes a directory in which to store retrieved "
                       "schemata")
    cmdopts.add_option("--rpc", dest="rpc", default="",
                       help="Takes a filename (or '-' for standard input) as "\
                       " argument. The contents of the file"\
                       " is a single NETCONF rpc operation (w/o the surrounding"\
                       " <rpc>).")

    (o, args) = parser.parse_args(args=sys_args)
    return (o, args, parser)
Пример #32
0
def main(builtinParameters = {}):
    # Bump the GIL check interval, its more important to get any one thread to a
    # blocking operation (hopefully exec) than to try and unblock other threads.
    #
    # FIXME: This is a hack.
    sys.setcheckinterval(1000)

    global options
    from optparse import OptionParser, OptionGroup
    parser = OptionParser("usage: %prog [options] {file-or-path}")

    parser.add_option("-j", "--threads", dest="numThreads", metavar="N",
                      help="Number of testing threads",
                      type=int, action="store", default=None)
    parser.add_option("", "--config-prefix", dest="configPrefix",
                      metavar="NAME", help="Prefix for 'lit' config files",
                      action="store", default=None)
    parser.add_option("", "--param", dest="userParameters",
                      metavar="NAME=VAL",
                      help="Add 'NAME' = 'VAL' to the user defined parameters",
                      type=str, action="append", default=[])

    group = OptionGroup(parser, "Output Format")
    # FIXME: I find these names very confusing, although I like the
    # functionality.
    group.add_option("-q", "--quiet", dest="quiet",
                     help="Suppress no error output",
                     action="store_true", default=False)
    group.add_option("-s", "--succinct", dest="succinct",
                     help="Reduce amount of output",
                     action="store_true", default=False)
    group.add_option("-v", "--verbose", dest="showOutput",
                     help="Show all test output",
                     action="store_true", default=False)
    group.add_option("", "--no-progress-bar", dest="useProgressBar",
                     help="Do not use curses based progress bar",
                     action="store_false", default=True)
    parser.add_option_group(group)

    group = OptionGroup(parser, "Test Execution")
    group.add_option("", "--path", dest="path",
                     help="Additional paths to add to testing environment",
                     action="append", type=str, default=[])
    group.add_option("", "--vg", dest="useValgrind",
                     help="Run tests under valgrind",
                     action="store_true", default=False)
    group.add_option("", "--vg-leak", dest="valgrindLeakCheck",
                     help="Check for memory leaks under valgrind",
                     action="store_true", default=False)
    group.add_option("", "--vg-arg", dest="valgrindArgs", metavar="ARG",
                     help="Specify an extra argument for valgrind",
                     type=str, action="append", default=[])
    group.add_option("", "--time-tests", dest="timeTests",
                     help="Track elapsed wall time for each test",
                     action="store_true", default=False)
    group.add_option("", "--no-execute", dest="noExecute",
                     help="Don't execute any tests (assume PASS)",
                     action="store_true", default=False)
    parser.add_option_group(group)

    group = OptionGroup(parser, "Test Selection")
    group.add_option("", "--max-tests", dest="maxTests", metavar="N",
                     help="Maximum number of tests to run",
                     action="store", type=int, default=None)
    group.add_option("", "--max-time", dest="maxTime", metavar="N",
                     help="Maximum time to spend testing (in seconds)",
                     action="store", type=float, default=None)
    group.add_option("", "--shuffle", dest="shuffle",
                     help="Run tests in random order",
                     action="store_true", default=False)
    group.add_option("", "--filter", dest="filter", metavar="REGEX",
                     help=("Only run tests with paths matching the given "
                           "regular expression"),
                     action="store", default=None)
    parser.add_option_group(group)

    group = OptionGroup(parser, "Debug and Experimental Options")
    group.add_option("", "--debug", dest="debug",
                      help="Enable debugging (for 'lit' development)",
                      action="store_true", default=False)
    group.add_option("", "--show-suites", dest="showSuites",
                      help="Show discovered test suites",
                      action="store_true", default=False)
    group.add_option("", "--show-tests", dest="showTests",
                      help="Show all discovered tests",
                      action="store_true", default=False)
    parser.add_option_group(group)

    (opts, args) = parser.parse_args()

    if not args:
        parser.error('No inputs specified')

    if opts.numThreads is None:
# Python <2.5 has a race condition causing lit to always fail with numThreads>1
# http://bugs.python.org/issue1731717
# I haven't seen this bug occur with 2.5.2 and later, so only enable multiple
# threads by default there.
       if sys.hexversion >= 0x2050200:
               opts.numThreads = lit.util.detectCPUs()
       else:
               opts.numThreads = 1

    inputs = args

    # Create the user defined parameters.
    userParams = dict(builtinParameters)
    for entry in opts.userParameters:
        if '=' not in entry:
            name,val = entry,''
        else:
            name,val = entry.split('=', 1)
        userParams[name] = val

    # Create the global config object.
    litConfig = lit.LitConfig.LitConfig(
        progname = os.path.basename(sys.argv[0]),
        path = opts.path,
        quiet = opts.quiet,
        useValgrind = opts.useValgrind,
        valgrindLeakCheck = opts.valgrindLeakCheck,
        valgrindArgs = opts.valgrindArgs,
        noExecute = opts.noExecute,
        debug = opts.debug,
        isWindows = (platform.system()=='Windows'),
        params = userParams,
        config_prefix = opts.configPrefix)

    tests = lit.discovery.find_tests_for_inputs(litConfig, inputs)

    if opts.showSuites or opts.showTests:
        # Aggregate the tests by suite.
        suitesAndTests = {}
        for t in tests:
            if t.suite not in suitesAndTests:
                suitesAndTests[t.suite] = []
            suitesAndTests[t.suite].append(t)
        suitesAndTests = list(suitesAndTests.items())
        suitesAndTests.sort(key = lambda item: item[0].name)

        # Show the suites, if requested.
        if opts.showSuites:
            print('-- Test Suites --')
            for ts,ts_tests in suitesAndTests:
                print('  %s - %d tests' %(ts.name, len(ts_tests)))
                print('    Source Root: %s' % ts.source_root)
                print('    Exec Root  : %s' % ts.exec_root)

        # Show the tests, if requested.
        if opts.showTests:
            print('-- Available Tests --')
            for ts,ts_tests in suitesAndTests:
                ts_tests.sort(key = lambda test: test.path_in_suite)
                for test in ts_tests:
                    print('  %s' % (test.getFullName(),))

        # Exit.
        sys.exit(0)

    # Select and order the tests.
    numTotalTests = len(tests)

    # First, select based on the filter expression if given.
    if opts.filter:
        try:
            rex = re.compile(opts.filter)
        except:
            parser.error("invalid regular expression for --filter: %r" % (
                    opts.filter))
        tests = [t for t in tests
                 if rex.search(t.getFullName())]

    # Then select the order.
    if opts.shuffle:
        random.shuffle(tests)
    else:
        tests.sort(key = lambda t: t.getFullName())

    # Finally limit the number of tests, if desired.
    if opts.maxTests is not None:
        tests = tests[:opts.maxTests]

    # Don't create more threads than tests.
    opts.numThreads = min(len(tests), opts.numThreads)

    extra = ''
    if len(tests) != numTotalTests:
        extra = ' of %d' % numTotalTests
    header = '-- Testing: %d%s tests, %d threads --'%(len(tests),extra,
                                                      opts.numThreads)

    progressBar = None
    if not opts.quiet:
        if opts.succinct and opts.useProgressBar:
            try:
                tc = lit.ProgressBar.TerminalController()
                progressBar = lit.ProgressBar.ProgressBar(tc, header)
            except ValueError:
                print(header)
                progressBar = lit.ProgressBar.SimpleProgressBar('Testing: ')
        else:
            print(header)

    startTime = time.time()
    display = TestingProgressDisplay(opts, len(tests), progressBar)
    provider = TestProvider(tests, opts.maxTime)

    try:
      import win32api
    except ImportError:
      pass
    else:
      def console_ctrl_handler(type):
        provider.cancel()
        return True
      win32api.SetConsoleCtrlHandler(console_ctrl_handler, True)

    runTests(opts.numThreads, litConfig, provider, display)
    display.finish()

    if not opts.quiet:
        print('Testing Time: %.2fs'%(time.time() - startTime))

    # Update results for any tests which weren't run.
    for test in tests:
        if test.result is None:
            test.setResult(lit.Test.Result(lit.Test.UNRESOLVED, '', 0.0))

    # List test results organized by kind.
    hasFailures = False
    byCode = {}
    for test in tests:
        if test.result.code not in byCode:
            byCode[test.result.code] = []
        byCode[test.result.code].append(test)
        if test.result.code.isFailure:
            hasFailures = True

    # Print each test in any of the failing groups.
    for title,code in (('Unexpected Passing Tests', lit.Test.XPASS),
                       ('Failing Tests', lit.Test.FAIL),
                       ('Unresolved Tests', lit.Test.UNRESOLVED)):
        elts = byCode.get(code)
        if not elts:
            continue
        print('*'*20)
        print('%s (%d):' % (title, len(elts)))
        for test in elts:
            print('    %s' % test.getFullName())
        sys.stdout.write('\n')

    if opts.timeTests and tests:
        # Order by time.
        test_times = [(test.getFullName(), test.result.elapsed)
                      for test in tests]
        lit.util.printHistogram(test_times, title='Tests')
    
    for name,code in (('Expected Passes    ', lit.Test.PASS),
                      ('Expected Failures  ', lit.Test.XFAIL),
                      ('Unsupported Tests  ', lit.Test.UNSUPPORTED),
                      ('Unresolved Tests   ', lit.Test.UNRESOLVED),
                      ('Unexpected Passes  ', lit.Test.XPASS),
                      ('Unexpected Failures', lit.Test.FAIL),):
        if opts.quiet and not code.isFailure:
            continue
        N = len(byCode.get(code,[]))
        if N:
            print('  %s: %d' % (name,N))

    # If we encountered any additional errors, exit abnormally.
    if litConfig.numErrors:
        sys.stderr.write('\n%d error(s), exiting.\n' % litConfig.numErrors)
        sys.exit(2)

    # Warn about warnings.
    if litConfig.numWarnings:
        sys.stderr.write('\n%d warning(s) in tests.\n' % litConfig.numWarnings)

    if hasFailures:
        sys.exit(1)
    sys.exit(0)
Пример #33
0
def cmdLineParser(argv=None):
    """
    This function parses the command line parameters and arguments
    """

    if not argv:
        argv = sys.argv

    checkSystemEncoding()

    _ = getUnicode(os.path.basename(argv[0]),
                   encoding=sys.getfilesystemencoding())

    usage = "%s%s [options]" % ("python " if not IS_WIN else "", \
            "\"%s\"" % _ if " " in _ else _)

    parser = OptionParser(usage=usage)

    try:
        parser.add_option("--hh",
                          dest="advancedHelp",
                          action="store_true",
                          help="Show advanced help message and exit")

        parser.add_option("--version",
                          dest="showVersion",
                          action="store_true",
                          help="Show program's version number and exit")

        parser.add_option("-v",
                          dest="verbose",
                          type="int",
                          help="Verbosity level: 0-6 (default %d)" %
                          defaults.verbose)

        # Target options
        target = OptionGroup(
            parser, "Target", "At least one of these "
            "options has to be provided to define the target(s)")

        target.add_option("-d",
                          dest="direct",
                          help="Connection string "
                          "for direct database connection")

        target.add_option(
            "-u",
            "--url",
            dest="url",
            help="Target URL (e.g. \"http://www.site.com/vuln.php?id=1\")")

        target.add_option("-l",
                          dest="logFile",
                          help="Parse target(s) from Burp "
                          "or WebScarab proxy log file")

        target.add_option(
            "-x",
            dest="sitemapUrl",
            help="Parse target(s) from remote sitemap(.xml) file")

        target.add_option("-m",
                          dest="bulkFile",
                          help="Scan multiple targets given "
                          "in a textual file ")

        target.add_option("-r",
                          dest="requestFile",
                          help="Load HTTP request from a file")

        target.add_option("-g",
                          dest="googleDork",
                          help="Process Google dork results as target URLs")

        target.add_option("-c",
                          dest="configFile",
                          help="Load options from a configuration INI file")

        # Request options
        request = OptionGroup(
            parser, "Request", "These options can be used "
            "to specify how to connect to the target URL")

        request.add_option("--method",
                           dest="method",
                           help="Force usage of given HTTP method (e.g. PUT)")

        request.add_option("--data",
                           dest="data",
                           help="Data string to be sent through POST")

        request.add_option(
            "--param-del",
            dest="paramDel",
            help="Character used for splitting parameter values")

        request.add_option("--cookie",
                           dest="cookie",
                           help="HTTP Cookie header value")

        request.add_option("--cookie-del",
                           dest="cookieDel",
                           help="Character used for splitting cookie values")

        request.add_option(
            "--load-cookies",
            dest="loadCookies",
            help="File containing cookies in Netscape/wget format")

        request.add_option("--drop-set-cookie",
                           dest="dropSetCookie",
                           action="store_true",
                           help="Ignore Set-Cookie header from response")

        request.add_option("--user-agent",
                           dest="agent",
                           help="HTTP User-Agent header value")

        request.add_option(
            "--random-agent",
            dest="randomAgent",
            action="store_true",
            help="Use randomly selected HTTP User-Agent header value")

        request.add_option("--host",
                           dest="host",
                           help="HTTP Host header value")

        request.add_option("--referer",
                           dest="referer",
                           help="HTTP Referer header value")

        request.add_option(
            "-H",
            "--header",
            dest="header",
            help="Extra header (e.g. \"X-Forwarded-For: 127.0.0.1\")")

        request.add_option(
            "--headers",
            dest="headers",
            help="Extra headers (e.g. \"Accept-Language: fr\\nETag: 123\")")

        request.add_option("--auth-type",
                           dest="authType",
                           help="HTTP authentication type "
                           "(Basic, Digest, NTLM or PKI)")

        request.add_option("--auth-cred",
                           dest="authCred",
                           help="HTTP authentication credentials "
                           "(name:password)")

        request.add_option(
            "--auth-file",
            dest="authFile",
            help="HTTP authentication PEM cert/private key file")

        request.add_option("--ignore-401",
                           dest="ignore401",
                           action="store_true",
                           help="Ignore HTTP Error 401 (Unauthorized)")

        request.add_option("--proxy",
                           dest="proxy",
                           help="Use a proxy to connect to the target URL")

        request.add_option("--proxy-cred",
                           dest="proxyCred",
                           help="Proxy authentication credentials "
                           "(name:password)")

        request.add_option("--proxy-file",
                           dest="proxyFile",
                           help="Load proxy list from a file")

        request.add_option("--ignore-proxy",
                           dest="ignoreProxy",
                           action="store_true",
                           help="Ignore system default proxy settings")

        request.add_option("--tor",
                           dest="tor",
                           action="store_true",
                           help="Use Tor anonymity network")

        request.add_option("--tor-port",
                           dest="torPort",
                           help="Set Tor proxy port other than default")

        request.add_option(
            "--tor-type",
            dest="torType",
            help="Set Tor proxy type (HTTP (default), SOCKS4 or SOCKS5)")

        request.add_option("--check-tor",
                           dest="checkTor",
                           action="store_true",
                           help="Check to see if Tor is used properly")

        request.add_option("--delay",
                           dest="delay",
                           type="float",
                           help="Delay in seconds between each HTTP request")

        request.add_option("--timeout",
                           dest="timeout",
                           type="float",
                           help="Seconds to wait before timeout connection "
                           "(default %d)" % defaults.timeout)

        request.add_option("--retries",
                           dest="retries",
                           type="int",
                           help="Retries when the connection timeouts "
                           "(default %d)" % defaults.retries)

        request.add_option("--randomize",
                           dest="rParam",
                           help="Randomly change value for given parameter(s)")

        request.add_option(
            "--safe-url",
            dest="safeUrl",
            help="URL address to visit frequently during testing")

        request.add_option("--safe-post",
                           dest="safePost",
                           help="POST data to send to a safe URL")

        request.add_option("--safe-req",
                           dest="safeReqFile",
                           help="Load safe HTTP request from a file")

        request.add_option(
            "--safe-freq",
            dest="safeFreq",
            type="int",
            help="Test requests between two visits to a given safe URL")

        request.add_option("--skip-urlencode",
                           dest="skipUrlEncode",
                           action="store_true",
                           help="Skip URL encoding of payload data")

        request.add_option("--csrf-token",
                           dest="csrfToken",
                           help="Parameter used to hold anti-CSRF token")

        request.add_option(
            "--csrf-url",
            dest="csrfUrl",
            help="URL address to visit to extract anti-CSRF token")

        request.add_option("--force-ssl",
                           dest="forceSSL",
                           action="store_true",
                           help="Force usage of SSL/HTTPS")

        request.add_option("--hpp",
                           dest="hpp",
                           action="store_true",
                           help="Use HTTP parameter pollution method")

        request.add_option(
            "--eval",
            dest="evalCode",
            help=
            "Evaluate provided Python code before the request (e.g. \"import hashlib;id2=hashlib.md5(id).hexdigest()\")"
        )

        # Optimization options
        optimization = OptionGroup(
            parser, "Optimization", "These "
            "options can be used to optimize the "
            "performance of sqlmap")

        optimization.add_option("-o",
                                dest="optimize",
                                action="store_true",
                                help="Turn on all optimization switches")

        optimization.add_option("--predict-output",
                                dest="predictOutput",
                                action="store_true",
                                help="Predict common queries output")

        optimization.add_option("--keep-alive",
                                dest="keepAlive",
                                action="store_true",
                                help="Use persistent HTTP(s) connections")

        optimization.add_option(
            "--null-connection",
            dest="nullConnection",
            action="store_true",
            help="Retrieve page length without actual HTTP response body")

        optimization.add_option("--threads",
                                dest="threads",
                                type="int",
                                help="Max number of concurrent HTTP(s) "
                                "requests (default %d)" % defaults.threads)

        # Injection options
        injection = OptionGroup(
            parser, "Injection", "These options can be "
            "used to specify which parameters to test "
            "for, provide custom injection payloads and "
            "optional tampering scripts")

        injection.add_option("-p",
                             dest="testParameter",
                             help="Testable parameter(s)")

        injection.add_option("--skip",
                             dest="skip",
                             help="Skip testing for given parameter(s)")

        injection.add_option(
            "--skip-static",
            dest="skipStatic",
            action="store_true",
            help="Skip testing parameters that not appear dynamic")

        injection.add_option("--dbms",
                             dest="dbms",
                             help="Force back-end DBMS to this value")

        injection.add_option(
            "--dbms-cred",
            dest="dbmsCred",
            help="DBMS authentication credentials (user:password)")

        injection.add_option("--os",
                             dest="os",
                             help="Force back-end DBMS operating system "
                             "to this value")

        injection.add_option("--invalid-bignum",
                             dest="invalidBignum",
                             action="store_true",
                             help="Use big numbers for invalidating values")

        injection.add_option(
            "--invalid-logical",
            dest="invalidLogical",
            action="store_true",
            help="Use logical operations for invalidating values")

        injection.add_option("--invalid-string",
                             dest="invalidString",
                             action="store_true",
                             help="Use random strings for invalidating values")

        injection.add_option("--no-cast",
                             dest="noCast",
                             action="store_true",
                             help="Turn off payload casting mechanism")

        injection.add_option("--no-escape",
                             dest="noEscape",
                             action="store_true",
                             help="Turn off string escaping mechanism")

        injection.add_option("--prefix",
                             dest="prefix",
                             help="Injection payload prefix string")

        injection.add_option("--suffix",
                             dest="suffix",
                             help="Injection payload suffix string")

        injection.add_option(
            "--tamper",
            dest="tamper",
            help="Use given script(s) for tampering injection data")

        # Detection options
        detection = OptionGroup(
            parser, "Detection", "These options can be "
            "used to customize the detection phase")

        detection.add_option("--level",
                             dest="level",
                             type="int",
                             help="Level of tests to perform (1-5, "
                             "default %d)" % defaults.level)

        detection.add_option("--risk",
                             dest="risk",
                             type="int",
                             help="Risk of tests to perform (1-3, "
                             "default %d)" % defaults.level)

        detection.add_option("--string",
                             dest="string",
                             help="String to match when "
                             "query is evaluated to True")

        detection.add_option("--not-string",
                             dest="notString",
                             help="String to match when "
                             "query is evaluated to False")

        detection.add_option("--regexp",
                             dest="regexp",
                             help="Regexp to match when "
                             "query is evaluated to True")

        detection.add_option("--code",
                             dest="code",
                             type="int",
                             help="HTTP code to match when "
                             "query is evaluated to True")

        detection.add_option(
            "--text-only",
            dest="textOnly",
            action="store_true",
            help="Compare pages based only on the textual content")

        detection.add_option("--titles",
                             dest="titles",
                             action="store_true",
                             help="Compare pages based only on their titles")

        # Techniques options
        techniques = OptionGroup(
            parser, "Techniques", "These options can be "
            "used to tweak testing of specific SQL "
            "injection techniques")

        techniques.add_option("--technique",
                              dest="tech",
                              help="SQL injection techniques to use "
                              "(default \"%s\")" % defaults.tech)

        techniques.add_option("--time-sec",
                              dest="timeSec",
                              type="int",
                              help="Seconds to delay the DBMS response "
                              "(default %d)" % defaults.timeSec)

        techniques.add_option(
            "--union-cols",
            dest="uCols",
            help="Range of columns to test for UNION query SQL injection")

        techniques.add_option(
            "--union-char",
            dest="uChar",
            help="Character to use for bruteforcing number of columns")

        techniques.add_option(
            "--union-from",
            dest="uFrom",
            help="Table to use in FROM part of UNION query SQL injection")

        techniques.add_option(
            "--dns-domain",
            dest="dnsName",
            help="Domain name used for DNS exfiltration attack")

        techniques.add_option(
            "--second-order",
            dest="secondOrder",
            help="Resulting page URL searched for second-order "
            "response")

        # Fingerprint options
        fingerprint = OptionGroup(parser, "Fingerprint")

        fingerprint.add_option(
            "-f",
            "--fingerprint",
            dest="extensiveFp",
            action="store_true",
            help="Perform an extensive DBMS version fingerprint")

        # Enumeration options
        enumeration = OptionGroup(
            parser, "Enumeration", "These options can "
            "be used to enumerate the back-end database "
            "management system information, structure "
            "and data contained in the tables. Moreover "
            "you can run your own SQL statements")

        enumeration.add_option("-a",
                               "--all",
                               dest="getAll",
                               action="store_true",
                               help="Retrieve everything")

        enumeration.add_option("-b",
                               "--banner",
                               dest="getBanner",
                               action="store_true",
                               help="Retrieve DBMS banner")

        enumeration.add_option("--current-user",
                               dest="getCurrentUser",
                               action="store_true",
                               help="Retrieve DBMS current user")

        enumeration.add_option("--current-db",
                               dest="getCurrentDb",
                               action="store_true",
                               help="Retrieve DBMS current database")

        enumeration.add_option("--hostname",
                               dest="getHostname",
                               action="store_true",
                               help="Retrieve DBMS server hostname")

        enumeration.add_option("--is-dba",
                               dest="isDba",
                               action="store_true",
                               help="Detect if the DBMS current user is DBA")

        enumeration.add_option("--users",
                               dest="getUsers",
                               action="store_true",
                               help="Enumerate DBMS users")

        enumeration.add_option("--passwords",
                               dest="getPasswordHashes",
                               action="store_true",
                               help="Enumerate DBMS users password hashes")

        enumeration.add_option("--privileges",
                               dest="getPrivileges",
                               action="store_true",
                               help="Enumerate DBMS users privileges")

        enumeration.add_option("--roles",
                               dest="getRoles",
                               action="store_true",
                               help="Enumerate DBMS users roles")

        enumeration.add_option("--dbs",
                               dest="getDbs",
                               action="store_true",
                               help="Enumerate DBMS databases")

        enumeration.add_option("--tables",
                               dest="getTables",
                               action="store_true",
                               help="Enumerate DBMS database tables")

        enumeration.add_option("--columns",
                               dest="getColumns",
                               action="store_true",
                               help="Enumerate DBMS database table columns")

        enumeration.add_option("--schema",
                               dest="getSchema",
                               action="store_true",
                               help="Enumerate DBMS schema")

        enumeration.add_option("--count",
                               dest="getCount",
                               action="store_true",
                               help="Retrieve number of entries for table(s)")

        enumeration.add_option("--dump",
                               dest="dumpTable",
                               action="store_true",
                               help="Dump DBMS database table entries")

        enumeration.add_option("--dump-all",
                               dest="dumpAll",
                               action="store_true",
                               help="Dump all DBMS databases tables entries")

        enumeration.add_option(
            "--search",
            dest="search",
            action="store_true",
            help="Search column(s), table(s) and/or database name(s)")

        enumeration.add_option("--comments",
                               dest="getComments",
                               action="store_true",
                               help="Retrieve DBMS comments")

        enumeration.add_option("-D",
                               dest="db",
                               help="DBMS database to enumerate")

        enumeration.add_option("-T",
                               dest="tbl",
                               help="DBMS database table(s) to enumerate")

        enumeration.add_option(
            "-C",
            dest="col",
            help="DBMS database table column(s) to enumerate")

        enumeration.add_option(
            "-X",
            dest="excludeCol",
            help="DBMS database table column(s) to not enumerate")

        enumeration.add_option("-U",
                               dest="user",
                               help="DBMS user to enumerate")

        enumeration.add_option("--exclude-sysdbs",
                               dest="excludeSysDbs",
                               action="store_true",
                               help="Exclude DBMS system databases when "
                               "enumerating tables")

        enumeration.add_option("--where",
                               dest="dumpWhere",
                               help="Use WHERE condition while table dumping")

        enumeration.add_option("--start",
                               dest="limitStart",
                               type="int",
                               help="First query output entry to retrieve")

        enumeration.add_option("--stop",
                               dest="limitStop",
                               type="int",
                               help="Last query output entry to retrieve")

        enumeration.add_option(
            "--first",
            dest="firstChar",
            type="int",
            help="First query output word character to retrieve")

        enumeration.add_option(
            "--last",
            dest="lastChar",
            type="int",
            help="Last query output word character to retrieve")

        enumeration.add_option("--sql-query",
                               dest="query",
                               help="SQL statement to be executed")

        enumeration.add_option("--sql-shell",
                               dest="sqlShell",
                               action="store_true",
                               help="Prompt for an interactive SQL shell")

        enumeration.add_option(
            "--sql-file",
            dest="sqlFile",
            help="Execute SQL statements from given file(s)")

        # Brute force options
        brute = OptionGroup(
            parser, "Brute force", "These "
            "options can be used to run brute force "
            "checks")

        brute.add_option("--common-tables",
                         dest="commonTables",
                         action="store_true",
                         help="Check existence of common tables")

        brute.add_option("--common-columns",
                         dest="commonColumns",
                         action="store_true",
                         help="Check existence of common columns")

        # User-defined function options
        udf = OptionGroup(
            parser, "User-defined function injection", "These "
            "options can be used to create custom user-defined "
            "functions")

        udf.add_option("--udf-inject",
                       dest="udfInject",
                       action="store_true",
                       help="Inject custom user-defined functions")

        udf.add_option("--shared-lib",
                       dest="shLib",
                       help="Local path of the shared library")

        # File system options
        filesystem = OptionGroup(
            parser, "File system access", "These options "
            "can be used to access the back-end database "
            "management system underlying file system")

        filesystem.add_option("--file-read",
                              dest="rFile",
                              help="Read a file from the back-end DBMS "
                              "file system")

        filesystem.add_option("--file-write",
                              dest="wFile",
                              help="Write a local file on the back-end "
                              "DBMS file system")

        filesystem.add_option("--file-dest",
                              dest="dFile",
                              help="Back-end DBMS absolute filepath to "
                              "write to")

        # Takeover options
        takeover = OptionGroup(
            parser, "Operating system access", "These "
            "options can be used to access the back-end "
            "database management system underlying "
            "operating system")

        takeover.add_option("--os-cmd",
                            dest="osCmd",
                            help="Execute an operating system command")

        takeover.add_option("--os-shell",
                            dest="osShell",
                            action="store_true",
                            help="Prompt for an interactive operating "
                            "system shell")

        takeover.add_option("--os-pwn",
                            dest="osPwn",
                            action="store_true",
                            help="Prompt for an OOB shell, "
                            "Meterpreter or VNC")

        takeover.add_option("--os-smbrelay",
                            dest="osSmb",
                            action="store_true",
                            help="One click prompt for an OOB shell, "
                            "Meterpreter or VNC")

        takeover.add_option("--os-bof",
                            dest="osBof",
                            action="store_true",
                            help="Stored procedure buffer overflow "
                            "exploitation")

        takeover.add_option("--priv-esc",
                            dest="privEsc",
                            action="store_true",
                            help="Database process user privilege escalation")

        takeover.add_option("--msf-path",
                            dest="msfPath",
                            help="Local path where Metasploit Framework "
                            "is installed")

        takeover.add_option("--tmp-path",
                            dest="tmpPath",
                            help="Remote absolute path of temporary files "
                            "directory")

        # Windows registry options
        windows = OptionGroup(
            parser, "Windows registry access", "These "
            "options can be used to access the back-end "
            "database management system Windows "
            "registry")

        windows.add_option("--reg-read",
                           dest="regRead",
                           action="store_true",
                           help="Read a Windows registry key value")

        windows.add_option("--reg-add",
                           dest="regAdd",
                           action="store_true",
                           help="Write a Windows registry key value data")

        windows.add_option("--reg-del",
                           dest="regDel",
                           action="store_true",
                           help="Delete a Windows registry key value")

        windows.add_option("--reg-key",
                           dest="regKey",
                           help="Windows registry key")

        windows.add_option("--reg-value",
                           dest="regVal",
                           help="Windows registry key value")

        windows.add_option("--reg-data",
                           dest="regData",
                           help="Windows registry key value data")

        windows.add_option("--reg-type",
                           dest="regType",
                           help="Windows registry key value type")

        # General options
        general = OptionGroup(
            parser, "General", "These options can be used "
            "to set some general working parameters")

        #general.add_option("-x", dest="xmlFile",
        #                    help="Dump the data into an XML file")

        general.add_option("-s",
                           dest="sessionFile",
                           help="Load session from a stored (.sqlite) file")

        general.add_option("-t",
                           dest="trafficFile",
                           help="Log all HTTP traffic into a "
                           "textual file")

        general.add_option(
            "--batch",
            dest="batch",
            action="store_true",
            help="Never ask for user input, use the default behaviour")

        general.add_option(
            "--charset",
            dest="charset",
            help="Force character encoding used for data retrieval")

        general.add_option(
            "--crawl",
            dest="crawlDepth",
            type="int",
            help="Crawl the website starting from the target URL")

        general.add_option(
            "--crawl-exclude",
            dest="crawlExclude",
            help="Regexp to exclude pages from crawling (e.g. \"logout\")")

        general.add_option("--csv-del",
                           dest="csvDel",
                           help="Delimiting character used in CSV output "
                           "(default \"%s\")" % defaults.csvDel)

        general.add_option(
            "--dump-format",
            dest="dumpFormat",
            help="Format of dumped data (CSV (default), HTML or SQLITE)")

        general.add_option("--eta",
                           dest="eta",
                           action="store_true",
                           help="Display for each output the "
                           "estimated time of arrival")

        general.add_option("--flush-session",
                           dest="flushSession",
                           action="store_true",
                           help="Flush session files for current target")

        general.add_option("--forms",
                           dest="forms",
                           action="store_true",
                           help="Parse and test forms on target URL")

        general.add_option("--fresh-queries",
                           dest="freshQueries",
                           action="store_true",
                           help="Ignore query results stored in session file")

        general.add_option("--hex",
                           dest="hexConvert",
                           action="store_true",
                           help="Use DBMS hex function(s) for data retrieval")

        general.add_option("--output-dir",
                           dest="outputDir",
                           action="store",
                           help="Custom output directory path")

        general.add_option(
            "--parse-errors",
            dest="parseErrors",
            action="store_true",
            help="Parse and display DBMS error messages from responses")

        general.add_option("--pivot-column",
                           dest="pivotColumn",
                           help="Pivot column name")

        general.add_option("--save",
                           dest="saveConfig",
                           help="Save options to a configuration INI file")

        general.add_option(
            "--scope",
            dest="scope",
            help="Regexp to filter targets from provided proxy log")

        general.add_option(
            "--test-filter",
            dest="testFilter",
            help="Select tests by payloads and/or titles (e.g. ROW)")

        general.add_option(
            "--test-skip",
            dest="testSkip",
            help="Skip tests by payloads and/or titles (e.g. BENCHMARK)")

        general.add_option("--update",
                           dest="updateAll",
                           action="store_true",
                           help="Update sqlmap")

        # Miscellaneous options
        miscellaneous = OptionGroup(parser, "Miscellaneous")

        miscellaneous.add_option(
            "-z",
            dest="mnemonics",
            help="Use short mnemonics (e.g. \"flu,bat,ban,tec=EU\")")

        miscellaneous.add_option(
            "--alert",
            dest="alert",
            help="Run host OS command(s) when SQL injection is found")

        miscellaneous.add_option(
            "--answers",
            dest="answers",
            help="Set question answers (e.g. \"quit=N,follow=N\")")

        miscellaneous.add_option(
            "--beep",
            dest="beep",
            action="store_true",
            help="Beep on question and/or when SQL injection is found")

        miscellaneous.add_option("--cleanup",
                                 dest="cleanup",
                                 action="store_true",
                                 help="Clean up the DBMS from sqlmap specific "
                                 "UDF and tables")

        miscellaneous.add_option(
            "--dependencies",
            dest="dependencies",
            action="store_true",
            help="Check for missing (non-core) sqlmap dependencies")

        miscellaneous.add_option("--disable-coloring",
                                 dest="disableColoring",
                                 action="store_true",
                                 help="Disable console output coloring")

        miscellaneous.add_option(
            "--gpage",
            dest="googlePage",
            type="int",
            help="Use Google dork results from specified page number")

        miscellaneous.add_option(
            "--identify-waf",
            dest="identifyWaf",
            action="store_true",
            help="Make a thorough testing for a WAF/IPS/IDS protection")

        miscellaneous.add_option(
            "--skip-waf",
            dest="skipWaf",
            action="store_true",
            help="Skip heuristic detection of WAF/IPS/IDS protection")

        miscellaneous.add_option(
            "--mobile",
            dest="mobile",
            action="store_true",
            help="Imitate smartphone through HTTP User-Agent header")

        miscellaneous.add_option(
            "--offline",
            dest="offline",
            action="store_true",
            help="Work in offline mode (only use session data)")

        miscellaneous.add_option(
            "--page-rank",
            dest="pageRank",
            action="store_true",
            help="Display page rank (PR) for Google dork results")

        miscellaneous.add_option(
            "--purge-output",
            dest="purgeOutput",
            action="store_true",
            help="Safely remove all content from output directory")

        miscellaneous.add_option(
            "--smart",
            dest="smart",
            action="store_true",
            help="Conduct thorough tests only if positive heuristic(s)")

        miscellaneous.add_option("--sqlmap-shell",
                                 dest="sqlmapShell",
                                 action="store_true",
                                 help="Prompt for an interactive sqlmap shell")

        miscellaneous.add_option(
            "--wizard",
            dest="wizard",
            action="store_true",
            help="Simple wizard interface for beginner users")

        # Hidden and/or experimental options
        parser.add_option("--dummy",
                          dest="dummy",
                          action="store_true",
                          help=SUPPRESS_HELP)

        parser.add_option("--pickled-options",
                          dest="pickledOptions",
                          help=SUPPRESS_HELP)

        parser.add_option("--disable-precon",
                          dest="disablePrecon",
                          action="store_true",
                          help=SUPPRESS_HELP)

        parser.add_option("--profile",
                          dest="profile",
                          action="store_true",
                          help=SUPPRESS_HELP)

        parser.add_option("--binary-fields",
                          dest="binaryFields",
                          help=SUPPRESS_HELP)

        parser.add_option("--cpu-throttle",
                          dest="cpuThrottle",
                          type="int",
                          help=SUPPRESS_HELP)

        parser.add_option("--force-dns",
                          dest="forceDns",
                          action="store_true",
                          help=SUPPRESS_HELP)

        parser.add_option("--force-threads",
                          dest="forceThreads",
                          action="store_true",
                          help=SUPPRESS_HELP)

        parser.add_option("--smoke-test",
                          dest="smokeTest",
                          action="store_true",
                          help=SUPPRESS_HELP)

        parser.add_option("--live-test",
                          dest="liveTest",
                          action="store_true",
                          help=SUPPRESS_HELP)

        parser.add_option("--stop-fail",
                          dest="stopFail",
                          action="store_true",
                          help=SUPPRESS_HELP)

        parser.add_option("--run-case", dest="runCase", help=SUPPRESS_HELP)

        parser.add_option_group(target)
        parser.add_option_group(request)
        parser.add_option_group(optimization)
        parser.add_option_group(injection)
        parser.add_option_group(detection)
        parser.add_option_group(techniques)
        parser.add_option_group(fingerprint)
        parser.add_option_group(enumeration)
        parser.add_option_group(brute)
        parser.add_option_group(udf)
        parser.add_option_group(filesystem)
        parser.add_option_group(takeover)
        parser.add_option_group(windows)
        parser.add_option_group(general)
        parser.add_option_group(miscellaneous)

        # Dirty hack to display longer options without breaking into two lines
        def _(self, *args):
            _ = parser.formatter._format_option_strings(*args)
            if len(_) > MAX_HELP_OPTION_LENGTH:
                _ = ("%%.%ds.." % (MAX_HELP_OPTION_LENGTH -
                                   parser.formatter.indent_increment)) % _
            return _

        parser.formatter._format_option_strings = parser.formatter.format_option_strings
        parser.formatter.format_option_strings = type(
            parser.formatter.format_option_strings)(_, parser, type(parser))

        # Dirty hack for making a short option -hh
        option = parser.get_option("--hh")
        option._short_opts = ["-hh"]
        option._long_opts = []

        # Dirty hack for inherent help message of switch -h
        option = parser.get_option("-h")
        option.help = option.help.capitalize().replace("this help",
                                                       "basic help")

        _ = []
        prompt = False
        advancedHelp = True
        extraHeaders = []

        for arg in argv:
            _.append(getUnicode(arg, encoding=sys.getfilesystemencoding()))

        argv = _
        checkDeprecatedOptions(argv)

        prompt = "--sqlmap-shell" in argv

        if prompt:
            parser.usage = ""
            cmdLineOptions.sqlmapShell = True

            _ = ["x", "q", "exit", "quit", "clear"]

            for option in parser.option_list:
                _.extend(option._long_opts)
                _.extend(option._short_opts)

            for group in parser.option_groups:
                for option in group.option_list:
                    _.extend(option._long_opts)
                    _.extend(option._short_opts)

            autoCompletion(AUTOCOMPLETE_TYPE.SQLMAP, commands=_)

            while True:
                command = None

                try:
                    command = raw_input("sqlmap-shell> ").strip()
                    command = getUnicode(command, encoding=sys.stdin.encoding)
                except (KeyboardInterrupt, EOFError):
                    print
                    raise SqlmapShellQuitException

                if not command:
                    continue
                elif command.lower() == "clear":
                    clearHistory()
                    print "[i] history cleared"
                    saveHistory(AUTOCOMPLETE_TYPE.SQLMAP)
                elif command.lower() in ("x", "q", "exit", "quit"):
                    raise SqlmapShellQuitException
                elif command[0] != '-':
                    print "[!] invalid option(s) provided"
                    print "[i] proper example: '-u http://www.site.com/vuln.php?id=1 --banner'"
                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, ex:
                raise SqlmapSyntaxException, "something went wrong during command line parsing ('%s')" % ex.message

        # Hide non-basic options in basic help case
        for i in xrange(len(argv)):
            if argv[i] == "-hh":
                argv[i] = "-h"
            elif re.search(r"\A-\w=.+", argv[i]):
                print "[!] potentially miswritten (illegal '=') short option detected ('%s')" % argv[
                    i]
            elif argv[i] == "-H":
                if i + 1 < len(argv):
                    extraHeaders.append(argv[i + 1])
            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] == "-h":
                advancedHelp = False
                for group in parser.option_groups[:]:
                    found = False
                    for option in group.option_list:
                        if option.dest not in BASIC_HELP_ITEMS:
                            option.help = SUPPRESS_HELP
                        else:
                            found = True
                    if not found:
                        parser.option_groups.remove(group)

        try:
            (args, _) = parser.parse_args(argv)
        except UnicodeEncodeError, ex:
            print "\n[!] %s" % ex.object.encode("unicode-escape")
            raise SystemExit
Пример #34
0
def main():
    sys_log = logging.handlers.SysLogHandler(SYSLOG_DEV)
    sys_log.setLevel(logging.DEBUG)
    sys_log.setFormatter(logging.Formatter(SYS_FORMAT))
    log.setLevel(logging.DEBUG)
    log.addHandler(sys_log)

    from optparse import OptionParser, OptionGroup
    global lib
    parser = OptionParser()
    for name, optionset in [
        ('SIP Registration', [
            ('username', 'Username with which to register with proxy', None),
            ('password', 'Password with which to register with proxy', None),
            ('proxy',
             'Proxy server (often an IP address, domain name is fine too), port can be specified with :5060 after the server name',
             None),
        ]),
        ('RTP Audio', [
            ('number',
             'Phone Number, if provided, attempt to make a call to this number, should be an Echo() application.  Note: this will be formatted as sip:<number>@<proxy>;user=phone',
             None),
            ('wav',
             'Wave file to play to the Echo application (frequency analysis of this file should == capture of the call to the Echo application)',
             DEFAULT_TEST_FILE),
        ]),
    ]:
        group = OptionGroup(parser, name)
        for (option, description, default) in optionset:
            group.add_option('--%s' % (option),
                             dest=option.replace('-', '_'),
                             default=default,
                             help=description)
        parser.add_option_group(group)
    options, args = parser.parse_args()
    lib = start_pj()
    try:
        acc, acc_cb = register_account(lib, options.proxy, options.username,
                                       options.password)
        info = acc.info()
        print 'Status=%s (%s)' % (info.reg_status, info.reg_reason)
        if info.reg_status != 200:
            log.error(
                "Unable to register in %s seconds: %s %s",
                REGISTER_TIMEOUT,
                info.reg_status,
                info.reg_status,
            )
            if info.reg_status in (404, ):
                log.error(
                    "Registration failed due to unknown account/username (Note: your service provider should have reported a 403 error (Forbidden) but reported a 404 error (Unknown) instead, this allows hostile systems to scan for accounts)."
                )
                returncode = EXIT_CODE_UNKNOWN_USER
            elif info.reg_status in (401, 403):
                log.error(
                    "Registration failed due to authorization failure (account/username incorrect)"
                )
                returncode = EXIT_CODE_BAD_PASSWORD
            elif info.reg_status == 100:
                host = check_proxy(options.proxy)
                if host:
                    # TODO: nmap -sU -p 5060 to see if the UDP port is open|filtered, but that
                    # requires su access to perform the scan...
                    log.error('Registration failed to host: %s (%s)',
                              options.proxy, host)
                    returncode = EXIT_CODE_CONNECT_FAILURE_HOST_GOOD
                else:
                    log.error("Unable to resolve host: %s", options.proxy)
                    returncode = EXIT_CODE_CONNECT_FAILURE_HOST_BAD
            else:
                returncode = EXIT_CODE_REG_FAILURE
        else:
            if options.number:
                tempdir = tempfile.mkdtemp('rec', 'siptest')
                try:
                    record_file = os.path.join(tempdir, 'recording.wav')

                    call_callback = make_call(
                        lib,
                        acc,
                        'sip:%s@%s;user=phone' % (
                            options.number,
                            options.proxy,
                        ),
                        record_file=record_file,
                        play_file=options.wav,
                    )
                    if call_callback.confirmed:
                        first, second = audio_stats(
                            options.wav), audio_stats(record_file)
                        if abs(first['rough frequency'] -
                               second['rough frequency']
                               ) > MAX_FREQUENCY_DELTA:
                            if second['maximum amplitude'] == second[
                                    'minimum amplitude'] == 0.0:
                                log.error(
                                    "Silence recorded, likely the RTP (audio) transport is blocked"
                                )
                                returncode = EXIT_CODE_SILENCE_RECORDED
                            else:
                                log.error(
                                    "Frequencies do not match, possibly redirected to the wrong extension or a server not configured with an Echo() application? %s != %s",
                                    first,
                                    second,
                                )
                                returncode = EXIT_CODE_FREQUENCY_MISMATCH_FAILURE
                        else:
                            returncode = EXIT_CODE_SUCCESS
                finally:
                    shutil.rmtree(tempdir, True)
            else:
                returncode = EXIT_CODE_SUCCESS
        acc.delete()
        lib.destroy()
        lib = None
        return returncode

    except Exception, e:
        log.error(
            "Exception: %s",
            traceback.format_exc(),
        )
        if lib:
            lib.destroy()
        lib = None
Пример #35
0
def make_parser():
    usage_message = "%prog [options] FILEPATH"

    parser = OptionParser(
        usage=usage_message,
        version="%prog {:s}\nhttps://github.com/fireeye/flare-floss/".format(
            version.__version__))

    parser.add_option("-n",
                      "--minimum-length",
                      dest="min_length",
                      help="minimum string length (default is %d)" %
                      MIN_STRING_LENGTH_DEFAULT)
    parser.add_option(
        "-f",
        "--functions",
        dest="functions",
        help="only analyze the specified functions (comma-separated)",
        type="string")
    parser.add_option(
        "--save-workspace",
        dest="save_workspace",
        help="save vivisect .viv workspace file in current directory",
        action="store_true")
    parser.add_option("-m",
                      "--show-metainfo",
                      dest="should_show_metainfo",
                      help="display vivisect workspace meta information",
                      action="store_true")
    parser.add_option(
        "--no-filter",
        dest="no_filter",
        help=
        "do not filter deobfuscated strings (may result in many false positive strings)",
        action="store_true")
    parser.add_option(
        "--max-instruction-count",
        dest="max_instruction_count",
        type=int,
        default=20000,
        help=
        "maximum number of instructions to emulate per function (default is 20000)"
    )
    parser.add_option(
        "--max-address-revisits",
        dest="max_address_revisits",
        type=int,
        default=0,
        help="maximum number of address revisits per function (default is 0)")

    shellcode_group = OptionGroup(
        parser, "Shellcode options",
        "Analyze raw binary file containing shellcode")
    shellcode_group.add_option("-s",
                               "--shellcode",
                               dest="is_shellcode",
                               help="analyze shellcode",
                               action="store_true")
    shellcode_group.add_option("-e",
                               "--shellcode_ep",
                               dest="shellcode_entry_point",
                               help="shellcode entry point",
                               type="string")
    shellcode_group.add_option("-b",
                               "--shellcode_base",
                               dest="shellcode_base",
                               help="shellcode base offset",
                               type="string")
    parser.add_option_group(shellcode_group)

    extraction_group = OptionGroup(
        parser, "Extraction options",
        "Specify which string types FLOSS shows from a file, "
        "by default all types are shown")
    extraction_group.add_option(
        "--no-static-strings",
        dest="no_static_strings",
        action="store_true",
        help="do not show static ASCII and UTF-16 strings")
    extraction_group.add_option("--no-decoded-strings",
                                dest="no_decoded_strings",
                                action="store_true",
                                help="do not show decoded strings")
    extraction_group.add_option("--no-stack-strings",
                                dest="no_stack_strings",
                                action="store_true",
                                help="do not show stackstrings")
    parser.add_option_group(extraction_group)

    format_group = OptionGroup(parser, "Format Options")
    format_group.add_option(
        "-g",
        "--group",
        dest="group_functions",
        help="group output by virtual address of decoding functions",
        action="store_true")
    format_group.add_option(
        "-q",
        "--quiet",
        dest="quiet",
        action="store_true",
        help="suppress headers and formatting to print only extracted strings")
    parser.add_option_group(format_group)

    logging_group = OptionGroup(parser, "Logging Options")
    logging_group.add_option("-v",
                             "--verbose",
                             dest="verbose",
                             help="show verbose messages and warnings",
                             action="store_true")
    logging_group.add_option("-d",
                             "--debug",
                             dest="debug",
                             help="show all trace messages",
                             action="store_true")
    parser.add_option_group(logging_group)

    output_group = OptionGroup(parser, "Script output options")
    output_group.add_option(
        "-i",
        "--ida",
        dest="ida_python_file",
        help=
        "create an IDAPython script to annotate the decoded strings in an IDB file"
    )
    output_group.add_option(
        "--x64dbg",
        dest="x64dbg_database_file",
        help=
        "create a x64dbg database/json file to annotate the decoded strings in x64dbg"
    )
    output_group.add_option(
        "-r",
        "--radare",
        dest="radare2_script_file",
        help=
        "create a radare2 script to annotate the decoded strings in an .r2 file"
    )
    output_group.add_option(
        "-j",
        "--binja",
        dest="binja_script_file",
        help=
        "create a Binary Ninja script to annotate the decoded strings in a BNDB file"
    )
    parser.add_option_group(output_group)

    identification_group = OptionGroup(parser, "Identification Options")
    identification_group.add_option(
        "-p",
        "--plugins",
        dest="plugins",
        help="apply the specified identification plugins only (comma-separated)"
    )
    identification_group.add_option(
        "-l",
        "--list-plugins",
        dest="list_plugins",
        help="list all available identification plugins and exit",
        action="store_true")
    parser.add_option_group(identification_group)

    profile_group = OptionGroup(parser, "FLOSS Profiles")
    profile_group.add_option(
        "-x",
        "--expert",
        dest="expert",
        help=
        "show duplicate offset/string combinations, save workspace, group function output",
        action="store_true")
    parser.add_option_group(profile_group)

    return parser
Пример #36
0
class Options(dict):
    """
    Wrapper for OptionParser - handles loading options from YAML,
    prompting for missing required options, and handling switches
    for appending or subtracting to default option values.
    """
    def __init__(self, yaml_path, **kwargs):
        """
        Set up args and OptionParser.
        """
        # Load YAML file and create OptionParser.
        with open(yaml_path) as f:
            data = load(f)
        kwargs["usage"] = "usage: %prog [options]"
        kwargs["epilog"] = data["epilog"]
        existing = kwargs.pop("existing")
        self.parser = OptionParser(**kwargs)

        # Set Options specific attributes.
        self.appendable = data["appendable"]
        self.append_option = data["append option"]
        self.subtract_option = data["subtract option"]
        self.defaults = data["defaults"]

        # Add each OptionGroup.
        formatting = {"appendable": ", ".join(data["appendable"])}
        for option_group in data["options"]:
            for name, options in option_group.items():
                group = OptionGroup(self.parser, name)
                for option in options:
                    formatting["default"] = self.defaults.get(option["dest"])
                    formatting["choices"] = "|".join(option.get("choices", []))
                    if option.get("action") == "store_true":
                        option["default"] = False
                    elif option.get("action") == "store_false":
                        option["default"] = True
                    option["help"] %= formatting
                    group.add_option(*option.pop("args"), **option)
                self.parser.add_option_group(group)
        self.defaults.update(existing)

    def append(self, option, value):
        """
        Append the value to the default in append mode.
        """
        default = self.defaults.get(option.dest)
        if value is None or default is None:
            return value
        if option.type == "string" and not value.startswith(","):
            value = "," + value
        return default + value

    def subtract(self, option, value):
        """
        Subtract the value from the default in subtract mode.
        """
        default = self.defaults.get(option.dest)
        if value is None or default is None:
            return value
        if option.type == "string" and not value.startswith(","):
            default = set(default.split(","))
            value = set(value.split(","))
            return ",".join(default - value)
        else:
            return default - value

    def all_options(self):
        """
        Flatten all options for all groups.
        """
        return [o for g in self.parser.option_groups for o in g.option_list]

    def parse_args(self):
        """
        Call OptionParser's parse_args() and handle defaults, append,
        subtract and prompting for missing options.
        """
        parsed, _ = self.parser.parse_args()
        final = {}
        append = getattr(parsed, self.append_option)
        subtract = getattr(parsed, self.subtract_option)
        for option in self.all_options():
            name = option.dest
            if name is not None:
                value = getattr(parsed, name)
                default = self.defaults.get(name)
                if append and option.get_opt_string() in self.appendable:
                    value = self.append(option, value)
                elif subtract and option.get_opt_string() in self.appendable:
                    value = self.subtract(option, value)
                if value is None:
                    value = default
                if value is None:
                    value = raw_input("Please enter '%s': " % option.help)
                self[name] = value
        return self

    def __str__(self):
        """
        Format the options.
        """
        formatted = []
        padding = len(max(self.keys(), key=len)) + 5
        for option in self.all_options():
            name = (option.dest + ": ").ljust(padding, ".")
            value = self[option.dest]
            formatted.append("%s %s" % (name, value))
        return "\n".join(formatted)
Пример #37
0
                  default=10)
parser.add_option("--max-cov",
                  dest="max",
                  help="An input file with precomputed coverage thresholds")
parser.add_option(
    "--base-quality-threshold",
    dest="b",
    help=
    "The Base-quality threshold for Qualities encoded in Sanger format (Illumina 1.8 format)",
    default=15)
parser.add_option("--coding",
                  dest="c",
                  help="the Illumina FASTQ quality coding",
                  default=1.8)

parser.add_option_group(group)
(options, args) = parser.parse_args()

################################### functions ######################################


def load_data(x):
    ''' import data either from a gzipped or or uncrompessed file or from STDIN'''
    import gzip
    if x == "-":
        y = sys.stdin
    elif x.endswith(".gz"):
        y = gzip.open(x, "r")
    else:
        y = open(x, "r")
    return y
Пример #38
0
    def parseArguments(self):
        usage = "Usage: %prog [-u|--url] target [-e|--extensions] extensions [options]"
        parser = OptionParser(usage,
                              epilog='''
You can change the dirsearch default configurations (default extensions, timeout, wordlist location, ...) by editing the "default.conf" file. More information at https://github.com/maurosoria/dirsearch.                          
''')

        # Mandatory arguments
        mandatory = OptionGroup(parser, 'Mandatory')
        mandatory.add_option('-u',
                             '--url',
                             help='URL target',
                             action='store',
                             type='string',
                             dest='url',
                             default=None)
        mandatory.add_option('-l',
                             '--url-list',
                             help='URL list target',
                             action='store',
                             type='string',
                             dest='urlList',
                             default=None)
        mandatory.add_option(
            '-e',
            '--extensions',
            help='Extensions list separated by comma (Example: php,asp)',
            action='store',
            dest='extensions',
            default=None)

        mandatory.add_option('-E',
                             '--extensions-list',
                             help='Use predefined list of common extensions',
                             action='store_true',
                             dest='defaultExtensions',
                             default=False)
        mandatory.add_option(
            '-X',
            '--exclude-extensions',
            help=
            'Exclude extensions list, separated by comma (Example: asp,jsp)',
            action='store',
            dest='excludeExtensions',
            default=None)

        # Dictionary Settings
        dictionary = OptionGroup(parser, 'Dictionary Settings')
        dictionary.add_option('-w',
                              '--wordlist',
                              action='store',
                              dest='wordlist',
                              help='Customize wordlist (separated by comma)',
                              default=self.wordlist)
        dictionary.add_option(
            '--prefixes',
            action='store',
            dest='prefixes',
            default=None,
            help='Add custom prefixes to all entries (separated by comma)')
        dictionary.add_option(
            '--suffixes',
            action='store',
            dest='suffixes',
            default=None,
            help=
            'Add custom suffixes to all entries, ignores directories (separated by comma)'
        )

        dictionary.add_option(
            '-f',
            '--force-extensions',
            action='store_true',
            dest='forceExtensions',
            default=self.forceExtensions,
            help=
            'Force extensions for every wordlist entry. Add %NOFORCE% at the end of the entry in the wordlist that you do not want to force'
        )
        dictionary.add_option(
            '--no-extension',
            dest='noExtension',
            action='store_true',
            help=
            'Remove extensions in all wordlist entries (Example: admin.php -> admin)'
        )
        dictionary.add_option(
            '--no-dot-extensions',
            dest='noDotExtensions',
            default=self.noDotExtensions,
            help='Remove the "." character before extensions',
            action='store_true')

        dictionary.add_option('-C',
                              '--capitalization',
                              action='store_true',
                              dest='capitalization',
                              default=self.capitalization,
                              help='Capital wordlist')
        dictionary.add_option('-U',
                              '--uppercase',
                              action='store_true',
                              dest='uppercase',
                              default=self.uppercase,
                              help='Uppercase wordlist')
        dictionary.add_option('-L',
                              '--lowercase',
                              action='store_true',
                              dest='lowercase',
                              default=self.lowercase,
                              help='Lowercase wordlist')

        # Optional Settings
        general = OptionGroup(parser, 'General Settings')
        general.add_option('-d',
                           '--data',
                           help='HTTP request data (POST, PUT, ... body)',
                           action='store',
                           dest='data',
                           type='str',
                           default=None)
        general.add_option('-r',
                           '--recursive',
                           help='Bruteforce recursively',
                           action='store_true',
                           dest='recursive',
                           default=self.recursive)
        general.add_option(
            '-R',
            '--recursive-level-max',
            help=
            'Max recursion level (subdirs) (Default: 1 [only rootdir + 1 dir])',
            action='store',
            type='int',
            dest='recursive_level_max',
            default=self.recursive_level_max)

        general.add_option('--suppress-empty',
                           action='store_true',
                           dest='suppressEmpty',
                           help='Suppress empty responses')
        general.add_option('--minimal',
                           action='store',
                           dest='minimumResponseSize',
                           type='int',
                           default=None,
                           help='Minimal response length')
        general.add_option('--maximal',
                           action='store',
                           dest='maximumResponseSize',
                           type='int',
                           default=None,
                           help='Maximal response length')

        general.add_option(
            '--scan-subdir',
            '--scan-subdirs',
            help='Scan subdirectories of the given URL (separated by comma)',
            action='store',
            dest='scanSubdirs',
            default=None)
        general.add_option(
            '--exclude-subdir',
            '--exclude-subdirs',
            help=
            'Exclude the following subdirectories during recursive scan (separated by comma)',
            action='store',
            dest='excludeSubdirs',
            default=self.excludeSubdirs)
        general.add_option('-t',
                           '--threads',
                           help='Number of Threads',
                           action='store',
                           type='int',
                           dest='threadsCount',
                           default=self.threadsCount)

        general.add_option(
            '-i',
            '--include-status',
            help=
            'Show only included status codes, separated by comma (example: 301, 500)',
            action='store',
            dest='includeStatusCodes',
            default=self.includeStatusCodes)
        general.add_option(
            '-x',
            '--exclude-status',
            help=
            'Do not show excluded status codes, separated by comma (example: 301, 500)',
            action='store',
            dest='excludeStatusCodes',
            default=self.excludeStatusCodes)
        general.add_option(
            '--exclude-texts',
            help=
            'Exclude responses by texts, separated by comma (example: "Not found", "Error")',
            action='store',
            dest='excludeTexts',
            default=None)

        general.add_option(
            '--exclude-regexps',
            help=
            'Exclude responses by regexps, separated by comma (example: "Not foun[a-z]{1}", "^Error$")',
            action='store',
            dest='excludeRegexps',
            default=None)
        general.add_option('-c',
                           '--cookie',
                           action='store',
                           type='string',
                           dest='cookie',
                           default=None)
        general.add_option('--user-agent',
                           action='store',
                           type='string',
                           dest='useragent',
                           default=self.useragent)

        general.add_option('-F',
                           '--follow-redirects',
                           action='store_true',
                           dest='noFollowRedirects',
                           default=self.redirect)
        general.add_option(
            '-H',
            '--header',
            help=
            'HTTP request headers, support multiple flags (example: --header "Referer: example.com")',
            action='append',
            type='string',
            dest='headers',
            default=None)
        general.add_option('--full-url',
                           action='store_true',
                           dest='full_url',
                           help='Print the full URL in the output')

        general.add_option('--random-agents',
                           '--random-user-agents',
                           action='store_true',
                           dest='useRandomAgents')
        general.add_option('-q',
                           '--quite-mode',
                           action='store_true',
                           dest='clean_view')

        # Connection Settings
        connection = OptionGroup(parser, 'Connection Settings')
        connection.add_option('--timeout',
                              action='store',
                              dest='timeout',
                              type='int',
                              default=self.timeout,
                              help='Connection timeout')
        connection.add_option('--ip',
                              action='store',
                              dest='ip',
                              default=None,
                              help='Resolve name to IP address')
        connection.add_option(
            '-s',
            '--delay',
            help='Delay between requests (support float number)',
            action='store',
            dest='delay',
            type='float',
            default=self.delay)

        connection.add_option('--proxy',
                              '--http-proxy',
                              action='store',
                              dest='httpProxy',
                              type='string',
                              default=self.proxy,
                              help='HTTP Proxy (example: localhost:8080)')
        connection.add_option('--proxylist',
                              '--http-proxy-list',
                              action='store',
                              dest='proxyList',
                              type='string',
                              default=self.proxylist,
                              help='File containg HTTP proxy servers')
        connection.add_option('-m',
                              '--http-method',
                              action='store',
                              dest='httpmethod',
                              type='string',
                              default=self.httpmethod,
                              help='HTTP method, default: GET')

        connection.add_option('--max-retries',
                              action='store',
                              dest='maxRetries',
                              type='int',
                              default=self.maxRetries)
        connection.add_option(
            '-b',
            '--request-by-hostname',
            help=
            'By default dirsearch will request by IP for speed. This will force requests by hostname',
            action='store_true',
            dest='requestByHostname',
            default=self.requestByHostname)

        # Report Settings
        reports = OptionGroup(parser, 'Reports')
        reports.add_option('--simple-report',
                           action='store',
                           help='Only found paths',
                           dest='simpleOutputFile',
                           default=None)
        reports.add_option('--plain-text-report',
                           action='store',
                           help='Found paths with status codes',
                           dest='plainTextOutputFile',
                           default=None)
        reports.add_option('--json-report',
                           action='store',
                           dest='jsonOutputFile',
                           default=None)

        parser.add_option_group(mandatory)
        parser.add_option_group(dictionary)
        parser.add_option_group(general)
        parser.add_option_group(connection)
        parser.add_option_group(reports)
        options, arguments = parser.parse_args()
        return options
Пример #39
0
    def cmd_parser():
        """
        command line parser, parses all of Zeus's arguments and flags
        """
        parser = OptionParser(usage="./zeus.py -d|r|l|f|b DORK|FILE|URL [ATTACKS] [--OPTS]")

        # mandatory options
        mandatory = OptionGroup(parser, "Mandatory Options",
                                "These options have to be used in order for Zeus to run")

        mandatory.add_option("-d", "--dork", dest="dorkToUse", metavar="DORK",
                             help="Specify a singular Google dork to use for queries")

        mandatory.add_option("-l", "--dork-list", dest="dorkFileToUse", metavar="FILE-PATH",
                             help="Specify a file full of dorks to run through")

        mandatory.add_option("-r", "--rand-dork", dest="useRandomDork", action="store_true",
                             help="Use a random dork from the etc/dorks.txt file to perform the scan")

        mandatory.add_option("-b", "--blackwidow", dest="spiderWebSite", metavar="URL",
                             help="Spider a single webpage for all available URL's")

        mandatory.add_option("-f", "--url-file", dest="fileToEnumerate", metavar="FILE-PATH",
                             help="Run an attack on URL's in a given file")

        # being worked on
        # TODO:/
        mandatory.add_option("-u", "--url", dest="singleTargetRecon", metavar="URL",
                             help=SUPPRESS_HELP)

        # attack options
        attacks = OptionGroup(parser, "Attack arguments",
                              "These arguments will give you the choice on how you want to check the websites")

        attacks.add_option("-s", "--sqli", dest="runSqliScan", action="store_true",
                           help="Run a Sqlmap SQLi scan on the discovered URL's")

        attacks.add_option("-p", "--port-scan", dest="runPortScan", action="store_true",
                           help="Run a Nmap port scan on the discovered URL's")

        attacks.add_option("-a", "--admin-panel", dest="adminPanelFinder", action="store_true",
                           help="Search for the websites admin panel")

        attacks.add_option("-x", "--xss-scan", dest="runXssScan", action="store_true",
                           help="Run an XSS scan on the found URL's")

        attacks.add_option("-w", "--whois-lookup", dest="performWhoisLookup", action="store_true",
                           help="Perform a WhoIs lookup on the provided domain")

        attacks.add_option("-c", "--clickjacking", dest="performClickjackingScan", action="store_true",
                           help="Perform a clickjacking scan on a provided URL")

        # being worked on
        # TODO:/
        attacks.add_option("-g", "--github-search", dest="searchGithub", action="store_true",
                           help=SUPPRESS_HELP)

        attacks.add_option("-P", "--pgp", dest="pgpLookup", action="store_true",
                           help="Perform a PGP public key lookup on the found URLs")

        attacks.add_option("--sqlmap-args", dest="sqlmapArguments", metavar="SQLMAP-ARGS",
                           help="Pass the arguments to send to the sqlmap API within quotes & "
                                "separated by a comma. IE 'dbms mysql, verbose 3, level 5'")

        attacks.add_option("--sqlmap-conf", dest="sqlmapConfigFile", metavar="CONFIG-FILE-PATH",
                           help="Pass a configuration file that contains the sqlmap arguments")

        attacks.add_option("--nmap-args", dest="nmapArguments", metavar="NMAP-ARGS",
                           help="Pass the arguments to send to the nmap API within quotes & "
                                "separated by a pipe. IE '-O|-p 445, 1080'")

        attacks.add_option("--show-sqlmap", dest="showSqlmapArguments", action="store_true",
                           help="Show the arguments that the sqlmap API understands")

        attacks.add_option("--show-nmap", dest="showNmapArgs", action="store_true",
                           help="Show the arguments that nmap understands")

        attacks.add_option("--show-possibles", dest="showAllConnections", action="store_true",
                           help="Show all connections made during the admin panel search")

        attacks.add_option("--tamper", dest="tamperXssPayloads", metavar="TAMPER-SCRIPT",
                           help="Send the XSS payloads through tampering before sending to the target")

        # being worked on
        # TODO:/
        attacks.add_option("--thread", dest="threadPanels", action="store_true",
                           help=SUPPRESS_HELP)

        attacks.add_option("--auto", dest="autoStartSqlmap", action="store_true",
                           help="Automatically start the sqlmap API (or at least try to)")

        # search engine options
        engines = OptionGroup(parser, "Search engine arguments",
                              "Arguments to change the search engine used (default is Google)")

        engines.add_option("-D", "--search-engine-ddg", dest="useDDG", action="store_true",
                           help="Use DuckDuckGo as the search engine")

        engines.add_option("-B", "--search-engine-bing", dest="useBing", action="store_true",
                           help="Use Bing as the search engine")

        engines.add_option("-A", "--search-engine-aol", dest="useAOL", action="store_true",
                           help="Use AOL as the search engine")

        # arguments to edit your search patterns
        search_items = OptionGroup(parser, "Search options",
                                   "Arguments that will control the search criteria")

        search_items.add_option("-L", "--links", dest="amountToSearch", type=int, metavar="HOW-MANY-LINKS",
                                help="Specify how many links to try and search on Google")

        search_items.add_option("-M", "--multi", dest="searchMultiplePages", action="store_true",
                                help="Search multiple pages of Google")

        search_items.add_option("-E", "--exclude-none", dest="noExclude", action="store_true",
                                help="Do not exclude URLs because they do not have a GET(query) parameter in them")

        search_items.add_option("-W", "--webcache", dest="parseWebcache", action="store_true",
                                help="Parse webcache URLs for the redirect in them")

        search_items.add_option("--x-forward", dest="forwardedForRandomIP", action="store_true",
                                help="Add a header called 'X-Forwarded-For' with three random IP addresses")

        search_items.add_option("--time-sec", dest="controlTimeout", metavar="SECONDS", type=int,
                                help="Control the sleep and timeout times in relevant situations")

        search_items.add_option("--identify-waf", dest="identifyProtection", action="store_true",
                                help="Attempt to identify if the target is protected by some kind of "
                                     "WAF/IDS/IPS")

        # being worked on
        # TODO:/
        search_items.add_option("--force-ssl", dest="forceSSL", action="store_true",
                                help=SUPPRESS_HELP)

        search_items.add_option("--identify-plugins", dest="identifyPlugin", action="store_true",
                                help="Attempt to identify what plugins the target is using")

        # obfuscation options
        anon = OptionGroup(parser, "Anonymity arguments",
                           "Arguments that help with anonymity and hiding identity")

        anon.add_option("--proxy", dest="proxyConfig", metavar="PROXY-STRING",
                        help="Use a proxy to do the scraping, will not auto configure to the API's")

        anon.add_option("--proxy-file", dest="proxyFileRand", metavar="FILE-PATH",
                        help="Grab a random proxy from a given file of proxies")

        anon.add_option("--random-agent", dest="useRandomAgent", action="store_true",
                        help="Use a random user-agent from the etc/agents.txt file")

        anon.add_option("--agent", dest="usePersonalAgent", metavar="USER-AGENT",
                        help="Use your own personal user-agent"),

        anon.add_option("--tor", dest="useTor", action="store_true",
                        help="Use Tor connection as the proxy and set the firefox browser settings to mimic Tor")

        # miscellaneous options
        misc = OptionGroup(parser, "Misc Options",
                           "These options affect how the program will run")

        misc.add_option("--verbose", dest="runInVerbose", action="store_true",
                        help="Run the application in verbose mode (more output)")

        misc.add_option("--batch", dest="runInBatch", action="store_true",
                        help="Skip the questions and run in default batch mode")

        misc.add_option("--update", dest="updateZeus", action="store_true",
                        help="Update to the latest development version")

        misc.add_option("--hide", dest="hideBanner", action="store_true",
                        help="Hide the banner during running")

        misc.add_option("--version", dest="showCurrentVersion", action="store_true",
                        help="Show the current version and exit")

        # being worked on
        # TODO:/
        misc.add_option("-T", "--x-threads", dest="amountOfThreads", metavar="THREAD-AMOUNT", type=int,
                        help=SUPPRESS_HELP)

        misc.add_option("--show-success", dest="showSuccessRate", action="store_true",
                        help="Calculate the dorks success rate and output the calculation in human readable form")

        misc.add_option("--show-description", dest="showPluginDescription", action="store_true",
                        help="Show the description of the identified plugins")

        parser.add_option_group(mandatory)
        parser.add_option_group(attacks)
        parser.add_option_group(search_items)
        parser.add_option_group(anon)
        parser.add_option_group(engines)
        parser.add_option_group(misc)

        opt, _ = parser.parse_args()
        return opt
Пример #40
0
        help='Directory to download and compare files in.')
optional_group.add_option('--url', action='store', dest='url', default=None,
                    help='URL to fetch ROOT files from. File search is recursive ' +
                    'for links in given URL.')
optional_group.add_option('--no-url', action='store_true', dest='no_url', default=False,
                    help='Search for files in DIR (specified by --dir option), ' +
                    'do NOT browse for files online.')
optional_group.add_option('--db', action='store', dest='db_name', default=None,
        help='SQLite3 .db filename to use for the comparison. Default: auto-generated SQLite3 .db file.')
optional_group.add_option('--cl', action='store_true', dest='clear_db', default=False,
                    help='Clean DB before comparison.')
optional_group.add_option('--dry', action='store_true', dest='dry', default=False,
                    help='Do not download or compare files, just show the progress.')
optional_group.add_option('--html', action='store_true', dest='html', default=False,
                    help='Generate static html. Default: %default.')
parser.add_option_group(optional_group)


def call_compare_using_files(args):
    file1, file2, work_path, db_name, clear_db = args
    command = ['./compare_using_files_v2.py', join(work_path, file1), join(work_path, file2), '--db', db_name]
    if clear_db:
        command.append('--cl')
    return call(command)

def partial_db_name(db_name, i):
    """Generates temporary database name."""
    return '%s___%d.db' % (db_name.strip('.db'), i + 1)

def merge_dbs(main_db, partial_db):
    conn = sqlite3.connect(main_db)
Пример #41
0
def createSearchMode():
    parser = OptionParser()

    select_group = OptionGroup(parser, "Select Tests")

    select_group.add_option(
        "--pattern",
        action="store",
        dest="pattern",
        default="",
        help="""\
Execute only tests matching the pattern. Defaults to all tests.""",
    )
    select_group.add_option(
        "--all",
        action="store_true",
        dest="all",
        default=False,
        help="""\
Execute all tests, continue execution even after failure of one.""",
    )

    parser.add_option_group(select_group)

    debug_group = OptionGroup(parser, "Test features")

    debug_group.add_option(
        "--debug",
        action="store_true",
        dest="debug",
        default=False,
        help="""\
Executing all self checks possible to find errors in Nuitka, good for test coverage.
Defaults to off.""",
    )

    parser.add_option_group(debug_group)

    options, positional_args = parser.parse_args()

    # Default to searching.
    mode = positional_args[0] if positional_args else "search"

    # Avoid having to use options style.
    if mode in ("search", "only"):
        if len(positional_args) >= 2 and not options.pattern:
            options.pattern = positional_args[1]

    if mode == "search":
        if options.all:
            return SearchModeAll()
        elif options.pattern:
            pattern = options.pattern.replace("/", os.path.sep)
            return SearchModeByPattern(pattern)
        else:
            return SearchModeImmediate()
    elif mode == "resume":
        return SearchModeResume(sys.modules["__main__"].__file__)
    elif mode == "only":
        if options.pattern:
            pattern = options.pattern.replace("/", os.path.sep)
            return SearchModeOnly(pattern)
        else:
            assert False
    elif mode == "coverage":
        return SearchModeCoverage()
    else:
        assert False
Пример #42
0
def main():
    print("Welcome to Intron-X!")

    # 0. parse command line options
    parser = OptionParser()
    parser.add_option("--intron_table",
                      "-i",
                      dest="intron_table",
                      default=None,
                      help="The intron table file for intron information.")
    parser.add_option("--fasta",
                      "-f",
                      dest="fasta_file",
                      default=None,
                      help="The fasta file of genome sequence.")
    parser.add_option("--out_dir",
                      "-o",
                      dest="out_dir",
                      default=None,
                      help="The directory for output [default: $fasta].")

    group = OptionGroup(parser, "Optional arguments")
    group.add_option("--kmer-range",
                     type="int",
                     nargs=2,
                     dest="kmer_range",
                     default=[1, 3],
                     help=("The min and max K in k-mers. [default: 1 3]"))
    group.add_option("--no-RNAfold",
                     action="store_true",
                     dest="no_RNAfold",
                     default=False,
                     help="No second strucutre for intron sequences")
    group.add_option("--no-weblogo",
                     action="store_true",
                     dest="no_weblogo",
                     default=False,
                     help="No weblogo figures for motifs")
    parser.add_option_group(group)

    (options, args) = parser.parse_args()
    if len(sys.argv[1:]) == 0:
        print("use -h or --help for help on argument.")
        sys.exit(1)
    if options.intron_table is None:
        print("Error: need --intron_table for intron information.")
        sys.exit(1)
    else:
        intron_info = np.genfromtxt(options.intron_table,
                                    delimiter="\t",
                                    dtype="str",
                                    skip_header=1)
    if intron_info.shape[1] == 6:
        bp_exist = False
        intron_info = np.append(intron_info, intron_info[:, 5:], axis=1)
        for i in range(intron_info.shape[0]):
            intron_info[i, 6] = str(
                (int(intron_info[i, 4]) + int(intron_info[i, 5])) / 2)
    else:
        bp_exist = True

    if options.fasta_file is None:
        print("Error: need --fasta for fasta file of genome sequence.")
        sys.exit(1)
    else:
        fasta_file = options.fasta_file

    if options.out_dir is None:
        out_dir = os.path.dirname(fasta_file) + "/intronX"
    else:
        out_dir = options.out_dir + "/intronX"

    kmin, kmax = options.kmer_range

    # 1. generate sequences for intron features
    seq_motif = seq_maker(intron_info, fasta_file, out_dir + "/seq/", kmin,
                          kmax)

    # 2. calculate motif scores
    seq_score1 = motif_score(seq_motif["seq_5ss"])
    seq_score2 = motif_score(seq_motif["seq_BPs"])
    seq_score3 = motif_score(seq_motif["seq_3ss"])

    # # 3. calculate secondary structure scores
    if options.no_RNAfold is False:
        fold_score1 = get_RNAfold(out_dir + "/seq/intron_seq.fa",
                                  out_file=None)
        fold_score2 = get_RNAfold(out_dir + "/seq/5ss_BPs_seq.fa",
                                  out_file=None)
        fold_score3 = get_RNAfold(out_dir + "/seq/BPs_3ss_seq.fa",
                                  out_file=None)
    else:
        fold_score1 = ["NA"] * (intron_info.shape[0])
        fold_score2 = ["NA"] * (intron_info.shape[0])
        fold_score3 = ["NA"] * (intron_info.shape[0])

    # 4. save results
    fid = open(out_dir + "/intronX.txt", "w")
    headline = "gene_id\tintron_id\tchrom\tstrand\tstart\tstop\tbp"
    headline += "\tintronL\t5ss_bpL\tbp_3ssL\tDelta_intron\tDelta_5ss_bp"
    headline += "\tDelta_bp_3ss\t5ss_motif\tbp_motif\t3ss_motif"
    for _seq in seq_motif["kmer_lst"]:
        headline += "\t%s" % _seq
    fid.writelines(headline + "\n")

    for i in range(intron_info.shape[0]):
        intronL = int(intron_info[i, 5]) - int(intron_info[i, 4]) + 1
        ss5_bpL = int(intron_info[i, 6]) - int(intron_info[i, 4])
        ss3_bpL = int(intron_info[i, 5]) - int(intron_info[i, 6])
        if intron_info[i, 3] == "-":
            ss5_bpL, ss3_bpL = ss3_bpL, ss5_bpL

        if bp_exist is True:
            aLine = "\t".join(list(intron_info[i, :]))
            aLine += "\t%d\t%d\t%d\t" % (intronL, ss5_bpL, ss3_bpL)
            aLine += "\t".join(
                [fold_score1[i], fold_score2[i], fold_score3[i]])
            aLine += "\t%.2f\t%.2f\t%.2f" % (seq_score1[i], seq_score2[i],
                                             seq_score3[i])
        else:
            intron_info[i, 6] = "NA"
            aLine = "\t".join(list(intron_info[i, :]))
            aLine += "\t%d\tNA\tNA\t" % (intronL)
            aLine += "\t".join([fold_score1[i], "NA", "NA"])
            aLine += "\t%.2f\tNA\t%.2f" % (seq_score1[i], seq_score3[i])

        for j in range(seq_motif["kmer_frq"].shape[1]):
            aLine += "\t%.2e" % seq_motif["kmer_frq"][i, j]
        fid.writelines(aLine + "\n")
    fid.close()

    if bp_exist is False:
        os.remove(out_dir + "/seq/BPs_seq.fa")
        os.remove(out_dir + "/seq/5ss_BPs_seq.fa")
        os.remove(out_dir + "/seq/BPs_3ss_seq.fa")
        if options.no_RNAfold is False:
            os.remove(out_dir + "/seq/5ss_BPs_seq.RNAfold.txt")
            os.remove(out_dir + "/seq/BPs_3ss_seq.RNAfold.txt")

    # 5. generate sequence motifs figures
    Fidx = [-4, -7, -16]
    motifs = ["/5ss_seq", "/BPs_seq", "/3ss_seq"]
    labels = ["5ss_motif", "BP_motif", "3ss_motif"]
    if options.no_weblogo:
        weblogo_idx = []
    elif bp_exist is False:
        weblogo_idx = [0, 2]
    else:
        weblogo_idx = [0, 1, 2]
    for i in weblogo_idx:
        label = motifs[i][1:]
        f1 = out_dir + "/seq/" + motifs[i]
        f2 = out_dir + motifs[i]  #-s large
        bashCommand = "weblogo -f %s.fa -o %s.pdf -F pdf -i %d --resolution 300 -t %s -c classic" % (
            f1, f2, Fidx[i], labels[i])
        process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
        output = process.communicate()[0]

    print("IntronX done!")
Пример #43
0
def main():
    parser = OptionParser(usage="""Usage:
  verify-manifests.py <repository> <flags> [<manifest specifier>, ...]

  This tool verifies md5sum-style manifests against a boar
  repository. The manifest(s) can themselves be fetched from the
  repostory, or given as ordinary files.

  The first non-flag argument must always be a boar repository path or
  URL.

  The source of the manifests must always be specified by giving one
  of the -F, -B and -S flags.

  The remaining non-flag arguments must consist of a list of manifest
  specifiers (or sent on stdin, if --stdin is used). The specifiers
  can be on different forms depending on the chosen source.

Examples:
  If there is a repository in "/var/repo" with a session named
  "MySession" with a manifest under the path "pictures/manifest.md5"
  with the blob id (md5sum) d41d8cd98f00b204e9800998ecf8427e, we could
  verify this manifest by any of the following commands:

    verify-manifests.py /var/repo -B d41d8cd98f00b204e9800998ecf8427e
    verify-manifests.py /var/repo -S "MySession/pictures/manifest.md5"

  If we have a manifest file stored in /home/me/pics-2012.md5, we can
  verify it with this line:

    verify-manifests.py /var/repo -F /home/me/pics-2012.md5""")

    group = OptionGroup(parser, "Manifest source")
    group.add_option("-F",
                     "--files",
                     dest="file_specs",
                     action="store_true",
                     help="Manifest specifiers are local file paths")
    group.add_option("-B",
                     "--blobids",
                     dest="blobid_specs",
                     action="store_true",
                     help="Manifest specifiers are blob ids")
    group.add_option("-S",
                     "--session-paths",
                     dest="spath_specs",
                     action="store_true",
                     help="Manifest specifiers are session paths")
    parser.add_option_group(group)

    parser.add_option(
        "--stdin",
        dest="stdin",
        action="store_true",
        help=
        "Read manifest specifiers from stdin instead of accepting them as arguments."
    )

    (options, args) = parser.parse_args()

    if not args:
        parser.print_help()
        sys.exit(1)

    if (options.file_specs, options.blobid_specs,
            options.spath_specs).count(True) != 1:
        parser.error(
            "You must always specify exactly one specifier flag (-F, -B, -S)")

    repourl = args[0]

    if options.stdin:
        assert len(
            args
        ) == 1, "--stdin cannot be combined with any other manifest specifiers"
        manifest_ids = [line.rstrip('\r\n') for line in sys.stdin.readlines()]
    else:
        manifest_ids = args[1:]

    if not manifest_ids:
        parser.error("You must always give at least one manifest specifier")

    extrepo = ExtRepo(repourl)

    if options.blobid_specs:
        verifier = verify_manifest_by_md5
    elif options.file_specs:
        verifier = verify_manifest_in_file
    elif options.spath_specs:
        verifier = verify_manifest_by_spath

    try:
        for manifest_id in manifest_ids:
            verifier(extrepo, manifest_id)
    except IntegrityError as e:
        print(e)
        print("ERROR WHILE VERIFYING MANIFEST %s" % manifest_id)
        sys.exit(1)
Пример #44
0
def run_command(argv):

    # pylint: disable=too-many-statements

    oparser = OptionParser(usage="usage: elbe control [options] <command>")

    oparser.add_option("--host",
                       dest="host",
                       default=cfg['soaphost'],
                       help="Ip or hostname of elbe-daemon.")

    oparser.add_option("--port",
                       dest="port",
                       default=cfg['soapport'],
                       help="Port of soap itf on elbe-daemon.")

    oparser.add_option("--pass",
                       dest="passwd",
                       default=cfg['elbepass'],
                       help="Password (default is foo).")

    oparser.add_option("--user",
                       dest="user",
                       default=cfg['elbeuser'],
                       help="Username (default is root).")

    oparser.add_option(
        "--retries",
        dest="retries",
        default="10",
        help="How many times to retry the connection to the server before "
        "giving up (default is 10 times, yielding 10 seconds).")

    oparser.add_option(
        "--build-bin",
        action="store_true",
        dest="build_bin",
        default=False,
        help="Build binary repository CDROM, for exact reproduction.")

    oparser.add_option("--build-sources",
                       action="store_true",
                       dest="build_sources",
                       default=False,
                       help="Build source CDROM")

    oparser.add_option(
        "--skip-pbuilder",
        action="store_true",
        dest="skip_pbuilder",
        default=False,
        help="skip pbuilder section of XML (dont build packages)")

    oparser.add_option("--output",
                       dest="output",
                       default=None,
                       help="Output files to <directory>")

    oparser.add_option("--matches",
                       dest="matches",
                       default=False,
                       help="Select files based on wildcard expression.")

    oparser.add_option("--pbuilder-only",
                       action="store_true",
                       dest="pbuilder_only",
                       default=False,
                       help="Only list/download pbuilder Files")

    oparser.add_option(
        "--cpuset",
        default=-1,
        type="int",
        help=
        "Limit cpuset of pbuilder commands (bitmask) (defaults to -1 for all CPUs)"
    )

    oparser.add_option(
        "--profile",
        dest="profile",
        default="",
        help="Make pbuilder commands build the specified profile")

    oparser.add_option("--cross",
                       dest="cross",
                       default=False,
                       action="store_true",
                       help="Creates an environment for crossbuilding if "
                       "combined with create. Combined with build it"
                       " will use this environment.")

    oparser.add_option("--no-ccache",
                       dest="noccache",
                       default=False,
                       action="store_true",
                       help="Deactivates the compiler cache 'ccache'")

    oparser.add_option("--ccache-size",
                       dest="ccachesize",
                       default="10G",
                       action="store",
                       type="string",
                       help="set a limit for the compiler cache size "
                       "(should be a number followed by an optional "
                       "suffix: k, M, G, T. Use 0 for no limit.)")

    devel = OptionGroup(
        oparser, "options for elbe developers",
        "Caution: Don't use these options in a productive environment")
    devel.add_option("--skip-urlcheck",
                     action="store_true",
                     dest="url_validation",
                     default=ValidationMode.CHECK_ALL,
                     help="Skip URL Check inside initvm")

    devel.add_option("--debug",
                     action="store_true",
                     dest="debug",
                     default=False,
                     help="Enable debug mode.")

    devel.add_option("--ignore-version-diff",
                     action="store_true",
                     dest="ignore_version",
                     default=False,
                     help="allow different elbe version on host and initvm")
    oparser.add_option_group(devel)

    (opt, args) = oparser.parse_args(argv)

    if len(args) < 1:
        print("elbe control - no subcommand given", file=sys.stderr)
        ClientAction.print_actions()
        return

    try:
        control = ElbeSoapClient(opt.host,
                                 opt.port,
                                 opt.user,
                                 opt.passwd,
                                 debug=opt.debug,
                                 retries=int(opt.retries))
    except URLError as e:
        print("Failed to connect to Soap server %s:%s\n" %
              (opt.host, opt.port),
              file=sys.stderr)
        print("", file=sys.stderr)
        print("Check, wether the initvm is actually running.", file=sys.stderr)
        print("try 'elbe initvm start'", file=sys.stderr)
        sys.exit(10)
    except socket.error as e:
        print("Failed to connect to Soap server %s:%s\n" %
              (opt.host, opt.port),
              file=sys.stderr)
        print("", file=sys.stderr)
        print("Check, wether the Soap Server is running inside the initvm",
              file=sys.stderr)
        print("try 'elbe initvm attach'", file=sys.stderr)
        sys.exit(10)
    except BadStatusLine as e:
        print("Failed to connect to Soap server %s:%s\n" %
              (opt.host, opt.port),
              file=sys.stderr)
        print("", file=sys.stderr)
        print("Check, wether the initvm is actually running.", file=sys.stderr)
        print("try 'elbe initvm start'", file=sys.stderr)
        sys.exit(10)

    try:
        v_server = control.service.get_version()
        if v_server != elbe_version:
            print("elbe v%s is used in initvm, this is not compatible with "
                  "elbe v%s that is used on this machine. Please install same "
                  "versions of elbe in initvm and on your machine." %
                  (v_server, elbe_version),
                  file=sys.stderr)
            print("To install elbe v%s into the initvm use "
                  "'elbe control install_elbe_version'" % elbe_version)

            if not opt.ignore_version:
                sys.exit(20)
    except AttributeError:
        print("the elbe installation inside the initvm doesn't provide a \
get_version interface. Please create a new initvm or upgrade \
elbe inside the existing initvm.",
              file=sys.stderr)
        if not opt.ignore_version:
            sys.exit(20)

    try:
        action = ClientAction(args[0])
    except KeyError:
        print("elbe control - unknown subcommand", file=sys.stderr)
        ClientAction.print_actions()
        sys.exit(20)

    try:
        action.execute(control, opt, args[1:])
    except WebFault as e:
        print("Server returned error:", file=sys.stderr)
        print("", file=sys.stderr)
        if hasattr(e.fault, 'faultstring'):
            print(e.fault.faultstring, file=sys.stderr)
        else:
            print(e, file=sys.stderr)
        sys.exit(5)
Пример #45
0
        default='CRITICAL')
    parser_debug.add_option('-p',
                            '--printtostdout',
                            action='store_true',
                            default=False,
                            help='Print all log messages to stdout')
    parser_debug.add_option(
        '-l',
        '--logfile',
        type='string',
        metavar='FILE',
        help=('Desired filename of log file output. Default '
              'is "' + defaultlogfilename + '"'),
        default=defaultlogfilename)
    # officially adds the debugging option group
    parser.add_option_group(parser_debug)
    options, args = parser.parse_args()  # here's where the options get parsed

    try:  # now try and get the debugging options
        loglevel = getattr(logging, options.debug)
    except AttributeError:  # set the log level
        loglevel = {
            3: logging.CRITICAL,
            2: logging.ERROR,
            1: logging.WARNING,
            0: logging.INFO,
            -1: logging.DEBUG,
        }[int(options.debug)]

    try:
        open(options.logfile, 'w')  # try and open the default log file
Пример #46
0
def main():
    global pkts_rcvd
    global EOF_rcvd
    global num_acks

    ###########################
    #set up options
    ###########################
    parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
    expert_grp = parser.add_option_group("Expert")
    parser.add_option(
        "-m",
        "--modulation",
        type="choice",
        choices=['bpsk', 'qpsk'],
        default='bpsk',
        help="Select modulation from: bpsk, qpsk [default=%%default]")
    parser.add_option("-v", "--verbose", action="store_true", default=False)
    parser.add_option("-p",
                      "--packets",
                      type="int",
                      default=3000,
                      help="set number of packets to send [default=%default]")
    parser.add_option(
        "",
        "--address",
        type="string",
        default=None,
        help=
        "set the address of the node (addresses are a single char) [default=%default]"
    )
    expert_grp.add_option(
        "-c",
        "--carrier-threshold",
        type="eng_float",
        default=-20,
        help="set carrier detect threshold (dB) [default=%default]")
    parser.add_option(
        "",
        "--pkt-gen-time",
        type="eng_float",
        default=.05,
        help="set the time between sending each packet (s) [default=%default]")
    parser.add_option(
        "",
        "--pkt-padding",
        type="int",
        default=1000,
        help=
        "pad packet with pkt-padding number of extra chars [default=%default]")
    parser.add_option("",
                      "--autoselect-freq",
                      action="store_true",
                      default=False)
    parser.add_option(
        "",
        "--test-time",
        type="int",
        default=500,
        help="number of seconds to run the test for [default=%default]")

    usrp_graph.add_options(parser, expert_grp)
    transmit_path.add_options(parser, expert_grp)
    receive_path.add_options(parser, expert_grp)
    blks2.ofdm_mod.add_options(parser, expert_grp)
    blks2.ofdm_demod.add_options(parser, expert_grp)
    cs_mac.add_options(parser, expert_grp)
    sense_path.add_options(parser, expert_grp)

    (options, args) = parser.parse_args()
    if len(args) != 0:
        parser.print_help(sys.stderr)
        sys.exit(1)

    #if options.rx_freq is None or options.tx_freq is None:
    #    sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
    #    parser.print_help(sys.stderr)
    #    sys.exit(1)
    if options.address is None:
        sys.stderr.write("You must specify a node address\n")
        parser.print_help(sys.stderr)
        sys.exit(1)

    ###########################
    #set PHY and MAC
    ###########################
    # Attempt to enable realtime scheduling
    r = gr.enable_realtime_scheduling()
    if r == gr.RT_OK:
        realtime = True
    else:
        realtime = False
        print "Note: failed to enable realtime scheduling"

    pkts_sent = 0
    # instantiate the MAC
    mac = cs_mac(options, rx_callback)

    # build the graph (PHY)
    tx_failures = []
    tb = usrp_graph(mac.phy_rx_callback, options)

    mac.set_flow_graph(tb)  # give the MAC a handle for the PHY
    mac.set_error_array(tx_failures)

    ###########################
    #manage packet generation and data gathering
    ###########################
    print
    print "address:        %s" % (options.address)
    print
    print "modulation:     %s" % (options.modulation, )
    #print "freq:           %s"   % (eng_notation.num_to_str(options.tx_freq))

    tb.rxpath.set_carrier_threshold(options.carrier_threshold)
    print "Carrier sense threshold:", options.carrier_threshold, "dB"

    tb.start()  # Start executing the flow graph (runs in separate threads)

    if options.autoselect_freq:
        new_freq = mac.find_best_freq()
        raw_input("Press Enter to begin transmitting")

    #n = 0
    #while n < 50:
    #    os.system("clear")
    #    new_freq = mac.find_best_freq()
    #    time.sleep(.3)
    #    n += 1
    #return

    mac.start()

    start_time = time.clock()
    while (pkts_sent < options.packets + 3):  # or not EOF_rcvd):
        #if options.verbose:
        #    print "give a new packet to the MAC"
        if pkts_sent > options.packets:
            mac.new_packet('x', "EOF")
        else:
            mac.new_packet('x',
                           str(pkts_sent).zfill(3) +
                           options.pkt_padding * "k")  # run the tests
        pkts_sent += 1
    #while not EOF_rcvd:
    #    time.sleep(options.pkt_gen_time)

    #for some reason the timing was off, and I need a factor of two to get it
    #approximately to where it should be
    while time.clock() - start_time < 2 * options.test_time:
        pass

    mac.stop()
    mac.wait()

    #do stuff with the measurement results
    print
    print time.strftime("%X")
    print "this node sent:     ", pkts_sent, " packets"
    print "there were:         ", len(
        tx_failures), " packets that were not successfully sent"
    #print "this node received: ", num_acks, " ACK packets"
    print "this node rcvd:     ", len(set(pkts_rcvd)), " packets"
    print "there were:         ", len(pkts_rcvd) - len(
        set(pkts_rcvd)), " spurious packet retransmissions"
    print "collisions:         ", mac.collisions
    if options.pkt_padding != 0:
        print "the packets this node sent were of length: ", len(
            str(pkts_sent).zfill(3) +
            options.pkt_padding * "k") + 2  # + 2 for the address chars
    #for item in pkts_rcvd:
    #    print "\t", item
    #print "succesfully sent the following packets"
    #for item in mac.sent_pkts:
    #    print "\t", item

    tb.stop()
    tb.wait()  # wait for it to finish
def main_blinkstick_util():
    global options
    global sticks

    parser = OptionParser(usage="usage: %prog [options] [color]",
                          formatter=IndentedHelpFormatterWithNL())

    parser.add_option("-i",
                      "--info",
                      action="store_true",
                      dest="info",
                      help="Display BlinkStick info")

    parser.add_option(
        "-s",
        "--serial",
        dest="serial",
        help=
        "Select device by serial number. If unspecified, action will be performed on all BlinkSticks."
    )

    parser.add_option("-v",
                      "--verbose",
                      action="store_true",
                      dest="verbose",
                      help="Display debug output")

    group = OptionGroup(parser, "Change color",
                        "These options control the color of the device  ")

    group.add_option("--channel",
                     default=0,
                     dest="channel",
                     help="Select channel. Applies only to BlinkStick Pro.")

    group.add_option("--index",
                     default=0,
                     dest="index",
                     help="Select index. Applies only to BlinkStick Pro.")

    group.add_option("--brightness",
                     default=100,
                     dest="limit",
                     help="Limit the brightness of the color 0..100")

    group.add_option("--limit",
                     default=100,
                     dest="limit",
                     help="Alias to --brightness option")

    group.add_option(
        "--set-color",
        dest="color",
        help=
        "Set the color for the device. This can also be the last argument for the script. "
        "The value can either be a named color, hex value, 'random' or 'off'.\n\n"
        "CSS color names are defined http://www.w3.org/TR/css3-color/ e.g. red, green, blue. "
        "Specify color using hexadecimal color value e.g. 'FF3366'")
    group.add_option("--inverse",
                     action="store_true",
                     dest="inverse",
                     help="Control BlinkSticks in inverse mode")

    group.add_option(
        "--set-led-count",
        dest="led_count",
        help="Set the number of LEDs to control for supported devices.")

    parser.add_option_group(group)

    group = OptionGroup(
        parser, "Control animations",
        "These options will blink, morph or pulse selected color.  ")

    group.add_option(
        "--blink",
        dest="blink",
        action='store_true',
        help=
        "Blink LED (requires --set-color or color set as last argument, and optionally --delay)"
    )

    group.add_option(
        "--pulse",
        dest="pulse",
        action='store_true',
        help=
        "Pulse LED (requires --set-color or color set as last argument, and optionally --duration)."
    )

    group.add_option(
        "--morph",
        dest="morph",
        action='store_true',
        help=
        "Morph to specified color (requires --set-color or color set as last argument, and optionally --duration)."
    )

    group.add_option(
        "--duration",
        dest="duration",
        default=1000,
        help=
        "Set duration of transition in milliseconds (use with --morph and --pulse)."
    )

    group.add_option(
        "--delay",
        dest="delay",
        default=500,
        help="Set time in milliseconds to light LED for (use with --blink).")

    group.add_option(
        "--repeats",
        dest="repeats",
        default=1,
        help="Number of repetitions (use with --blink and --pulse).")

    parser.add_option_group(group)

    group = OptionGroup(
        parser, "Device data and behaviour",
        "These options will change device mode and data stored internally.  ")

    group.add_option(
        "--set-mode",
        default=0,
        dest="mode",
        help=
        "Set mode for BlinkStick Pro:\n\n    0 - default\n\n    1 - inverse\n\n    2 - ws2812\n\n    3 - ws2812 mirror"
    )

    group.add_option("--set-infoblock1",
                     dest="infoblock1",
                     help="Set the first info block for the device.")

    group.add_option("--set-infoblock2",
                     dest="infoblock2",
                     help="Set the second info block for the device.")

    parser.add_option_group(group)

    group = OptionGroup(parser, "Advanced options", "")

    group.add_option(
        "--add-udev-rule",
        action="store_true",
        dest="udev",
        help=
        "Add udev rule to access BlinkSticks without root permissions. Must be run as root."
    )

    parser.add_option_group(group)

    (options, args) = parser.parse_args()

    if options.serial is None:
        sticks = blinkstick.find_all()
    else:
        sticks = [blinkstick.find_by_serial(options.serial)]

        if len(sticks) == 0:
            print("BlinkStick with serial number " + options.device +
                  " not found...")
            return 64

    #Global action
    if options.udev:

        try:
            filename = "/etc/udev/rules.d/85-blinkstick.rules"
            file = open(filename, 'w')
            file.write(
                'SUBSYSTEM=="usb", ATTR{idVendor}=="20a0", ATTR{idProduct}=="41e5", MODE:="0666"'
            )
            file.close()

            print("Rule added to {0}").format(filename)
        except IOError as e:
            print(str(e))
            print(
                "Make sure you run this script as root: sudo blinkstick --add-udev-rule"
            )
            return 64

        print("Reboot your computer for changes to take effect")
        return 0

    for stick in sticks:
        if options.inverse:
            stick.set_inverse(True)

        stick.set_max_rgb_value(int(float(options.limit) / 100.0 * 255))

        stick.set_error_reporting(False)

    #Actions here work on all BlinkSticks
    for stick in sticks:
        if options.infoblock1:
            stick.set_info_block1(options.infoblock1)

        if options.infoblock2:
            stick.set_info_block2(options.infoblock2)

        if options.mode:
            if options.mode == "0" or options.mode == "1" or options.mode == "2" or options.mode == "3":
                stick.set_mode(int(options.mode))
            else:
                print("Error: Invalid mode parameter value")

        elif options.led_count:
            led_count = int(options.led_count)

            if led_count > 0 and led_count <= 32:
                stick.set_led_count(led_count)
            else:
                print("Error: Invalid led-count parameter value")

        elif options.info:
            print_info(stick)
        elif options.color or len(args) > 0:
            if options.color:
                color = options.color
            else:
                color = args[0]

            # determine color
            fargs = {}
            if color.startswith('#'):
                fargs['hex'] = color
            elif color == "random":
                fargs['name'] = 'random'
            elif color == "off":
                fargs['hex'] = "#000000"
            else:
                if len(color) == 6:
                    # If color contains 6 chars check if it's hex
                    try:
                        int(color, 16)
                        fargs['hex'] = "#" + color
                    except:
                        fargs['name'] = color
                else:
                    fargs['name'] = color

            fargs['index'] = int(options.index)
            fargs['channel'] = int(options.channel)

            # handle blink/pulse/morph
            func = stick.set_color
            if options.blink:
                func = stick.blink
                fargs['delay'] = options.delay
                fargs['repeats'] = int(options.repeats)
            elif options.pulse:
                func = stick.pulse
                fargs['duration'] = options.duration
                fargs['repeats'] = int(options.repeats)
            elif options.morph:
                func = stick.morph
                fargs['duration'] = options.duration

            func(**fargs)

        else:
            parser.print_help()
            return 0

    return 0
Пример #48
0
def build_command_line_parser():
    parser = OptionParser(
        description='Read unified messages from datagrepper or a file '
        'replay to unified message bus. If no file is specified, '
        'messages will be retreived from datagrepper.')
    parser.add_option('-d',
                      '--debug',
                      dest='debug',
                      action='store_true',
                      default=False,
                      help='Output more information for debug')

    datagrepper_group = OptionGroup(parser, 'DATAGREPPER options',
                                    'From where to retreive old messages.')
    datagrepper_group.add_option(
        '--server-url',
        dest='server_url',
        help='Server URL of DATAGREPPER. Example: http://hostname[:port]/')
    datagrepper_group.add_option('--no-ssl-verify',
                                 dest='no_ssl_verify',
                                 action='store_true',
                                 help='Do not verify SSL.')
    parser.add_option_group(datagrepper_group)

    msgfile_group = OptionGroup(
        parser, 'File options',
        'Read message from file. Each message in that file should be a '
        'string that can be decoded into a JSON object.')
    msgfile_group.add_option('-f',
                             '--file',
                             metavar='FILE',
                             dest='msg_file',
                             help='File containing message to replay.')
    msgfile_group.add_option(
        '-m',
        '--multiple-lines',
        dest='multiple_lines',
        action='store_true',
        default=False,
        help='Indicate file contains multiple messages line by line.')
    parser.add_option_group(msgfile_group)

    broker_group = OptionGroup(
        parser, 'Message broker options',
        'Send grepped messages to this broker to replay them.')
    broker_group.add_option(
        '-e',
        '--env',
        choices=list(broker_envs.keys()),
        default='dev',
        help='Environment to connect to. Valid choices: {} '
        '(default: %default)'.format(', '.join(broker_envs.keys())))
    broker_group.add_option('--address',
                            dest='address',
                            help='Address which can be queue or topic')
    broker_group.add_option('--certificate-file',
                            dest='cert_file',
                            help='Certificate file')
    broker_group.add_option('--private-key-file',
                            dest='private_key_file',
                            help='Private Key file')
    broker_group.add_option(
        '--ca-certs',
        dest='ca_certs',
        default='/etc/pki/tls/certs/ca-bundle.crt',
        help='Trusted CA certificate bundle (default: %default)')
    parser.add_option_group(broker_group)

    return parser
Пример #49
0
def getOptionParser():
    usage = "usage: %prog [options] job-url-or-path"
    parser = OptionParser(usage=usage)

    prodEnvOptionGroup = OptionGroup(
        parser, "Product Env Options",
        "Normal user use these options to set jvm parameters, job runtime mode etc. "
        "Make sure these options can be used in Product Env.")
    prodEnvOptionGroup.add_option("-j",
                                  "--jvm",
                                  metavar="<jvm parameters>",
                                  dest="jvmParameters",
                                  action="store",
                                  default=DEFAULT_JVM,
                                  help="Set jvm parameters if necessary.")
    prodEnvOptionGroup.add_option(
        "--jobid",
        metavar="<job unique id>",
        dest="jobid",
        action="store",
        default="-1",
        help="Set job unique id when running by Distribute/Local Mode.")
    prodEnvOptionGroup.add_option(
        "-m",
        "--mode",
        metavar="<job runtime mode>",
        action="store",
        default="standalone",
        help="Set job runtime mode such as: standalone, local, distribute. "
        "Default mode is standalone.")
    prodEnvOptionGroup.add_option(
        "-p",
        "--params",
        metavar="<parameter used in job config>",
        action="store",
        dest="params",
        help=
        'Set job parameter, eg: the source tableName you want to set it by command, '
        'then you can use like this: -p"-DtableName=your-table-name", '
        'if you have mutiple parameters: -p"-DtableName=your-table-name -DcolumnName=your-column-name".'
        'Note: you should config in you job tableName with ${tableName}.')
    prodEnvOptionGroup.add_option(
        "-r",
        "--reader",
        metavar="<parameter used in view job config[reader] template>",
        action="store",
        dest="reader",
        type="string",
        help='View job config[reader] template, eg: mysqlreader,streamreader')
    prodEnvOptionGroup.add_option(
        "-w",
        "--writer",
        metavar="<parameter used in view job config[writer] template>",
        action="store",
        dest="writer",
        type="string",
        help='View job config[writer] template, eg: mysqlwriter,streamwriter')
    parser.add_option_group(prodEnvOptionGroup)

    devEnvOptionGroup = OptionGroup(
        parser, "Develop/Debug Options",
        "Developer use these options to trace more details of DataX.")
    devEnvOptionGroup.add_option("-d",
                                 "--debug",
                                 dest="remoteDebug",
                                 action="store_true",
                                 help="Set to remote debug mode.")
    devEnvOptionGroup.add_option(
        "--loglevel",
        metavar="<log level>",
        dest="loglevel",
        action="store",
        default="info",
        help="Set log level such as: debug, info, all etc.")
    parser.add_option_group(devEnvOptionGroup)
    return parser
Пример #50
0
# Miscellaneous options
misc = OptionGroup(parser, Style.BRIGHT + "Miscellaneous" + Style.RESET_ALL)

misc.add_option("--dependencies",
                action="store_true",
                dest="noncore_dependencies",
                default=False,
                help="Check for third-party (non-core) dependencies.")

misc.add_option("--skip-waf",
                action="store_true",
                dest="skip_waf",
                default=False,
                help="Skip heuristic detection of WAF/IPS/IDS protection.")

parser.add_option_group(general)
parser.add_option_group(target)
parser.add_option_group(request)
parser.add_option_group(enumeration)
parser.add_option_group(file_access)
parser.add_option_group(modules)
parser.add_option_group(injection)
parser.add_option_group(detection)
parser.add_option_group(misc)
"""
Dirty hack from sqlmap [1], to display longer options without breaking into two lines.
[1] https://github.com/sqlmapproject/sqlmap/blob/fdc8e664dff305aca19acf143c7767b9a7626881/lib/parse/cmdline.py
"""


def _(self, *args):
Пример #51
0
    def __init__(self, output_lvl=1):
        """
        Set up CLI options, logging levels, and start everything off.
        Afterwards, run a dev server if asked to.
        """

        # CLI options
        # -----------
        parser = OptionParser(version='%prog v{0}'.format(wok.version))

        # Add option to initialize an new project
        init_grp = OptionGroup(parser, "Initialize project",
                "Creates a config file and the required directories. ")
        init_grp.add_option('--init', action='store_true',
                dest='initproject',
                help="create a confg file before generating the site")
        init_grp.add_option('--site_title',
                dest='site_title',
                help="configures the site title to the given value")
        parser.add_option_group(init_grp)

        # Add option to to run the development server after generating pages
        devserver_grp = OptionGroup(parser, "Development server",
                "Runs a small development server after site generation. "
                "--address and --port will be ignored if --server is absent.")
        devserver_grp.add_option('--server', action='store_true',
                dest='runserver',
                help="run a development server after generating the site")
        devserver_grp.add_option('--address', action='store', dest='address',
                help="specify ADDRESS on which to run development server")
        devserver_grp.add_option('--port', action='store', dest='port',
                type='int',
                help="specify PORT on which to run development server")
        parser.add_option_group(devserver_grp)

        # Options for noisiness level and logging
        logging_grp = OptionGroup(parser, "Logging",
                "By default, log messages will be sent to standard out, "
                "and report only errors and warnings.")
        parser.set_defaults(loglevel=logging.WARNING)
        logging_grp.add_option('-q', '--quiet', action='store_const',
                const=logging.ERROR, dest='loglevel',
                help="be completely quiet, log nothing")
        logging_grp.add_option('--warnings', action='store_const',
                const=logging.WARNING, dest='loglevel',
                help="log warnings in addition to errors")
        logging_grp.add_option('-v', '--verbose', action='store_const',
                const=logging.INFO, dest='loglevel',
                help="log ALL the things!")
        logging_grp.add_option('--debug', action='store_const',
                const=logging.DEBUG, dest='loglevel',
                help="log debugging info in addition to warnings and errors")
        logging_grp.add_option('--log', '-l', dest='logfile',
                help="log to the specified LOGFILE instead of standard out")
        parser.add_option_group(logging_grp)

        cli_options, args = parser.parse_args()

        # Set up logging
        # --------------
        logging_options = {
            'format': '%(levelname)s: %(message)s',
            'level': cli_options.loglevel,
        }
        if cli_options.logfile:
            logging_options['filename'] = cli_options.logfile
        else:
            logging_options['stream'] = sys.stdout

        logging.basicConfig(**logging_options)

        # Init project
        # ------------

        if cli_options.initproject:
            ''' Create the config file and the required directories if the user said to.
            '''
            orig_dir = os.getcwd()
            os.chdir(self.SITE_ROOT)

            # create config

            options = Engine.default_options.copy()

            # read old config if present
            if os.path.isfile('config'):
                with open('config') as f:
                    yaml_config = yaml.load(f)

                if yaml_config:
                    options.update(yaml_config)

            if cli_options.site_title:
                options['site_title'] = cli_options.site_title

            # save new config
            with open('config', 'w') as f:
                    yaml.dump(options, f)

            # create required dirs

            required_dirs = [options['content_dir'], options['template_dir']]
            for required_dir in required_dirs:
                if not os.path.isdir(required_dir):
                    os.mkdir(required_dir)

            os.chdir(orig_dir)

        # Action!
        # -------
        self.generate_site()

        # Dev server
        # ----------
        if cli_options.runserver:
            ''' Run the dev server if the user said to, and watch the specified
            directories for changes. The server will regenerate the entire wok
            site if changes are found after every request.
            '''
            output_dir = os.path.join(self.options['server_root'])
            host = '' if cli_options.address is None else cli_options.address
            port = 8000 if cli_options.port is None else cli_options.port
            server = dev_server(serv_dir=output_dir, host=host, port=port,
                dir_mon=True,
                watch_dirs=[
                    self.options['media_dir'],
                    self.options['template_dir'],
                    self.options['content_dir']
                ],
                change_handler=self.generate_site)
            server.run()
Пример #52
0
                             help='Input database name')
    db_operations.add_option('--collection',
                             type="string",
                             action='store',
                             default=None,
                             help='Input collection name')

    find_updates = OptionGroup(
        parser, "Torrent popularity search",
        "Used to identify changes in leeches and seeds")
    find_updates.add_option("--with_changes",
                            action="store_true",
                            default=False,
                            help='Find items with latest changes')

    parser.add_option_group(find_updates)
    parser.add_option_group(torrent_search)
    parser.add_option_group(db_operations)

    (options, args) = parser.parse_args()

    if options.with_search:
        if 'browse-' not in options.search_type and len(options.title) < 1:
            parser.error("Missing required option: --search_type='%s'" %
                         str(search_types))

        if options.with_db:
            if options.collection is None:
                parser.error(
                    "Missing required option: --collections='collections_name'"
                )
Пример #53
0
def main(argv=None, qtGUI=None):
    global options, GUI, splitWorkerPool, splitWorkerOutput, mergeWorkerPool, mergeWorkerOutput
    parser = OptionParser(usage="Usage: kcc-c2p [options] comic_folder",
                          add_help_option=False)
    mainOptions = OptionGroup(parser, "MANDATORY")
    otherOptions = OptionGroup(parser, "OTHER")
    mainOptions.add_option("-y",
                           "--height",
                           type="int",
                           dest="height",
                           default=0,
                           help="Height of the target device screen")
    mainOptions.add_option("-i",
                           "--in-place",
                           action="store_true",
                           dest="inPlace",
                           default=False,
                           help="Overwrite source directory")
    mainOptions.add_option(
        "-m",
        "--merge",
        action="store_true",
        dest="merge",
        default=False,
        help="Combine every directory into a single image before splitting")
    otherOptions.add_option("-d",
                            "--debug",
                            action="store_true",
                            dest="debug",
                            default=False,
                            help="Create debug file for every splitted image")
    otherOptions.add_option("-h",
                            "--help",
                            action="help",
                            help="Show this help message and exit")
    parser.add_option_group(mainOptions)
    parser.add_option_group(otherOptions)
    options, args = parser.parse_args(argv)
    if qtGUI:
        GUI = qtGUI
    else:
        GUI = None
    if len(args) != 1:
        parser.print_help()
        return 1
    if options.height > 0:
        options.sourceDir = args[0]
        options.targetDir = args[0] + "-Splitted"
        if os.path.isdir(options.sourceDir):
            rmtree(options.targetDir, True)
            copytree(options.sourceDir, options.targetDir)
            work = []
            pagenumber = 1
            splitWorkerOutput = []
            splitWorkerPool = Pool(maxtasksperchild=10)
            if options.merge:
                print("Merging images...")
                directoryNumer = 1
                mergeWork = []
                mergeWorkerOutput = []
                mergeWorkerPool = Pool(maxtasksperchild=10)
                mergeWork.append([options.targetDir])
                for root, dirs, files in os.walk(options.targetDir, False):
                    dirs, files = walkSort(dirs, files)
                    for directory in dirs:
                        directoryNumer += 1
                        mergeWork.append([os.path.join(root, directory)])
                if GUI:
                    GUI.progressBarTick.emit('Combining images')
                    GUI.progressBarTick.emit(str(directoryNumer))
                for i in mergeWork:
                    mergeWorkerPool.apply_async(func=mergeDirectory,
                                                args=(i, ),
                                                callback=mergeDirectoryTick)
                mergeWorkerPool.close()
                mergeWorkerPool.join()
                if GUI and not GUI.conversionAlive:
                    rmtree(options.targetDir, True)
                    raise UserWarning("Conversion interrupted.")
                if len(mergeWorkerOutput) > 0:
                    rmtree(options.targetDir, True)
                    raise RuntimeError(
                        "One of workers crashed. Cause: " +
                        mergeWorkerOutput[0][0], mergeWorkerOutput[0][1])
            print("Splitting images...")
            for root, _, files in os.walk(options.targetDir, False):
                for name in files:
                    if getImageFileName(name) is not None:
                        pagenumber += 1
                        work.append([root, name, options])
                    else:
                        os.remove(os.path.join(root, name))
            if GUI:
                GUI.progressBarTick.emit('Splitting images')
                GUI.progressBarTick.emit(str(pagenumber))
                GUI.progressBarTick.emit('tick')
            if len(work) > 0:
                for i in work:
                    splitWorkerPool.apply_async(func=splitImage,
                                                args=(i, ),
                                                callback=splitImageTick)
                splitWorkerPool.close()
                splitWorkerPool.join()
                if GUI and not GUI.conversionAlive:
                    rmtree(options.targetDir, True)
                    raise UserWarning("Conversion interrupted.")
                if len(splitWorkerOutput) > 0:
                    rmtree(options.targetDir, True)
                    raise RuntimeError(
                        "One of workers crashed. Cause: " +
                        splitWorkerOutput[0][0], splitWorkerOutput[0][1])
                if options.inPlace:
                    rmtree(options.sourceDir)
                    move(options.targetDir, options.sourceDir)
            else:
                rmtree(options.targetDir, True)
                raise UserWarning("Source directory is empty.")
        else:
            raise UserWarning("Provided path is not a directory.")
    else:
        raise UserWarning("Target height is not set.")
Пример #54
0
	"""
    ############ 'python netcon_mummer.py mapping_dir query_genome gexf_out [wscheme] [testing]'
    parser = OptionParser(usage=usage)
    # mandatory
    group1 = OptionGroup(parser, "Mandatory Arguments")
    group1.add_option("-i",
                      "--input",
                      dest="query_genome",
                      help="target genome to be scaffolded",
                      metavar="FILE")
    group1.add_option("-f",
                      "--files",
                      dest="mapping_dir",
                      help="DIR where the comparison genomes are stored",
                      metavar="DIR")
    parser.add_option_group(group1)
    group1.add_option("-o",
                      "--output",
                      dest="out",
                      help="write graph to FILE",
                      metavar="FILE")
    # optional
    group2 = OptionGroup(parser, "Optional Arguments")
    group2.add_option(
        "-w",
        "--weightingscheme",
        dest="scheme",
        action="store_true",
        default=False,
        help="use a weighting scheme based on sequence similarity")
    group2.add_option("-t",
Пример #55
0
class CommandLineParser(object):

    # Defines what --regular means
    REGULAR_CMD = [
        'sslv2', 'sslv3', 'tlsv1', 'tlsv1_1', 'tlsv1_2', 'tlsv1_3', 'reneg',
        'resum', 'certinfo', 'http_get', 'hide_rejected_ciphers',
        'compression', 'heartbleed', 'openssl_ccs', 'fallback', 'robot'
    ]
    SSLYZE_USAGE = 'usage: %prog [options] target1.com target2.com:443 target3.com:443{ip} etc...'

    # StartTLS options
    START_TLS_PROTOCOLS = [
        'smtp', 'xmpp', 'xmpp_server', 'pop3', 'ftp', 'imap', 'ldap', 'rdp',
        'postgres', 'auto'
    ]
    START_TLS_USAGE = 'StartTLS should be one of: {}. The \'auto\' option will cause SSLyze to deduce the protocol ' \
                      '(ftp, imap, etc.) from the supplied port number, ' \
                      'for each target servers.'.format(' , '.join(START_TLS_PROTOCOLS))

    # Mapping of StartTls protocols and ports; useful for starttls=auto
    STARTTLS_PROTOCOL_DICT = {
        'smtp': TlsWrappedProtocolEnum.STARTTLS_SMTP,
        587: TlsWrappedProtocolEnum.STARTTLS_SMTP,
        25: TlsWrappedProtocolEnum.STARTTLS_SMTP,
        'xmpp': TlsWrappedProtocolEnum.STARTTLS_XMPP,
        5222: TlsWrappedProtocolEnum.STARTTLS_XMPP,
        'xmpp_server': TlsWrappedProtocolEnum.STARTTLS_XMPP_SERVER,
        5269: TlsWrappedProtocolEnum.STARTTLS_XMPP_SERVER,
        'pop3': TlsWrappedProtocolEnum.STARTTLS_POP3,
        109: TlsWrappedProtocolEnum.STARTTLS_POP3,
        110: TlsWrappedProtocolEnum.STARTTLS_POP3,
        'imap': TlsWrappedProtocolEnum.STARTTLS_IMAP,
        143: TlsWrappedProtocolEnum.STARTTLS_IMAP,
        220: TlsWrappedProtocolEnum.STARTTLS_IMAP,
        'ftp': TlsWrappedProtocolEnum.STARTTLS_FTP,
        21: TlsWrappedProtocolEnum.STARTTLS_FTP,
        'ldap': TlsWrappedProtocolEnum.STARTTLS_LDAP,
        3268: TlsWrappedProtocolEnum.STARTTLS_LDAP,
        389: TlsWrappedProtocolEnum.STARTTLS_LDAP,
        'rdp': TlsWrappedProtocolEnum.STARTTLS_RDP,
        3389: TlsWrappedProtocolEnum.STARTTLS_RDP,
        'postgres': TlsWrappedProtocolEnum.STARTTLS_POSTGRES,
        5432: TlsWrappedProtocolEnum.STARTTLS_POSTGRES
    }

    def __init__(self, available_plugins, sslyze_version):
        """Generate SSLyze's command line parser.
        """
        self._parser = OptionParser(version=sslyze_version,
                                    usage=self.SSLYZE_USAGE)

        # Add generic command line options to the parser
        self._add_default_options()

        # Add plugin-specific options to the parser
        self._add_plugin_options(available_plugins)

        # Add the --regular command line parameter as a shortcut if possible
        regular_help = 'Regular HTTPS scan; shortcut for --{}'.format(
            ' --'.join(self.REGULAR_CMD))
        self._parser.add_option('--regular',
                                action='store_true',
                                dest=None,
                                help=regular_help)

    def parse_command_line(self):
        """Parses the command line used to launch SSLyze.
        """

        (args_command_list, args_target_list) = self._parser.parse_args()

        if args_command_list.update_trust_stores:
            # Just update the trust stores and do nothing
            TrustStoresRepository.update_default()
            raise TrustStoresUpdateCompleted()

        # Handle the --targets_in command line and fill args_target_list
        if args_command_list.targets_in:
            if args_target_list:
                raise CommandLineParsingError(
                    'Cannot use --targets_list and specify targets within the command line.'
                )

            try:  # Read targets from a file
                with open(args_command_list.targets_in) as f:
                    for target in f.readlines():
                        if target.strip():  # Ignore empty lines
                            if not target.startswith(
                                    '#'):  # Ignore comment lines
                                args_target_list.append(target.strip())
            except IOError:
                raise CommandLineParsingError(
                    'Can\'t read targets from input file \'{}.'.format(
                        args_command_list.targets_in))

        if not args_target_list:
            raise CommandLineParsingError('No targets to scan.')

        # Handle the --regular command line parameter as a shortcut
        if self._parser.has_option('--regular'):
            if getattr(args_command_list, 'regular'):
                setattr(args_command_list, 'regular', False)
                for cmd in self.REGULAR_CMD:
                    setattr(args_command_list, cmd, True)

        # Sanity checks on the command line options
        # Prevent --quiet and --xml_out -
        if args_command_list.xml_file and args_command_list.xml_file == '-' and args_command_list.quiet:
            raise CommandLineParsingError(
                'Cannot use --quiet with --xml_out -.')

        # Prevent --quiet and --json_out -
        if args_command_list.json_file and args_command_list.json_file == '-' and args_command_list.quiet:
            raise CommandLineParsingError(
                'Cannot use --quiet with --json_out -.')

        # Prevent --xml_out - and --json_out -
        if args_command_list.json_file and args_command_list.json_file == '-' \
                and args_command_list.xml_file and args_command_list.xml_file == '-':
            raise CommandLineParsingError(
                'Cannot use --xml_out - with --json_out -.')

        # Sanity checks on the client cert options
        client_auth_creds = None
        if bool(args_command_list.cert) ^ bool(args_command_list.key):
            raise CommandLineParsingError(
                'No private key or certificate file were given. See --cert and --key.'
            )

        elif args_command_list.cert:
            # Private key formats
            if args_command_list.keyform == 'DER':
                key_type = OpenSslFileTypeEnum.ASN1
            elif args_command_list.keyform == 'PEM':
                key_type = OpenSslFileTypeEnum.PEM
            else:
                raise CommandLineParsingError(
                    '--keyform should be DER or PEM.')

            # Let's try to open the cert and key files
            try:
                client_auth_creds = ClientAuthenticationCredentials(
                    args_command_list.cert, args_command_list.key, key_type,
                    args_command_list.keypass)
            except ValueError as e:
                raise CommandLineParsingError(
                    'Invalid client authentication settings: {}.'.format(e[0]))

        # HTTP CONNECT proxy
        http_tunneling_settings = None
        if args_command_list.https_tunnel:
            try:
                http_tunneling_settings = HttpConnectTunnelingSettings.from_url(
                    args_command_list.https_tunnel)
            except ValueError as e:
                raise CommandLineParsingError(
                    'Invalid proxy URL for --https_tunnel: {}.'.format(e[0]))

        # STARTTLS
        tls_wrapped_protocol = TlsWrappedProtocolEnum.PLAIN_TLS
        if args_command_list.starttls:
            if args_command_list.starttls not in self.START_TLS_PROTOCOLS:
                raise CommandLineParsingError(self.START_TLS_USAGE)
            else:
                # StartTLS was specified
                if args_command_list.starttls in self.STARTTLS_PROTOCOL_DICT.keys(
                ):
                    # Protocol was given in the command line
                    tls_wrapped_protocol = self.STARTTLS_PROTOCOL_DICT[
                        args_command_list.starttls]

        # Number of connection retries
        if args_command_list.nb_retries < 1:
            raise CommandLineParsingError(
                'Cannot have a number smaller than 1 for --nb_retries.')

        # Create the server connectivity info for each specifed servers
        # A limitation when using the command line is that only one client_auth_credentials and http_tunneling_settings
        # can be specified, for all the servers to scan
        good_server_list = []
        bad_server_list = []
        for server_string in args_target_list:
            try:
                hostname, ip_address, port = CommandLineServerStringParser.parse_server_string(
                    server_string)
                # TODO(AD): Unicode hostnames may fail on Python2
                #hostname = hostname.decode('utf-8')
                server_info = ServerConnectivityInfo(
                    hostname=hostname,
                    port=port,
                    ip_address=ip_address,
                    tls_wrapped_protocol=tls_wrapped_protocol,
                    tls_server_name_indication=args_command_list.sni,
                    xmpp_to_hostname=args_command_list.xmpp_to,
                    client_auth_credentials=client_auth_creds,
                    http_tunneling_settings=http_tunneling_settings)
                # Keep the original server string to display it in the CLI output if there was a connection error
                server_info.server_string = server_string

                good_server_list.append(server_info)
            except ServerConnectivityError as e:
                # Will happen for example if the DNS lookup failed or the server string is malformed
                bad_server_list.append(FailedServerScan(server_string, e))
            except ValueError as e:
                # Will happen for example if xmpp_to is specified for a non-XMPP connection
                raise CommandLineParsingError(e[0])

        # Command line hacks
        # Handle --starttls=auto now that we parsed the server strings
        if args_command_list.starttls == 'auto':
            for server_info in good_server_list:
                # We use the port number to deduce the protocol
                if server_info.port in self.STARTTLS_PROTOCOL_DICT.keys():
                    server_info.tls_wrapped_protocol = self.STARTTLS_PROTOCOL_DICT[
                        server_info.port]

        # Handle --http_get now that we parsed the server strings
        # Doing it here is hacky as the option is defined within PluginOpenSSLCipherSuites
        if args_command_list.http_get:
            for server_info in good_server_list:
                if server_info.port == 443:
                    server_info.tls_wrapped_protocol = TlsWrappedProtocolEnum.HTTPS

        return good_server_list, bad_server_list, args_command_list

    def _add_default_options(self):
        """Add default command line options to the parser.
        """
        # Updating the trust stores
        update_stores_group = OptionGroup(self._parser, 'Trust stores options',
                                          '')
        update_stores_group.add_option(
            '--update_trust_stores',
            help=
            'Update the default trust stores used by SSLyze. The latest stores will be downloaded from '
            'https://github.com/nabla-c0d3/trust_stores_observatory. This option is meant to be used separately, '
            'and will silence any other command line option supplied to SSLyze.',
            dest='update_trust_stores',
            action='store_true',
        )
        self._parser.add_option_group(update_stores_group)

        # Client certificate options
        clientcert_group = OptionGroup(self._parser,
                                       'Client certificate options', '')
        clientcert_group.add_option(
            '--cert',
            help=
            'Client certificate chain filename. The certificates must be in PEM format and must be sorted '
            'starting with the subject\'s client certificate, followed by intermediate CA certificates if '
            'applicable.',
            dest='cert')
        clientcert_group.add_option('--key',
                                    help='Client private key filename.',
                                    dest='key')
        clientcert_group.add_option(
            '--keyform',
            help='Client private key format. DER or PEM (default).',
            dest='keyform',
            default='PEM')
        clientcert_group.add_option('--pass',
                                    help='Client private key passphrase.',
                                    dest='keypass',
                                    default='')
        self._parser.add_option_group(clientcert_group)

        # Input / output
        output_group = OptionGroup(self._parser, 'Input and output options',
                                   '')
        # XML output
        output_group.add_option(
            '--xml_out',
            help=
            'Write the scan results as an XML document to the file XML_FILE. If XML_FILE is set to "-", the XML '
            'output will instead be printed to stdout.',
            dest='xml_file',
            default=None)
        # JSON output
        output_group.add_option(
            '--json_out',
            help=
            'Write the scan results as a JSON document to the file JSON_FILE. If JSON_FILE is set to "-", the '
            'JSON output will instead be printed to stdout. The resulting JSON file is a serialized version of '
            'the ScanResult objects described in SSLyze\'s Python API: the nodes and attributes will be the same. '
            'See https://nabla-c0d3.github.io/sslyze/documentation/available-scan-commands.html for more details.',
            dest='json_file',
            default=None)
        # Read targets from input file
        output_group.add_option(
            '--targets_in',
            help=
            'Read the list of targets to scan from the file TARGETS_IN. It should contain one host:port per '
            'line.',
            dest='targets_in',
            default=None)
        # No text output
        output_group.add_option(
            '--quiet',
            action='store_true',
            dest='quiet',
            help=
            'Do not output anything to stdout; useful when using --xml_out or --json_out.'
        )
        self._parser.add_option_group(output_group)

        # Connectivity option group
        connect_group = OptionGroup(self._parser, 'Connectivity options', '')
        # Timeout
        connect_group.add_option(
            '--timeout',
            help=
            'Set the timeout value in seconds used for every socket connection made to the target server(s). '
            'Default is {}s.'.format(str(SSLConnection.NETWORK_TIMEOUT)),
            type='int',
            dest='timeout',
            default=SSLConnection.NETWORK_TIMEOUT)
        # Control connection retry attempts
        connect_group.add_option(
            '--nb_retries',
            help=
            'Set the number retry attempts for all network connections initiated throughout the scan. Increase '
            'this value if you are getting a lot of timeout/connection errors when scanning a specific server. '
            'Decrease this value to increase the speed of the scans; results may however return connection '
            'errors. Default is {} connection attempts.'.format(
                str(SSLConnection.NETWORK_MAX_RETRIES)),
            type='int',
            dest='nb_retries',
            default=SSLConnection.NETWORK_MAX_RETRIES)
        # HTTP CONNECT Proxy
        connect_group.add_option(
            '--https_tunnel',
            help=
            'Tunnel all traffic to the target server(s) through an HTTP CONNECT proxy. HTTP_TUNNEL should be the '
            'proxy\'s URL: \'http://*****:*****@HOST:PORT/\'. For proxies requiring authentication, only Basic '
            'Authentication is supported.',
            dest='https_tunnel',
            default=None)
        # STARTTLS
        connect_group.add_option(
            '--starttls',
            help=
            'Perform a StartTLS handshake when connecting to the target server(s). '
            '{}'.format(self.START_TLS_USAGE),
            dest='starttls',
            default=None)
        connect_group.add_option(
            '--xmpp_to',
            help=
            'Optional setting for STARTTLS XMPP. XMPP_TO should be the hostname to be put in the \'to\' '
            'attribute of the XMPP stream. Default is the server\'s hostname.',
            dest='xmpp_to',
            default=None)
        # Server Name Indication
        connect_group.add_option(
            '--sni',
            help=
            'Use Server Name Indication to specify the hostname to connect to.  Will only affect TLS 1.0+ '
            'connections.',
            dest='sni',
            default=None)
        self._parser.add_option_group(connect_group)

    def _add_plugin_options(self, available_plugins):
        """Recovers the list of command line options implemented by the available plugins and adds them to the command
        line parser.
        """
        for plugin_class in available_plugins:
            # Add the current plugin's commands to the parser
            group = OptionGroup(self._parser, plugin_class.get_title(),
                                plugin_class.get_description())
            for option in plugin_class.get_cli_option_group():
                group.add_option(option)
            self._parser.add_option_group(group)
Пример #56
0
def parse_command_line():
    """Collects, parses and validates command line arguments."""

    prs = OptionParser(version="%prog $Revision$")

    # Mandatory parameters
    grp0 = OptionGroup(prs, "Source and destination",
                       "*** Mandatory parameters always required ***")
    grp0.add_option(
        "-r",
        "--raster",
        dest="raster",
        action="append",
        default=None,
        help="append raster to list of input files, at least one raster "
        "file required. You may use wildcards (?,*) for specifying multiple files."
    )
    grp0.add_option(
        "-t",
        "--table",
        dest="table",
        action="store",
        default=None,
        help=
        "raster destination in form of [<schema>.]<table> or base raster table for overview level>1, required"
    )
    prs.add_option_group(grp0)

    # Optional parameters - raster manipulation
    grp_r = OptionGroup(
        prs, "Raster processing",
        "Optional parameters used to manipulate input raster dataset")
    grp_r.add_option("-s",
                     "--srid",
                     dest="srid",
                     action="store",
                     type="int",
                     default=-1,
                     help="assign output raster with specified SRID")
    grp_r.add_option(
        "-b",
        "--band",
        dest="band",
        action="store",
        type="int",
        default=None,
        help="specify number of the band to be extracted from raster file")
    grp_r.add_option(
        "-k",
        "--block-size",
        dest="block_size",
        action="store",
        default=None,
        help="cut raster(s) into tiles to be inserted one by table row."
        "BLOCK_SIZE is expressed as WIDTHxHEIGHT. Incomplete tiles are completed with nodata values"
    )
    grp_r.add_option(
        "-R",
        "--register",
        dest="register",
        action="store_true",
        default=False,
        help="register the raster as a filesystem (out-db) raster")
    grp_r.add_option(
        "-l",
        "--overview-level",
        dest="overview_level",
        action="store",
        type="int",
        default=1,
        help='create overview tables named as o_<LEVEL>_<RASTER_TABLE> and '
        'populate with GDAL-provided overviews (regular blocking only)')
    prs.add_option_group(grp_r)

    # Optional parameters - database/table manipulation
    grp_t = OptionGroup(
        prs, 'Database processing',
        'Optional parameters used to manipulate database objects')
    grp_t.add_option(
        '-c',
        '--create',
        dest='create_table',
        action='store_true',
        default=False,
        help=
        'create new table and populate it with raster(s), this is the default mode'
    )
    grp_t.add_option('-a',
                     '--append',
                     dest='append_table',
                     action='store_true',
                     default=False,
                     help='append raster(s) to an existing table')
    grp_t.add_option(
        "-d",
        "--drop",
        dest="drop_table",
        action="store_true",
        default=False,
        help="drop table, create new one and populate it with raster(s)")
    grp_t.add_option(
        "-f",
        "--field",
        dest="column",
        action="store",
        default=g_rt_column,
        help="specify name of destination raster column, default is 'rast'")
    grp_t.add_option("-F",
                     "--filename",
                     dest="filename",
                     action="store_true",
                     default=False,
                     help="add a column with the name of the file")
    grp_t.add_option("-I",
                     "--index",
                     dest="index",
                     action="store_true",
                     default=False,
                     help="create a GiST index on the raster column")
    grp_t.add_option("-M",
                     "--vacuum",
                     dest="vacuum",
                     action="store_true",
                     default=False,
                     help="issue VACUUM command against all generated tables")
    grp_t.add_option(
        '-V',
        '--create-raster-overviews',
        dest='create_raster_overviews_table',
        action='store_true',
        default=False,
        help='create RASTER_OVERVIEWS table used to store overviews metadata')
    prs.add_option_group(grp_t)

    # Other optional parameters
    grp_u = OptionGroup(prs, "Miscellaneous", "Other optional parameters")
    grp_u.add_option(
        "-e",
        "--endian",
        dest="endian",
        action="store",
        type="int",
        default=g_rt_endian,
        help="control endianness of generated binary output of raster; "
        "specify 0 for XDR and 1 for NDR (default); "
        "only NDR output is supported now")
    grp_u.add_option("-w",
                     "--raster-version",
                     dest="version",
                     action="store",
                     type="int",
                     default=g_rt_version,
                     help="specify version of raster protocol, default is 0; "
                     "only value of zero is supported now")
    grp_u.add_option("-o",
                     "--output",
                     dest="output",
                     action="store",
                     default=sys.stdout,
                     help="specify output file, otherwise send to stdout")
    grp_u.add_option("-v",
                     "--verbose",
                     dest="verbose",
                     action="store_true",
                     default=False,
                     help="verbose mode. Useful for debugging")
    prs.add_option_group(grp_u)

    (opts, args) = prs.parse_args()

    # Validate options
    if opts.create_table and opts.drop_table and opts.append_table:
        prs.error("options -c, -a and -d are mutually exclusive")
    if opts.create_table and opts.drop_table:
        prs.error("options -c and -d are mutually exclusive")
    if opts.create_table and opts.append_table:
        prs.error("options -c and -a are mutually exclusive")
    if opts.append_table and opts.drop_table:
        prs.error("options -a and -d are mutually exclusive")
    if (not opts.create_table and not opts.drop_table
            and not opts.append_table) or opts.drop_table:
        opts.create_table = True

    if opts.raster is None:
        prs.error(
            "use option -r to specify at least one input raster. Wildcards (?,*) are accepted."
        )

    if opts.block_size is not None and len(opts.raster) != 1:
        prs.error("regular blocking supports single-raster input only")

    if opts.block_size is not None:
        if len(opts.block_size.split('x')) != 2 and len(
                opts.block_size.split('X')) != 2:
            prs.error("invalid format of block size, expected WIDTHxHEIGHT")

    if opts.overview_level > 1 and opts.block_size is None:
        prs.error(
            "regular blocking mode required to enable overviews support (level > 1)"
        )

    if opts.create_raster_overviews_table and opts.overview_level <= 1:
        prs.error(
            'create table for RASTER_OVERVIEWS available only if overviews import requested'
        )

    # XXX: Now, if --band=Nth, then only Nth band of all specified rasters is dumped/imported
    #      This behavior can be changed to support only single-raster input if --band option used.
    #if opts.band is not None and len(opts.raster) > 1:
    #    prs.error("option -b requires single input raster only, specify -r option only once")

    if opts.table is None:
        prs.error("use option -t to specify raster destination table")
    if len(opts.table.split('.')) > 2:
        prs.error(
            "invalid format of table name specified with option -t, expected [<schema>.]table"
        )

    if opts.output is None:
        prs.error(
            "failed to initialise output file, try to use option -o explicitly"
        )

    if opts.version is not None:
        if opts.version != g_rt_version:
            prs.error(
                "invalid version of WKT Raster protocol specified, only version 0 is supported"
            )
    else:
        prs.error("use option -w to specify version of WKT Raster protocol")

    if opts.endian is not None:
        if opts.endian != NDR and opts.endian != XDR:
            prs.error(
                "invalid endianness value, valid ones are 0 for NDR or 1 for XDR"
            )
    else:
        prs.error("use option -e to specify endianness of binary output")

    return (opts, args)
Пример #57
0
                      dest='left',
                      type='float',
                      default='-180.0')
bbox_group.add_option('-r',
                      '--right',
                      dest='right',
                      type='float',
                      default='180.0')
bbox_group.add_option('-t', '--top', dest='top', type='float', default='90.0')
bbox_group.add_option('-b',
                      '--bottom',
                      dest='bottom',
                      type='float',
                      default='-90.0')

parser.add_option_group(bbox_group)

tframe_group = OptionGroup(parser, "Time Frame (YYYY-MM-DD)")
tframe_group.add_option('-s', '--start', dest='start', default='2000-01-01')
tframe_group.add_option('-e', '--end', dest='end', default='2100-01-01')
parser.add_option_group(tframe_group)

parser.add_option('-H',
                  '--history',
                  dest='history',
                  action="store_true",
                  default=False,
                  help="Output all versions, not just current version.")

parser.add_option('-c',
                  '--changesets',
    def __init__(self, Freq):
        global parser
        parser = OptionParser(option_class=eng_option,
                              conflict_handler="resolve")
        expert_grp = parser.add_option_group("Expert")
        parser.add_option("-s",
                          "--size",
                          type="eng_float",
                          default=1024,
                          help="set packet size [default=%default]")
        parser.add_option("-M",
                          "--megabytes",
                          type="eng_float",
                          default=10.0,
                          help="set megabytes to transmit [default=%default]")
        parser.add_option("",
                          "--discontinuous",
                          action="store_true",
                          default=False,
                          help="enable discontinuous mode")
        parser.add_option("",
                          "--from-file",
                          default=None,
                          help="use file for packet contents")
        parser.add_option("-e",
                          "--interface",
                          type="string",
                          default="eth0",
                          help="Select ethernet interface. Default is eth0")
        parser.add_option(
            "-m",
            "--MAC_addr",
            type="string",
            default="",
            help="Select USRP2 by its MAC address.Default is auto-select")
        parser.add_option("-j",
                          "--start",
                          type="eng_float",
                          default=1e7,
                          help="Start ferquency [default = %default]")
        parser.add_option("-k",
                          "--stop",
                          type="eng_float",
                          default=1e8,
                          help="Stop ferquency [default = %default]")
        parser.add_option(
            "",
            "--tune-delay",
            type="eng_float",
            default=1e-3,
            metavar="SECS",
            help=
            "time to delay (in seconds) after changing frequency[default=%default]"
        )
        parser.add_option(
            "",
            "--dwell-delay",
            type="eng_float",
            default=1e-3,
            metavar="SECS",
            help=
            "time to dwell (in seconds) at a given frequncy[default=%default]")
        parser.add_option("-G",
                          "--gain",
                          type="eng_float",
                          default=None,
                          help="set gain in dB (default is midpoint)")
        parser.add_option("-s",
                          "--fft-size",
                          type="int",
                          default=256,
                          help="specify number of FFT bins [default=%default]"
                          )  # changed default value(256)
        parser.add_option("-d",
                          "--decim",
                          type="intx",
                          default=16,
                          help="set decimation to DECIM [default=%default]"
                          )  # changed default value(16)
        parser.add_option("-i",
                          "--input_file",
                          default="",
                          help="radio input file",
                          metavar="FILE")
        parser.add_option(
            "-S",
            "--sense-bins",
            type="int",
            default=128,
            help="set number of bins in the OFDM block [default=%default]")

        usrp_transmit_path.add_options(parser, expert_grp)
        ofdm.ofdm_mod.add_options(parser, expert_grp)
        (self.options, self.args) = parser.parse_args()
        if len(self.args) != 0:
            parser.print_help()
            sys.exit(1)
        if self.options.tx_freq is None:
            sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
            parser.print_help(sys.stderr)
            sys.exit(1)
        self.options.tx_freq = Freq.value
        self.tb = transmit(self.options)
        r = gr.enable_realtime_scheduling()
        if r != gr.RT_OK:
            print "Warning: failed to enable realtime scheduling"
Пример #59
0
def create_parser():
    parser = OptionParser(
        usage="%prog [command] [options] file1.mp3 [file2.mp3...]",
        version="%prog " + stagger.versionstr)

    # General options
    parser.add_option("-l",
                      "--list",
                      action="store_true",
                      dest="list",
                      help="List known frame types and exit")

    parser.add_option(
        "-f",
        "--frameid",
        action="store_true",
        dest="frameid",
        help="Print low-level frame ids instead of descriptive names")

    parser.add_option("-q",
                      "--quiet",
                      action="store_true",
                      dest="quiet",
                      help="Suppress warning messages on stderr")

    parser.add_option("-v",
                      "--verbose",
                      action="store_true",
                      dest="verbose",
                      help="Explain what is being done")
    parser.add_option(
        "-n",
        "--no-act",
        action="store_false",
        dest="act",
        default=True,
        help="Don't actually change any files; implies --verbose.")

    # Command group
    group = OptionGroup(parser, "Commands")
    group.add_option("-p",
                     "--print",
                     action="store_true",
                     dest="print",
                     help="Print tag contents (default)")
    group.add_option("-d",
                     "--delete",
                     action="store_true",
                     dest="delete",
                     help="Delete tags")

    group.add_option("-s",
                     "--set",
                     action="append",
                     nargs=2,
                     dest="set",
                     metavar="FRAME VALUE",
                     help="Set FRAME to VALUE")

    group.add_option("-r",
                     "--remove",
                     action="append",
                     dest="remove",
                     metavar="FRAME",
                     help="Remove all instances of FRAME")

    parser.add_option_group(group)

    # Debug group
    group = OptionGroup(parser, "Debugging options")
    group.add_option("--dump",
                     action="store",
                     dest="dump",
                     metavar="FILE",
                     help="Dump raw binary tag from FILE to stdout")

    group.add_option("--load",
                     action="store",
                     dest="load",
                     metavar="FILE",
                     help="Load binary tag dump from stdin and apply to FILE")

    group.add_option("--stats",
                     action="store_true",
                     dest="stats",
                     help="Print resource usage statistics before termination")
    parser.add_option_group(group)

    return parser
Пример #60
0
def main():

    parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
    expert_grp = parser.add_option_group("Expert")

    parser.add_option(
        "-m",
        "--modulation",
        type="choice",
        choices=['bpsk', 'qpsk'],
        default='bpsk',
        help="Select modulation from: bpsk, qpsk [default=%%default]")

    parser.add_option("-v", "--verbose", action="store_true", default=False)
    expert_grp.add_option(
        "-c",
        "--carrier-threshold",
        type="eng_float",
        default=30,
        help="set carrier detect threshold (dB) [default=%default]")
    expert_grp.add_option("",
                          "--tun-device-filename",
                          default="/dev/net/tun",
                          help="path to tun device file [default=%default]")

    digital.ofdm_mod.add_options(parser, expert_grp)
    digital.ofdm_demod.add_options(parser, expert_grp)
    transmit_path.add_options(parser, expert_grp)
    receive_path.add_options(parser, expert_grp)
    uhd_receiver.add_options(parser)
    uhd_transmitter.add_options(parser)

    (options, args) = parser.parse_args()
    if len(args) != 0:
        parser.print_help(sys.stderr)
        sys.exit(1)

    if options.rx_freq is None or options.tx_freq is None:
        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
        parser.print_help(sys.stderr)
        sys.exit(1)

    # open the TUN/TAP interface
    (tun_fd, tun_ifname) = open_tun_interface(options.tun_device_filename)

    # Attempt to enable realtime scheduling
    r = gr.enable_realtime_scheduling()
    if r == gr.RT_OK:
        realtime = True
    else:
        realtime = False
        print "Note: failed to enable realtime scheduling"

    # instantiate the MAC
    mac = cs_mac(tun_fd, verbose=True)

    # build the graph (PHY)
    tb = my_top_block(mac.phy_rx_callback, options)

    mac.set_flow_graph(tb)  # give the MAC a handle for the PHY

    print "modulation:     %s" % (options.modulation, )
    print "freq:           %s" % (eng_notation.num_to_str(options.tx_freq))

    tb.rxpath.set_carrier_threshold(options.carrier_threshold)
    print "Carrier sense threshold:", options.carrier_threshold, "dB"

    print
    print "Allocated virtual ethernet interface: %s" % (tun_ifname, )
    print "You must now use ifconfig to set its IP address. E.g.,"
    print
    print "  $ sudo ifconfig %s 192.168.200.1" % (tun_ifname, )
    print
    print "Be sure to use a different address in the same subnet for each machine."
    print

    tb.start()  # Start executing the flow graph (runs in separate threads)

    mac.main_loop()  # don't expect this to return...

    tb.stop()  # but if it does, tell flow graph to stop.
    tb.wait()  # wait for it to finish