2
2
28r2015-10-13 17:01:22
bash
28r, 2015-10-13 17:01:22

How to write a password generator in bash?

Decided to learn bash.
There is a need to generate digital passwords according to the following scheme.
At the first stage, the user selects the time range of the password (for example, in the format dd.mm.yyyy hh.mm - hh.mm), not less than an hour and not more than 10 hours. If a password has already been generated for this time period, then the user is informed about this (for example: It is impossible to generate a password for the interval 10/30/2015 22.00-23.00 Since there is a password for the interval 10/30/2015 21.00-10.31.2015 01.00).
At the second stage, the password is generated and saved to a file, the file name corresponds to the time range of the password.
At the third stage, the contents of the file (possibly with a name) are displayed on the screen and (ideally) printed on the printer.
Obstacles appeared already at the first stage.
#!/bin/bash
MATRIX="0123456789"
LENGTH="8"
while [ "${n:=1}" -le "$LENGTH" ]
do
PASS="$PASS${MATRIX:$(($RANDOM% ${#MATRIX})):1}"
let n+=1
done
echo "$PASS"
On the same page where I found this example, I met a negative opinion about this construction and a recommendation to use the following one (completely unreadable for me).
/dev/urandom | grep -o '' | head -n 5 | tr -d '\n'; echo
Is this task even feasible in bash?
PS learned the command to print text files
lpr <filename>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alexander, 2015-10-13
alexandr

base64 /dev/urandom | head -10 | tr -d -c '0-9' | cut -c1-8

We encode a random sequence from /dev/urandom into ASCII using base64, get the first 10 lines of head -n10, remove all of them except for the numbers tr -d -c '0-9' (newlines are also removed, we get one line from digits), and leave the first 8 characters from them cut -c1-8.

A
AVKor, 2015-10-13
@AVKor

To get started, you can see how Perl is done:
cat `which makepasswd`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question