LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-07-2014, 04:40 PM   #1
sherzod33
LQ Newbie
 
Registered: Oct 2014
Posts: 1

Rep: Reputation: Disabled
need help to generate dynamic password between 8 - 16 characters


i have password generator but i cant get it to generate dynamic length. any idead how to do it?

if [ $# -eq 0 ]; then
num=("0" "1" "2" "3" "4" "5" "7" "8" "9")
special=("@" "#" "$" "%" "*" "-" "+")
upper=(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)
lower=(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)
lower2=${#lower[*]} #${lower[$((RANDOM%lower2))]}
upper2=${#upper[*]} #${upper[$((RANDOM%upper2))]}
num2=${#num[*]} #${num[$((RANDOM%num2))]}
special2=${#special[*]} #${special[$((RANDOM%special2))]}

echo "${special[$((RANDOM%special2))]}${num[$((RANDOM%num2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}" | fold -w 1 | shuf | tr -d '\n'
echo
fi
 
Old 10-07-2014, 04:51 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
A little off the wall, first can you encap your code in CODE tags, will help from getting a 10ft horizontal scroll bar

You can use random to generate a random number between a set of integers, then use that as your length for your password. Having a hard time decyphering this pw generator script so couldnt tell you how to implement in your current code, just thinking about this I might do it slightly different myself. I would just set one array and then use random in a while loop to append a character until it reached the pwlength value.


Code:
pwlength=$((RANDOM%16+8))
 
Old 10-07-2014, 09:00 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,512

Rep: Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541
Depending on how those passwords are intended to be used, you might want to filter out obscenities, threats against public officials, remarks that might be considered offensive to Belgians, etc.

How to do that?? Hmmmm,....

You could publish the password candidate on Facebook and see if anyone notices a problem with it.

Or, just don't use any vowels.
 
Old 10-09-2014, 02:04 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
If you want to generate random password between 8-16 character. Try this.

Code:
#!/bin/bash
n=$(shuf -i 8-16 -n 1)
cat /dev/urandom | tr -dc 'a-zA-Z0-9-!@#$%^&*()_+~' | fold -w $n | head -n 1

But you want to use your code and you want modification in it, try this.
Code:
#/bin/bash
if [ $# -eq 0 ]; then
num=("0" "1" "2" "3" "4" "5" "7" "8" "9")
special=("@" "#" "$" "%" "*" "-" "+")
upper=(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)
lower=(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)
lower2=${#lower[*]} #${lower[$((RANDOM%lower2))]}
upper2=${#upper[*]} #${upper[$((RANDOM%upper2))]}
num2=${#num[*]} #${num[$((RANDOM%num2))]}
special2=${#special[*]} #${special[$((RANDOM%special2))]}
n=$(shuf -i 8-16 -n 1)
echo "${special[$((RANDOM%special2))]}${num[$((RANDOM%num2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${special[$((RANDOM%special2))]}${num[$((RANDOM%num2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}${lower[$((RANDOM%lower2))]}${upper[$((RANDOM%upper2))]}" | fold -w $n | head -n 1
fi

Last edited by eklavya; 10-09-2014 at 02:08 AM.
 
Old 10-09-2014, 02:17 AM   #5
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Not really answering the question but did you check out the binary pwgen?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate SPECIAL alphanumeric WORDLIST - no characters appearing more than X times Stanley_212 Programming 4 01-20-2013 07:46 PM
[SOLVED] generate dynamic variable in unix vaibhavs17 Linux - General 2 05-02-2012 11:06 AM
[SOLVED] Reading lines to an array and generate dynamic zenity list jomann Programming 15 05-24-2011 10:40 PM
.htaccess - how do I strip characters from a dynamic url? using Mod_rewrite kilaz Linux - Server 3 10-15-2008 12:28 PM
How do I generate ASCII control characters from they keyboard? Mahonri Linux - General 2 09-20-2007 08:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration