Skip to content

Instantly share code, notes, and snippets.

@3dprogramin
Created October 14, 2021 14:36
Show Gist options
  • Save 3dprogramin/74f70faf91f4ce94823e01b1df7901e9 to your computer and use it in GitHub Desktop.
Save 3dprogramin/74f70faf91f4ce94823e01b1df7901e9 to your computer and use it in GitHub Desktop.
Password generator
#!/usr/bin/python
import string, random, sys, os
# requires xclip to be installed
# copy to clipboard
def copy2clip(text):
os.system(f'echo -n "{text}" | xclip -sel clip')
# notify at OS level
def os_notify(message="Password copied to clipboard"):
os.system(f'notify-send "{message}"')
# generate password alphanumeric (lowercase & uppercase) plus not so common characters
def generate_password(length):
letters = string.ascii_lowercase + string.ascii_uppercase + '0123456789' + '~!@#$%^&*()_+-=[]{};:<>?,.'
return ''.join(random.choice(letters) for _ in range(length))
def main():
try:
password_length = int(sys.argv[-1]) if len(sys.argv) > 1 else 20
password = generate_password(password_length)
print(f'Password: {password}')
# `xclip` package is required for this
copy2clip(password)
# `notify-send` required for this
os_notify()
except Exception as ex:
print(f'Error: {ex}')
if __name__ == "__main__":
main()
# ------------------------------------------------------------------------------------------
# Use this to create symbolic link so that you can run it in a new terminal with
# `password_generator [PASSWORD_LENGTH]`
# sudo ln -s /home/user/absolute/path/password_generator.py /usr/bin/password_generator
# ------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment