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.

3 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)
View Timeline