Bash Loop for password gen


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Loop for password gen
# 1  
Old 09-15-2013
Bash Loop for password gen

Hey guys, I'm trying to make a bash script to do password generation.
The script takes 2 arguments, number of characters and number of passes to generate, but I can't get the loop to work properly.

Code:
#!/bin/bash
echo
echo
echo -ne "Password length:"
read pwd_length
echo
echo -ne "Number of passes to generate:"
read pwd_number

char=(0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ! @ \# $ % ^ \& \+ - \) \( = )

counter=0
while [ $counter -le $pwd_number ]
do
max=${#char[*]}
for i in `seq 1 $pwd_length`
do
let rand=${RANDOM}%${max}
counter=$(( $counter + 1 ))
out="${out}${char[$rand]}"
done

echo
echo "$out"
done

# 2  
Old 09-15-2013
Unless you just have to create a new script, check out this bash function:
Bash Random Password Generator | LegRoom.net
# 3  
Old 09-16-2013
Yeah I saw some of the other ones around, I just want to know what is wrong with my script for the future
# 4  
Old 09-16-2013
What is wrong with your script? What is the error?

--ahamed
# 5  
Old 09-16-2013
When it runs it only ever spits out a max of 2 results, the second if always 50% of the first and then another x number of chars, see below:

Code:
Password length:2

Number of passes to generate:3

9a

9aCk

# 6  
Old 09-16-2013
Try these changes...
Code:
...
counter=0
max=${#char[*]}

while [ $counter -lt $pwd_number ]
do
  out=""
  for i in `seq 1 $pwd_length`
  do
    let rand=${RANDOM}%${max}
    out="${out}${char[$rand]}"
  done

  counter=$(( $counter + 1 ))
  echo
  echo "$out"
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 09-16-2013
Legend, that's fixed it, thanks bro!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script for password changes help

I am super new to scripting and I am trying to create a bash script that is interactive that will change other user passwords as well as a few other thing (ie. change SMB pw, see pw expiration,lock/unlock user). I have started it by making it check to see if the username entered is in the... (2 Replies)
Discussion started by: thumbelina
2 Replies

2. Shell Programming and Scripting

Make a password protected bash script resist/refuse “bash -x” when the password is given

I want to give my long scripts to customer. The customer must not be able to read the scripts even if he has the password. The following command locks and unlocks the script but the set +x is simply ignored. The code: read -p 'Script: ' S && C=$S.crypt H='eval "$((dd if=$0 bs=1 skip=//|gpg... (7 Replies)
Discussion started by: frad
7 Replies

3. Shell Programming and Scripting

Bash for multiple accounts with auto-gen passwords

Hello, I am studying few things on unux and scripting. I need a script to create bulk users in unux. I need some assistance from you for creating 100 or more User IDs using a bash script: Here's my requirements: 1. I need to create 100 or even more user ids of different naming... (1 Reply)
Discussion started by: Shelldorado
1 Replies

4. Shell Programming and Scripting

loop picks up password for 2 entry...how to avoid that ?

hello all, i am trying to find a better to do what i am doing right now... i have a file called sidlist...which has my database_name and password to the respective database so something like below.. file is called sidlist and entry is below... test, abc123 kes12, abcd12 pss, abcd1234... (5 Replies)
Discussion started by: abdul.irfan2
5 Replies

5. Shell Programming and Scripting

loop to read multiple username and password

Hello all, I am i am trying to read username password. Bassicaly, i have file called sidlist and it has my database name, username and password.... looks something like this.... db1, user1, pass1 db2, user2, pass2 db3, user3, pass4 but i dont know how to make it work, until i get... (4 Replies)
Discussion started by: abdul.irfan2
4 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. OS X (Apple)

Multiple hosts SSH NO PASSWORD - each time it overrides the last key gen

Hello, here is my problem: I have 20 machines that need to SSH into 1 machine throughout the day. The issue is every time I go through the process of putting my keys from one of the computers that needs to SSH to the server that needs to accept all the incoming SSH's it overrides the last one. ... (6 Replies)
Discussion started by: yoyoyo777
6 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

While loop password creation problem

#!/bin/bash #Filename: Assignment Author: Luke Francis quit=n while do clear echo "OPERATOR ADMINISTRATIVE TOOL" clear echo "1. User Information" echo "2. Network Connectivity" echo "3. Processes" echo "4. System Information" echo "5. Hardware Utilization" echo "Which option do you... (3 Replies)
Discussion started by: warlock129
3 Replies

10. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies
Login or Register to Ask a Question