Change python script to be non-interactive and accept list input
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a script for running information on domains at the moment. Right now it prompts the user for input. I need it to run non-promted.

The ideal run would be only option 1 (domain input) and passing a file of domains:

For single domains:

asnrecon.py -domain google.com

or for a list of domains:

asnrecon.py -file domains.txt

The script is here: https://github.com/ttymeh/asn

awarded to drakmail

Crowdsource coding tasks.

4 Solutions


import getopt
import sys

if __name__ == '__main__':
    setdefaulttimeout(DEFAULT_SOCKET_TIMEOUT)

    try:
        arguments, values = getopt.getopt(sys.argv[1:], "f:d:", ["domain=","file="])
    except getopt.error as err:
        print("asnrecon.py -d  <domain-name> | -f <list-file> ")
        sys.exit(2)

    for opt, arg in arguments:
        if opt in ("-f", "--file"):
            file = open(arg, 'r') 
            lines = file.readlines()
            for domain in lines: 
                IPPoolASN(domain).resolve_ip_ranges()

        elif opt in ("-d", "--domain"):
            IPPoolASN(arg).resolve_ip_ranges()
Winning solution

I'm created the pull request: https://github.com/ttymeh/asn/pull/2/files


**Run it as python asnrecon.py --scan 1 --domain 'google.com'**

import argparse

class IPPoolASN(IPPool):
    """ The class that builds ip pool by the given hostname. """
    def __init__(self, hostname, option='y'):
        self.option = option
        try:
            from pyasn import pyasn
        except ImportError:
            system('pip install pyasn')
            from pyasn import pyasn

        try:
            asndb = pyasn(ASNDB_FILE_NAME)
        except IOError:
            if self.option == 'y':
                self._install_asndb()
                asndb = pyasn(ASNDB_FILE_NAME)
            else:
                raise RuntimeError('File "%s" not found.' % ASNDB_FILE_NAME)

if __name__ == '__main__':
    setdefaulttimeout(DEFAULT_SOCKET_TIMEOUT)

# noinspection PyBroadException
try:
    makedirs(TMP_DIR_NAME)

    parser = argparse.ArgumentParser(description='Python non-interactive mode')
    parser.add_argument('--scan', type=int, help='scan type', required=True)
    parser.add_argument('--domain', help='domain name')
    parser.add_argument('--file', help='list of domains in file')

    args = parser.parse_args()
    selected = args.scan

    if selected == 1:
        if args.domain:
            domain = args.domain.strip()
            IPPoolASN(domain).resolve_ip_ranges()
        else:
            raise RuntimeError('Domain not specified')
    elif selected == 2:
        if args.file:
            with open(args.file) as f:
                for line in f:
                    domain = line.strip()
                    IPPool(domain).resolve_ip_ranges()
        else:
            raise RuntimeError('Domain file missing')
    else:
        raise RuntimeError('Unknown option: "%s".' % selected)

except RuntimeError as e:
    print(e.message)
except SystemExit:
    pass
except:
    print(format_exc())
finally:
    rmtree(TMP_DIR_NAME, ignore_errors=True)

PLY library is a very powerful tool for making parsers, allows very complex control languages.
If you join command line parameters and pass the resulting string to the parser, it will check for allowed command line format, and run all code elements in the required order.
Also, you can make control language syntax as you want by defining new lexemes via regexps, and syntax structures in BNF-like notation.

https://github.com/ttymeh/asn/pull/3

View Timeline