LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Blogs > sudhagud
User Name
Password

Notices

Rate this Entry

Password generator for Linux - Bash Script

Posted 12-07-2011 at 01:05 PM by sudhagud

Getting on with system administration means to have a constant security aware work flow. I have written a simple script to generate a fairly complex password. By default it creates a 8 character length. If a length is given then it can be between 9-32. Anything above 32 is truncated to 32 characters and anything less than 9 is defaulted to 8.

Suggestions are welcome. The script itself.

Code:
#!/bin/bash
##################################################################
#
# Script to generate a fairly complex password
# 
##################################################################
#
# Author: Bellamkonda Sudhakar
# License: GNU GPL v3
##################################################################
VERSION=0.1
##################################################################
function uerror {
printf "%s\n" "Not root"
}
function numerr {
printf "%s\n" "Numbers only"
}
##################################################################
function usage {
printf "\n%s\n" "Password Generator V0.1"
printf "\n%s" "pwgen <Option>"
printf "\n%5s%s" " " "-n where n can be a numeric between 9-32, when not given default is 8"
printf "\n%5s%s\n" " " "-h displays this usage page"
}
#################################################################
function pgen {
PASSWORD=`tr -dc A-Za-z0-9_ < /dev/urandom | head -c $L`
printf "%s\n" "$PASSWORD"
}
##################################################################
#Main Program
L=8
#
if [ $UID -ne 0 ] ; then uerror; exit 0; fi
#
if [ "$1" == "-h" ] ; then usage; exit 0; fi
#
if [ $# -eq 0 ] ; then pgen; exit 0; fi 
#
if [ $1 -eq $1 2>/dev/null ]
then
    if [ $1 -gt 32 ] ; then L=32; fi
    if [ $1 -lt 32 -a $1 -gt 8 ] ; then L=$1; fi
    pgen 
else
    numerr
    usage
    exit 0
fi
Make things easy for yourself, use it and have fun.

- Bellamkonda Sudhakar
Views 2087 Comments 1
« Prev     Main     Next »
Total Comments 1

Comments

  1. Old Comment
    There are some other easier ways to generate a password in bash.

    As an example,
    date +%s | base64 | sha256sum | head -c 16 ; echo

    Some other ways are listed at this page.
    Posted 11-07-2014 at 11:30 AM by illiak illiak is offline
 

  



All times are GMT -5. The time now is 06:10 PM.

Main Menu
Advertisement

Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration