Recently I started using Python more and more in my work because it is such a versatile language. One can do a whole range of stuff with it.
Build a UI ? ✔
Build an API️ ? ✔
Write a script to scrape a website ? ✔
Control the mouse on your PC? ✔
There is almost nothing that you cannot do using Python. It is pretty simple to learn too (almost feels like English — at least the basics) , and the community is great. There are so many resources and libraries for Python out there that there is always confusion as to what to choose.
Recently, I took up the task to renew SSL certificates for my team. While doing that I had to set a password. Previously when we had generated the certs, we just created a password which we could remember easily - a modified version of our team name - not the best practice for passwords. So I decided to write a simple python script to generate a random password. Took me barely 2 minutes to come up with a script. Very rudimentary, right?
Then I thought, well why don’t I make it more generic with input for length of the password as an argument. So I went ahead and modified it to this. Better? Yes. Can it be even better? Yes, definitely.
This version just takes length as the parameter. But sometimes we have minimum requirements for passwords such as there should be special characters, numbers etc., but there is no standard to that. Everyone has their own requirements for a password. So I thought of providing the option for what kind of password should be generated as a user input too. I came up with this updated version. Well, that should solve most of our problems, right?
Ein moment bitte, how do I use this?
Well, you have to have a copy of this script, install python and then run
python script.py <length> <option> .
This worked for me, but I wasn’t satisfied. How can this be made even more simpler for people to understand 🤔? People should be able to just say
generate <length> <option> to generate a password. Enter CLI tools.
Python to the rescue again. There are a number of libraries in python to achieve this. The most popular ones are argparse, click and fire. I had previously looked at these options at work during a Hackathon to build a CLI tool. I went with click then because it seemed to be the best available option for building complex CLI tools. (The hackathon project was related to auto-generating boilerplate code in Java - a story for another day). I went with click here too, and converted that script to a CLI tool - ranpass.
Currently it has options for generating 4 different types of passwords of any length:
- Only lowercase alphabets
- Lowercase + uppercase alphabets
- Alphanumeric + Special characters (Default option, because passwords should be as secure as possible)
To use it ensure you have python installed in your system and then do a
pip install ranpass After this generating a password is just as simple as typing
ranpass generate -l 12
This is the story of why I built the CLI tool, not how. I shall be writing about how to build, package and distribute a CLI tool built with Python in a future article. Cheers!