본문 바로가기
Development/Python

[Python] 파이썬 Random Password 생성 Tool

by 선인장 🌵 2023. 12. 6.
728x90
728x90

파이썬 Random Password 생성 Tool

업무를 하다 보면 3개월에 한 번씩 패스워드를 변경해야 하거나 시스템 혹은 Database등과 같은 곳에 새로운 계정을 만들게 되면 패스워드를 여러 규칙에 따라서 작성해야 한다.

대부분 여러 규칙에는 8자리 이상, 대/소문자, 숫자, 특수문자를 넣어서 사용하는 경우가 많이 있는데 이것을 신경 쓰면서 생성하는 게 매우 귀찮은 일이고 비슷한 형태로 생성을 하게 된다.

그래서 간단하게 파이썬을 이용해서 해당 규칙에 적합하도록 패스워드를 생성하는 Tool을 만들어 보았다. 

[Python] 파이썬 Random Password 생성 Tool

물론 패스워드를 관리하는 부분은 KeePassXC라는 소프트웨어를 사용하고 있다. 

해당 소프트웨어에 대해서는 다음에 소개하도록 하겠다.

 

KeePassXC Password Manager

Today, we are releasing KeePassXC 2.7.6 with a few bug fixes and enhancements. This version fixes a crash on macOS that occurred on exit. We also improved the visual display when dragging entries to move/copy, Quick Unlock is now automatically activated wh

keepassxc.org

그럼 제작한 Tool 사용 방법에 대해서 간단히 알아보도록 하자.

728x90

1. 환경 설명

해당 Tool은 다른 라이브러리 설치 없이 기본적으로 파이썬에서 제공하는 라이브러리를 이용해서 작성하였다.

또한 Python 3.x 버전에서는 구동하는데 전혀 문제가 없다.

  • random
  • sys
  • string
  • argparse

1.1 Python Code

이제 작성한 파이썬 Code를 살펴보도록 하자.

기본적으로 패스워드의 길이는 16자리이며, 대/소문자, 숫자, 특수문자로 생성하도록 되어 있다. 

# Python Code
# -*- coding: utf-8 -*-
import sys
import string
import argparse
from random import choice


def get_random_password(length=16):
    """
    Get Random Password
    :param length: Default Length 16
    :return:
    """
    punctuation = r"$^+_-&"
    return "".join(choice(string.ascii_letters + string.digits + punctuation) for _ in range(length))


if __name__ == "__main__":
    parser = argparse.ArgumentParser(prog='Random Password', description='Generate Random Password')
    try:
        parser.add_argument('-c', '--count', type=int, nargs='?', const=16,
                            help='Random password digits (16 by default)')
        args = parser.parse_args()
        if args.count:
            print(get_random_password(args.count))
        else:
            print(get_random_password())
    except Exception as e:
        print(e)
        sys.exit(0)

2. 사용 방법

해당 파이썬 Code를 이용해서 한번 사용해 보도록 하자.

2.1 Help(도움말)

우선적으로 정상적으로 구동되는지 어떤 정보가 있는지 확인하기 위해서 먼저 Help 옵션을 이용하여 실행해 보도록 하자.

  • $ python random_pw.py -h
  • $ python random_pw.py --help
# Help 옵션 확인

$ python random_pw.py -h
usage: Random Password [-h] [-c [COUNT]]

Generate Random Password

options:
  -h, --help            show this help message and exit
  -c [COUNT], --count [COUNT]
                        Random password digits (16 by default)

2.2 Random Password 만들기

그럼 이제 Random Password를 만들어 보도록 하자.

먼저 기본적으로 실행을 하게 되면 16자리의 대/소문자, 숫자, 특수문자의 Password가 생성된다.

  • $ python random_pw.py
# Python 실행

$ python random_pw.py
9IGprBSedu$FrgHi

$ python random_pw.py
kMmtprR+SNeQ_L5S

이번에는 -c 옵션을 이용해서 자릿수를 변경해 보도록 하자.

  • $ python random_pw.py -c 8
  • $ python random_pw.py -c 20
# Python 실행

$ python random_pw.py -c 8
Pt4wsw$A

$ python random_pw.py -c 20
jc$j_h7mVA6To&h1sF78
728x90

3. 예제 Code

기본 라이브러리를 이용해서 Random Password를 생성하는 Tool을 알아보았다. 

위에 나온 예제 파일은 Github에 올려 놓았다. 

해당 내용에 대해서 문의 있거나 궁금한 내용이 있다면 Github Issus 혹은 댓글로 남겨주면 최대한 빠르게 답을 할 수 있도록 하겠다. 

 

GitHub - happylie/python-example-code: Python Example Code

Python Example Code. Contribute to happylie/python-example-code development by creating an account on GitHub.

github.com

728x90
728x90


🌵댓글

 

loading