Skip to content

Instantly share code, notes, and snippets.

@btbytes
Created May 20, 2020 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btbytes/87e10bc7768b67cd1435eefa332ef971 to your computer and use it in GitHub Desktop.
Save btbytes/87e10bc7768b67cd1435eefa332ef971 to your computer and use it in GitHub Desktop.
Shell Safe API Key / Password generator
#!/usr/bin/env python
"""
apikeygen.py
Generate a shell safe API key (or password)
Generated string should not have any characters that need to be escaped in
the shell. The following characters have special meaning in some shell contexts
! # $ ' ( ) * , ; . < = > ? [ ] ^ { } | ~ - . : \
Arguments:
-l --length length of the API key to be generated. Default is 32.
-s --source a string of characters to use
Revision:
2020-05-19: Initial revision
Reference:
https://unix.stackexchange.com/a/357932
"""
import random
def apikeygen(length, source):
return ''.join(random.sample(source, length))
if __name__ == '__main__':
import argparse
import string
source = string.ascii_lowercase\
+ string.ascii_uppercase\
+ string.digits\
+ '@%_/+'
dirty = '@%'
parser = argparse.ArgumentParser()
parser.add_argument(
'-l',
'--length',
type=int,
help='length of the API key to be generated.',
default=32)
parser.add_argument(
'-s',
'--source',
help='a string of characters to use.',
default=source)
parser.add_argument(
'-c',
'--clickable',
help='make the string clickable.',
action='store_true',
default=source)
args = parser.parse_args()
source = args.source
if args.clickable:
source = ''.join([c for c in source if c not in dirty])
print(apikeygen(args.length, source))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment