Password Generation and Shell Script Arguments
Contents:
-
Random Data, Cryptographic Hash Functions, Text and String Manipulation
-
Positional Parameters, Arguments, For Loops, Special Parameters
Random Data, Cryptographic Hash Functions, Text and String Manipulations
-
RANDOM
: builtin variable which generates a random integer each time that it's referenced. -
date
: use it's formatting options to control its output. -
A checksum is a numeric value computed for a block of data that is relatively unique. Checksums were and are used to verify integrity of data such as files. Checksums are actually hexadecimal numbers.
-
Pipe turns the output of the previous command as standard input in the command that follows the pipe.
-
head
outputs the first part of files or the head portion of a file. Without any option it prints the fist 10 lines of a file.-c
: prints the first k bytes of each file.-c1
: prints the first character of the file.
-
We can pipe multiple commands into a single chain of pipes using the outputs of one as stdinput to another using the pipes.
-
!
generates the most recent command used by us which starts with the character followed by!
. eg:!v
->vim luser-demo05.sh
, etc. -
shuf
: randomly select a line -
fold
: wraps each input line to fit in a specified width. If we need to change our entire line of special characters into seperate lines then we simply need a width of one. Transforms single line of text into multiple lines. -
In Unix/Linux philosophy, each program should only do one thing and that it should do it very well. eg: the
echo
command only displays output, it doesn't do anything else. -
Commands
$ man bash # search for `random`
$ echo "${RANDOM}"
$ sha1sum FILE
$ sha256sum FILE
$ ls -l /usr/bin/*sum
$ date +%s%N | sha256sum
$ type -a head
$ head -n2 /etc/passwd
$ head -n 2 /etc/passwd
$ head -2 /etc/passwd # All three are same. Different ways to write the same thing.
$ echo "testing" | head -c3
$ date +%s%N | sha256sum | head -c10
$ man shuf
$ man fold
$
Script
#!/bin/bash
# This script generates a list of random passwords
# A random number as a password
PASSWORD="${RANDOM}"
echo "${PASSWORD}"
# Three random numbers together
PASSWORD="${RANDOM}${RANDOM}${RANDOM}"
echo "${PASSWORD}"
# Use the current date/time as the basis for the password
PASSWORD=$(date +%s)
echo "${PASSWORD}"
# Use nanoseconds to act as randomization
PASSWORD=$(date +%s%N)
echo "${PASSWORD}"
# A better password
PASSWORD=$(date +%s%N | sha256sum | head -c32)
echo "${PASSWORD}"
# An even better password
PASSWORD=$(date +%s%N${RANDOM}${RANDOM} | sha512sum | head -c48)
echo "${PASSWORD}"
# Append a special character to the passsword
SPECIAL_CHARACTER=$(echo '~!@#$%^&*()_+=-' | fold -w1 | shuf | head -c1)
echo "${PASSWORD}${SPECIAL_CHARACTER}"
Positional Paramters, Arguments, For Loops, Special Parameters
Commands
$ man bash # search for Special Parameters `#`
$ man bash # search for Special Parameters `@`
$ man bash # search for Special Parameters `*`
$ man bash # search for Env Variable: PATH
$ man dirname
$ man basename
Script
#!/bin/bash
# This script generates a random password for each user specified on the command line
# Display what the user typed on the command line
echo "You executed this command: ${0}"
# Display the path and filename of the script
echo "You used $(dirname ${0}) as the path to the $(basename ${0}) script."
# Tell them how many arguments they passed in
# Inside the script they are parameters, outside they are arguments
NUMBER_OF_PARAMETERS="${#}"
echo "You supplied ${NUMBER_OF_PARAMETERS} argument(s) on the command line."
# Make sure they at least supply one argument
if [[ "${NUMBER_OF_PARAMETERS}" -lt 1 ]]
then
echo "Usage: ${0} USER_NAME [USER_NAME]..."
exit 1
fi
# Generate and display a password for each parameter
for USER_NAME in "${@}"
do
PASSWORD=$(date +%s%N | sha256sum | head -c48)
echo "${USER_NAME}: ${PASSWORD}"
done
Explaination:
The vairable of ${0}
is actually a positional parameter and Positional Parameters are variables that contains the contents of the command line.
Parameter vs Argument: A paramter is a variable that is being used inside the shell script. An argument is the data passed into the shell script. So the argument supplied in the command line becomes the value stored in a paramter.
With that in mind, the very first parameter is ${0}
which contains the name of the script itself. ${1}
stores the value of the first argument passed to the script on the command line.
We can also override a default shell program. for example overriding the head
command.
$ which head
$ sudo vim /usr/local/bin/head # New path for the `head` program
~ #!/bin/bash
~ echo 'Hello from my head'
$ chmod 755 /usr/local/bin/head
$ which head
$ which -a head # to list all available `head` command
$ head
> Hello World
$ sudo rm /usr/local/bin/head