Skip to content

Instantly share code, notes, and snippets.

@kbfreder
Last active March 10, 2020 20:21
Show Gist options
  • Save kbfreder/3a4b200f1c2808ae9a60f8b6d9913128 to your computer and use it in GitHub Desktop.
Save kbfreder/3a4b200f1c2808ae9a60f8b6d9913128 to your computer and use it in GitHub Desktop.
because who doesn't love a good script
import argparse
import config_file as cfg # a config file
# i like to have 'main' as a stand-alone function, so I have the option
# of importing this file as a module, and calling main directly
def main(*args):
# in which we do the things
print('Running main function')
# in which we parse the arguments passed to the script
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Identify anomalies in port counts')
# a string
parser.add_argument('-d', '--date',
action='store',
help='Date-time string (format yyyy-mm-ddTHH:MM)',
default=None)
# an integer
parser.add_argument('-n', '--numdays',
action='store',
type=int,
help='Number of days back to use for baseline',
default=30)
# pull default value from a config file
parser.add_argument('-l', '--minlen', type=int, action='store',
help='Minimum length of domain SLD', default=cfg.min_len)
# a Boolean
parser.add_argument('-t', '--test-mode',
action='store_true',
help='run in test mode',
default=None)
# a mutually exclusive group
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--dst',
action='store_true',
help='Analyze by dstport',
default=None)
group.add_argument('--src',
action='store_true',
help='Analyze by srcport',
default=None)
args = parser.parse_args()
if args.test_mode:
print(args)
else:
# how to handle our mutually exclusive arguments
if args.dst:
logging.info('Analyzing dstport')
port = 'dstport'
direction = 'outbound'
elif args.src:
logging.info('Analyzing srcport')
port = 'srcport'
direction = 'inbound'
# finally, call the main function
main(args.date, args.numdays, args.minlen, port, direction)
'''
How to call the script:
python argparse.py -d '2020-03-04' -n 30 -l 5 --dst
python argparse.py -h
python argparse.py -t
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment