Politics and Technology.

Friday, May 1, 2009

Random Password Alias

I find myself generating passwords often. While my password tool of choice is KeePass, sometimes I just need a quick commandline tool to spit out a password. I've added the below as an alias to "randompass" on my Solaris 9 box. It also seems to work on RHEL, without the need of the trailing echo.

head -n 10 /dev/urandom | tr -cd "[:alnum:][:punct:]" | cut -c 1-12 ; echo ""

To see it in action on c-shell in Solaris, see below. I'll loop the command 10 times so I have my pick of the most visually appealing choice to use.

% alias randompass 'head -n 10 /dev/urandom | tr -cd "[:alnum:][:punct:]" | cut -c 1-12 ; echo ""'
% foreach i ( `seq 1 10` )
foreach? randompass
foreach? end

The "head" command just clips enough random bits from the psuedo random generator device for us to make a random password, which when interpreted through the shell can be both printable and non-printable bits. The "tr" transcode just pulls out from the random bits the ASCII alphanumeric and punctuation characters and discards the rest. Finally, we just "cut" the first twelve characters for our password.

I found on my c-shell on Solaris, I needed the trailing echo to force the newline, whereas on my bash shell on RHEL it wasn't required.