0

Im working on my first python "app" and after some advice from the participants on Stackoverflow. Ive decided to scrap what I had and start from scratch.

It seems to be parsing the arguments nicely for usage etc but im not sure how I am meant to assign the values to the args?

Do I have to create a nest of ifs? if so how do i do that for the args in docopt?

maybe like this?

if opt in ("-f", "--file"):
    FWORD = arg

CODE

#!/usr/bin/python

"""
Basic domain bruteforcer

Usage:
  your_script.py (-f <file>) (-d <domain>) [-t 10] [-v]
  your_script.py -h | --help

Options:
  -h --help     Show this screen.
  -f --file     File to read potential Sub-domains from. (Required argument)
  -p --proxy    Proxy address and port. [default: http://127.0.0.1:8080] (Optional)
  -d --domain   Domain to bruteforce.(Required argument)
  -t --thread   Thread count. (Optional)
  -v --verbose  Turn debug on. (Optional)
"""
from docopt import docopt

def fread(FWORD, *args):
    flist = open(FWORD).readlines()
    return flist


if __name__ == "__main__":
        arguments = docopt(__doc__, version='0.1a')
        print fread(fword)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
iNoob
  • 1,375
  • 3
  • 19
  • 47

2 Answers2

1

You almost got it. Your arguments variable contains the argument and you look them up as you would in a dict. So if you want to call the fread function with the file argument your main would look like this:

if __name__ == "__main__":
    arguments = docopt(__doc__, version='0.1a')
    fread(arguments['<file>'])

If you call the script like this:

> python your_script.py -f myfiles/file.txt -d google.com

Then your arguments will look like this:

>>> print arguments
{'--domain': True,
 '--file': True,
 '--help': False,
 '--thread': False,
 '--verbose': False,
 '10': False,
 '<domain>': 'google.com',
 '<file>': 'myfiles/file.txt'}
Reite
  • 1,677
  • 10
  • 12
  • thanks for the response. The file is provided on the commandline as an argument for -f --file. What im trying to work out is how do i pair the value to -f or to any of my options or arguments? – iNoob Mar 06 '14 at 15:30
  • What do you mean by "pair the value" what value? The value given on the commandline as the -f parameter is in arguments[''] and the -d argument is in arguments['']. – Reite Mar 06 '14 at 15:40
  • sorry im a noob so my descriptions arent very helpful. if i want to pass the value for -f to `def fread` how would i do that? how does the function know to use the -f value? – iNoob Mar 06 '14 at 15:42
  • I think you might be a bit confused about how docopt works. I've tried to clarify a bit in my edit. Let me know if its still unclear. – Reite Mar 06 '14 at 15:59
  • thanks, yes I can print the arguments and see the txt file as the value but if i try print the return value from the function using `print fread(arguments[''])` i get the following error Traceback (most recent call last): File "./attack2.py", line 32, in print fread(arguments['']) KeyError: '' Im trying to work out how to pass the function the file – iNoob Mar 06 '14 at 16:05
  • if i stick the file name in like this ` print fread('wordfile.txt')` it works as expected but i dont want to have to enter the filename into the code manually, thats the idea behind getting the file from commandline input of --file – iNoob Mar 06 '14 at 16:08
  • Are you running the script from the command line and specifying the parameters like in my answer? – Reite Mar 06 '14 at 16:10
  • yes I am, but if i just print the arguments they look like this rather than as yours do unknown@ubuntu:~$ ./attack2.py -f wordfile.txt -d google.com {'--domain': 'google.com', '--file': 'wordfile.txt', '--help': False, '--thread': False, '--verbose': False, '10': False} unknown@ubuntu:~$ – iNoob Mar 06 '14 at 16:12
  • i have it working now, i specified '--file' instead of <'file'> – iNoob Mar 06 '14 at 16:15
0

You should take a look at argparse from the python standard library.

triggertoast
  • 124
  • 4
  • 1
    @tiggertoast I have looked at it and docopt looks much nicer and simpler. I was using `geptopt` originally. – iNoob Mar 06 '14 at 15:18