It’s been awhile since I posted something, and even more since I posted computer stuff.

Well, this time I make a little script to generate «secure» passwords from command line with options to modify the number of generated passwords and the number of characters in each password.

The real trick it’s done through a combination of a few shell tools.

  • < : The redirection symbol, it means, send from one side to another.
  • /dev/urandom : «Unblocked» random source
  • tr : Translate or delete characters
  • head : Output the first part of files

So, let’s build the expression which brings to us the passwords. First of all we use


to send /dev/urandom output to stdout. The next step is to process the chars dropped by urandom, this can be made with tr command  and the -dc options, d deletes chars and c takes the complement of the given parameter,

</dev/urandom tr -dc '1234567890!@#$%&/()=qwertyuiopQWERTYUIOPasdfghjklASDFGHJKLzxcvbnmZXCVBNM'

, so, tr will take the complement of that ugly string and then delete it, so it removes all the chars that are not inside the long string above, leaving just an «easily» read string.

Now, if we leave the command as is, we get an extremely large string and of course, unusable as password, so we need to cut it, then we pipe to head command with the -cn option, it says «just take the first n characters of anything is given to you». By this time we set n=8, a nice length for a good password.

</dev/urandom tr -dc  '1234567890!@#$%&/()=qwertyuiopQWERTYUIOPasdfghjklASDFGHJKLzxcvbnmZXCVBNM' | head -c8

Now, you can try it!. It gives you a 8 char length password and as you can see, it can be considered(most times) a good password. As an extra, you can put an echo “” at the end of the command to tell bash that puts a new line before the prompt.

</dev/urandom tr -dc   '1234567890!@#$%&/()=qwertyuiopQWERTYUIOPasdfghjklASDFGHJKLzxcvbnmZXCVBNM'  | head -c8; echo ""

And if you want to automatize the generation of passwords and variate the number of generated passwords and the length of it, you can build a little script, I’m not gonna explain to you, but I leave it here, use it and enjoy it.

#!/usr/bin/env bash

 </dev/urandom tr -dc '1234567890!@#$%&/()=qwertyuiopQWERTYUIOPasdfghjklASDFGHJKLzxcvbnmZXCVBNM' | head -c${1}

 echo "${0##*/} c p"
 echo -e "\tc is the number of chars in each generated password"
 echo -e "\tp is the number of password to be generated\n"
 exit 1

 if [ "$#" = "0" ];then
 elif [ "$#" = "1" ];then
 if [ "$1" = "-h" ];then
 elif [ "$1" = "--help" ];then


 echo "Chars in each password: ${cpass}"
 echo "Number of passwords to generate: ${npass}"
 echo -e "Generating...\n"

 while [ ${i} -le ${npass} ];do
 echo -n -e "\t${i}: "
 genpass ${cpass}
 echo ""
 let i=i+1;

 echo ""

control $1 $2

See you soon!.


One response »

  1. maop says:

    Yo solo uso: echo $(</dev/urandom tr -cd a-zA-Z0-9|head -c10)

    De hecho ese mismo algoritmo usa groxy para su @genpass 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s