Solved

bash 14 character password

Posted on 2010-11-22
5
453 Views
Last Modified: 2012-05-10
Hi,

I need assistance with a script which generates 14 characte password. I need to generate about 25 passwords, and they all need to have at least 2 Upper Case, 2 Numbers and 2 special characters.

Thank you in advance.

Mike
#!/bin/bash
# random password generator by typedeaF
# This program has no "real" value other than introducing some bash concepts and putting them to use.
# to strip the comments: cat this.file | grep -v '^# '


# Sets the maximum size of the password the script will generate
MAXSIZE=14

# Holds valid password characters. I choose alpha-numeric + the shift-number keyboard keys
# I put escape chars on all the non alpha-numeric characters just for precaution
array1=(
q w e r t y u i o p a s d f g h j k l z x c v b n m Q W E R T Y U I O P A S D
F G H J K L Z X C V B N M 1 2 3 4 5 6 7 8 9 0 ! @ # $ % ^ & * ( )
)

# Used in conjunction with modulus to keep random numbers in range of the array size
MODNUM=${#array1[*]}

# Keeps track of the number characters in the password we have generated
pwd_len=0

# Bash's command substitution syntax to store the results of the tput command
term_clear=$(tput clear)

# Stores the number of lines or rows on the terminal display
max_lines=$(tput lines)

# Stores the number of columns on the terminal display
max_cols=$(tput cols)

# Finds the appropriate spot to indent for horizontally centered output
indent=$(( ((max_cols / 2)) - ((MAXSIZE / 2)) ))

# Finds the vertical center of the terminal.
line_num=$(( max_lines / 2 ))

# Clear the screen
echo $term_clear

# The outer while loop starts at 0 and loops till MAXSIZE, creating a passwd char each iteration.
# The shells $RANDOM variable creates a semi-random unsigned number. This is our entropy. =x
# x simply holds some random unsigned int that will be used to make the character scramble.
# 500 was choosen for speed and nothing else. Leave out the mod 500 if you want or change it.
# The inner loop displays the password characters. Tput keeps the cursor in the proper position.
# Mod MODNUM keeps the random number inside the size of the array so it doesnt over index.
while [ $pwd_len -lt $MAXSIZE ]
do
  x=$(($RANDOM%500))
  y=0
  while [ $y -lt $x ]
  do
    ((y++))
    index=$(($RANDOM%$MODNUM))
    tput cup $line_num $(( indent + pwd_len ))
    echo -n "${array1[$index]}"
  done
  ((pwd_len++))
done

# Place the cursor at the bottom of the screen --where is usually at.
tput cup $max_lines 0

exit 0

Open in new window

0
Comment
Question by:dpoper1
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 34193041
Have you considered using apg?

apg -m 14 -n 25 -M SNC

will be pretty close (not exactly) what you want
0
 

Author Comment

by:dpoper1
ID: 34193506
This works for me, now, I would like to have it running in a cron job, in such a way that it will dump it to a plain htm file, from which I will pick up the passwords, do you happen how to accomplish that.

I need a list like:

5Cz9y4GPxJAGtA
hwu*:U~,q9?Z4i
W,o?2r}yuWc1gm
|;wK2p#(u<z!O,
=W\;TE1^a.qjXz
LsA%fa?#i9*5(l
#C}Ekv9FSk{z#.
~c4kpgINaZ]ZcD

apg -a 1 -m 14 -n 25 -M sNCl

Open in new window

0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 34193536
     

apg -a 1 -m 14 -n 25 -M sNCl >/path/to/file.txt

or if you really want HTML, then use the script below

#!/bin/bash
exec 1>/path/to/file.html

cat <<EOF
<html>
<head>
<title>Passwords</title>
</head>
<body>
<pre>
EOF

	

apg -a 1 -m 14 -n 25 -M sNCl

cat <<EOF
</pre>
</body>
</html>
EOF

Open in new window

0
 

Author Comment

by:dpoper1
ID: 34193892
Hi Tintin,

How would I be able to get a box in which they all appear, I get sometimes 25 lines some others 12. Also how would I be able to do a submit button so I could generate it on the fly.

Thanx



#!/bin/bash

> /var/www/scripts/pass.htm

exec 1>/var/www/scripts/pass.htm

cat <<EOF
<html>
<head>
<title>Passwords</title>
</head>
<body>
<pre>
<DIV ALIGN=CENTER>
<font size="6">
EOF

apg -a 1 -m 14 -n 25 -M sNCl

cat <<EOF
</font>
</DIV>
</pre>
</body>
</html>
EOF

Open in new window

0
 

Author Comment

by:dpoper1
ID: 34193928
Sometimes I get something like the output below.
ftI|m7=3v{}6fr
*XN=}$(n1*ZkRz
%"|72}J^1p~]{=
%%K3
des4vEw!}lPKX}
{xIV6+uPHn1jvd
=KP2YDR-`}9?#A
@KFwdT1~'f@~9^
yxj^aq-0[bJs]w
P&sbNHX8kE{S"d
5$e+G9d1*ce+_Z
!30%W6nwctZy6F
G8.`mY@n(\("s@
aKx#q1?wT(\Y16
hWEQX+*e[9}Z/!
JQyL>+)7
8F,:MAy[R?S[Wb
[?/}auY[rQFt1o
=di8%W.GJZ'8Tt
G1GSewp8;JVMHg
6aoGE2X@Z

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question