Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

bash 14 character password

Posted on 2010-11-22
5
Medium Priority
?
465 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 2000 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

580 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