Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

UNIX Bash – Random Secure Password

Posted on April 9, 2019 By David Kittell

Tested on Raspberry Pi but should work on any Debian based OS

# Install Dictionaries
if [ $(dpkg-query -W -f='${Status}' wamerican 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  sudo apt install wamerican -y;
  else
  echo 'Already Installed';
fi

if [ $(dpkg-query -W -f='${Status}' wbritish 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  sudo apt install wbritish -y;
  else
  echo 'Already Installed';
fi

# This will combine all words into a new file that we can filter for bad words.
sort /usr/share/dict/american-english /usr/share/dict/british-english /usr/share/dict/words | uniq | sudo dd of=words-password

# This will be your badwordlist, fill it with the words you would not like to see in a password
sudo touch /usr/share/dict/badwordlist

NOTE: With the three languages included you may get some words for your password that may be offensive if you choose not to put words in the badwordlist file. If so just run the script again.

#!/bin/bash

CleanUpPassword () {
  DirtyPassword="$1"
  #echo "Dirty Password: $DirtyPassword"

  # Replace i, I, l with a number one
  CleanPassword=$(echo "$DirtyPassword" | sed "s|i|1|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|I|1|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|l|1|g" )

  # Replace a, A with @
  CleanPassword=$(echo "$CleanPassword" | sed "s|a|@|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|A|@|g" )

  # Replace e, E with 3
  CleanPassword=$(echo "$CleanPassword" | sed "s|e|3|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|E|3|g" )

  # Replace s, S with $
  CleanPassword=$(echo "$CleanPassword" | sed "s|s|$|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|S|$|g" )

  # Replace o, O with a number zero
  CleanPassword=$(echo "$CleanPassword" | sed "s|o|0|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|O|0|g" )

  #echo "Clean Password: $CleanPassword"
  echo "$CleanPassword"
}

# Get Random Word - Start
GetRandomWord () {
passwordwordlist=/usr/share/dict/words-password

sWord1=$(shuf -n1 $passwordwordlist);
chrlen=${#sWord1}
#echo $chrlen;
if [ $chrlen -lt 4 ];
then
GetRandomWord;
elif [ $chrlen -gt 8 ];
then
GetRandomWord;
fi
#echo $sWord1;
}

GetRandomWordMod () {
  GetRandomWord
  #echo $sWord1
  
first=`echo $sWord1|cut -c1|tr [a-z] [A-Z]`;
second=`echo $sWord1|cut -c2-`;
sWord2=$(echo $first$second);

    sWord3=$(echo "$sWord2" | sed 's/a/@/g' | sed 's/e/3/g' | sed 's/i/1/g' | sed 's/o/0/g' | sed 's/u/_/g' | sed 's/y/-/g')
 
  #echo $sWord3
}

CreatePassword () {
  # Change Disk Password - Start
  # Password Format:
  # ##.Word$.Oth3r
  # Numbers. Word with Caps and Special Character.Word again
  # 12-20 Characters in length

  # Get two digit random number - Start
  sPWDPart1=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 0-9 | head -c 2; echo)
  #echo $sPWDPart1
  # Get two digit random number - Stop

  GetRandomWordMod
  sPWDPart2=$sWord3

  GetRandomWordMod
  sPWDPart3=$sWord3

  sFullPWD=$(echo "$sPWDPart1.$sPWDPart2.$sPWDPart3")
  #sFullPWD=$(cat ~/FileVault.txt)
  # Change Disk Password - Stop

  printf "\nProposed Password:\n$sFullPWD\n\nAcceptable?, if yes type y or if no type n followed by [ENTER]:\n"
  read sPWDCreated
  case $sPWDCreated in
    [yY])
      
      clear

      echo "\nWrite down this ${#sFullPWD} character password:\n$sFullPWD"

     
      ;;
    *)
      CreatePassword
      ;;
  esac

}

CreatePassword
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code Debian Raspberry Pi Raspbian (Raspberry Pi OS) Ubuntu UNIX UNIX Shell Scripts Zorin OS

Post navigation

Previous post
Next post

Related Posts

Folder Delete Everything

Posted on December 14, 2013October 26, 2015

Careful with this function as it can really do some serious damage if you choose the wrong folder. ‘Folder Delete Everything – Start Private Sub DeleteDirectory(ByVal dir_name As String) Try Dim file_name As String Dim files As Collection Dim i As Integer ‘ Get a list of files it contains….

Read More

PowerShell – Install MySQL

Posted on February 9, 2016

Recently I had to install MySQL and configure it rather quickly, hopefully this will help someone else First off install Chocolatey Then Install MySQL choco install -y mysql All information on this site is shared with the intention to help. Before any source code or program is ran on a…

Read More

Classic ASP Send Email

Posted on March 3, 2014May 25, 2017

<% Set myMail=CreateObject("CDO.Message") if Request("subject") <> "" then myMail.Subject=Request("subject") else myMail.Subject="<Subject>" end if if Request("from") <> "" then myMail.From=Request("from") else myMail.From="<From Email Address>" end if if Request("To") <> "" then myMail.To=Request("To") else myMail.To="<To Email Address>" end if if Request("Cc") <> "" then myMail.CC=Request("Cc") else myMail.CC="<CC Email Address>" end if myMail.TextBody=…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes