Advertisement
Guest User

pw.sh

a guest
Jun 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.83 KB | None | 0 0
  1. #! /bin/bash
  2. #Author: Dylan Ivan Hubbard
  3. #Name: PasswordGenerator
  4.  
  5.  
  6. ##########################################################################################################
  7.  
  8.  
  9.                 GENERROR1 (){
  10.  
  11.  
  12. # Creating our error function which will inform the user on the correct script usage.
  13. # tpuf setaf will display our guidelines in a distinctive color, allowing the
  14. # user to recognize it properly.
  15.  
  16. ###########################################################################################################
  17.  
  18.     tput setaf 3
  19.  
  20.         echo -e "Password is incorrect, guidelines provided: \n"
  21.  
  22.    echo -e " MUST contain at least 1 digit
  23.         MUST contain at least 1 uppercase character
  24.         MUST contain at least 1 lowercase character
  25.         MUST contain ONE special character
  26.         MUST be a minimum of 8 characters long
  27.  
  28.            Have a g'day, mate. "
  29.  
  30.  
  31.     tput setaf 7
  32.  
  33. exit 0
  34.  
  35. }
  36.  
  37. ##########################################################################################################################################
  38. #PASSWORD FUNCTION
  39.  
  40.  
  41.  
  42.                 PASSWORDGEN (){
  43.  
  44.     tput setaf 2
  45.  
  46.  
  47.    echo -n "Here is your password, eh: "
  48.  
  49.  
  50.     pick() { echo ${1:RANDOM%${#1}:1}; }
  51.  
  52.  
  53. {
  54.  
  55.         pick '@#$%&*+-='
  56.          pick '0123456789'
  57.                 pick 'abcdefghijklmnopqrstuvwxyz'
  58.                     pick 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  59.  
  60.  
  61.  
  62.         for i in $( seq 1 $(( 4 + RANDOM % 8 )) )
  63.  
  64.  
  65. do
  66.  
  67.  
  68.  
  69. pick '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  70.  
  71.  
  72.  
  73.  
  74.              done
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.       } | sort -R | tr -d '\n'
  83.  
  84.  
  85.  
  86.  
  87.       echo ""
  88.  
  89.  
  90.  
  91.  
  92. tput setaf 7
  93.  
  94.  
  95.                  exit 1
  96.  
  97.          }
  98.  
  99. ##################################################################################################################################
  100.  
  101. # Assuming the user enters a password and not a parameter such as "-h", we will check the length of the password entered,
  102. # It cannot be less than 8 characters and not more than 16.
  103.  
  104.         if [ $# -eq 1 ]
  105.  
  106.  
  107.  
  108.                   then
  109.  
  110.  
  111.  
  112.           USERPASSWORD=$(echo -n $1 | wc -c)
  113.  
  114.  
  115.  
  116.                    fi
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.        if  [[ $USERPASSWORD -gt 16  ||  $USERPASSWORD -lt 8 ]]
  124.  
  125.  
  126.                 then
  127.  
  128.  
  129.  
  130.                 echo "Error - Invalid Length - $1"
  131.  
  132.  
  133.  
  134.                   error=1
  135.  
  136.  
  137.                     fi
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. ###################################################################################################################################
  148.  
  149.  
  150. # Here we will read what the user's input is, we will check if the first
  151. # positional parameter contains any characters, if not, we will generate a
  152. # password for the user according to our guidelines, but if -h is our
  153. # parameter, our script will list the guidelines in order, to use the
  154. # script accordingly.
  155.  
  156.             if [ "$1" = "" ]
  157.  
  158.         then
  159.  
  160.             PASSWORDGEN
  161.  
  162. fi
  163.  
  164.  
  165.  
  166.  
  167.             if [ "$1" = "-h" ]
  168.  
  169.  
  170.         then
  171.  
  172.             GENERROR1
  173.  
  174. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement