95 lines
2.4 KiB
Python
95 lines
2.4 KiB
Python
import argparse
|
|
import sys
|
|
|
|
def usage(file):
|
|
print('usage: %s add-comment [-o outfile] [-p port] [-f fingerprint] host comment [infile]' % sys.argv[0])
|
|
|
|
def add_comment():
|
|
parser = argparse.ArgumentParser(
|
|
description = """Add a comment to entry / entries matching the
|
|
given criteria""",
|
|
# We want to provide help on --help, but the default thing
|
|
# also adds -h, which we don't want
|
|
add_help = False,
|
|
# Display our name with the add-comment
|
|
prog = '% add-comment' % sys.argv[0]
|
|
)
|
|
|
|
# --help to get help
|
|
parser.add_argument('--help',
|
|
action = 'help',
|
|
help = 'show this help message and exit'
|
|
)
|
|
|
|
# -o can be used to write the results to a file instead of stdout
|
|
parser.add_argument('-o',
|
|
# Given one argument, we open a file of that name and store it
|
|
# to outfile, which will be sys.stdout.buffer otherwise.
|
|
# We use .buffer since we're going to write binary data
|
|
action = 'store',
|
|
dest = 'outfile',
|
|
type = argparse.FileType('wb'),
|
|
default = sys.stdout.buffer,
|
|
# This is what will be displayed in the help after -o
|
|
metavar = 'outfile',
|
|
help = 'write the sshwot file to a given file instead of the stdout'
|
|
)
|
|
|
|
# -p/--port for port (default port is 22)
|
|
parser.add_argument('-p', '--port',
|
|
action = 'store',
|
|
dest = 'port',
|
|
# Automatically convert to integer
|
|
type = int,
|
|
help = 'the port associated with the given host'
|
|
)
|
|
|
|
# -f/--fingerprint for fingerprint
|
|
parser.add_argument('-f', '--fingerprint',
|
|
action = 'store',
|
|
dest = 'fingerprint',
|
|
help = 'the fingerprint to filter for'
|
|
)
|
|
|
|
# Host and comment are required
|
|
parser.add_argument('host',
|
|
help = 'the domain to filter for'
|
|
)
|
|
parser.add_argument('comment',
|
|
help = 'the comment to add to the entries'
|
|
)
|
|
|
|
# Input file
|
|
parser.add_argument('infile',
|
|
nargs = '?',
|
|
type = argparse.FileType('rb'),
|
|
# Default to stdin. .buffer is because we do binary I/O
|
|
default = sys.stdin.buffer,
|
|
# The text shown for these in the usage
|
|
help = 'a sshwot file to alter'
|
|
)
|
|
|
|
# This automatically parses the command line args for us. If it
|
|
# returns, we have correct arguments
|
|
args = parser.parse_args(sys.argv[2:])
|
|
|
|
print(args) #debg
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
usage(sys.stderr)
|
|
sys.exit(1)
|
|
|
|
if sys.argv[1] == 'add-comment':
|
|
add_comment()
|
|
else:
|
|
usage(sys.stderr)
|
|
sys.exit(1)
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
main()
|
|
except Exception as err:
|
|
print('Error: %s' % err, file=sys.stderr)
|
|
sys.exit(1)
|