Register
It is currently Sun Sep 21, 2014 6:06 am

randpass varible


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Sun Aug 05, 2012 4:25 pm   

Joined: Sun Aug 05, 2012 4:20 pm
Posts: 7
Hey,

#Functions#

# Generate a random password
# $1 = number of characters; defaults to 32
# $2 = include special characters; 1 = yes, 0 = no; defaults to 1
function randpass() {
[ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-32}
echo
}

This generates a random password.

I want to use this function within a script to reset a users password and then email me the password but im not sure how to set the function as a varible and make them both display the same password...

please help :)


Top
 Profile  
 PostPosted: Sun Aug 05, 2012 5:14 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
hi,

simply
Code:
password=$(randPass)

btw, cat is useless.

Quote:
[...]defaults to 32
?? this means there could be no $1 :( that may never happen! (unless it's given as nothing between quotes')


Top
 Profile  
 PostPosted: Sun Aug 05, 2012 5:32 pm   

Joined: Sun Aug 05, 2012 4:20 pm
Posts: 7
Hey,

but then easy time that varible is used it generates a new password.

I want it to be the same password, so i set the users pass and then that pass is emailed to me...

Thanks


Top
 Profile  
 PostPosted: Sun Aug 05, 2012 6:11 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
at least did you try before replying?
Code:
$ test -n $var && unset var
$ myFunc() { echo $((++var));}
$ myVar=$(myFunc)
$ echo $myVar
1
$ echo $myVar
1
$ echo $myVar
1
$ echo $myVar
1
$


Top
 Profile  
 PostPosted: Mon Aug 06, 2012 2:28 am   

Joined: Sun Aug 05, 2012 4:20 pm
Posts: 7
Yes, it doesn't work, each time i echo the varible a new password is displayed.

#Functions#

# Generate a random password
# $1 = number of characters; defaults to 32
# $2 = include special characters; 1 = yes, 0 = no; defaults to 1
function randpass() {
[ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-32}
echo

}
pass1=$(randpass 15 0)
echo $pass1


Top
 Profile  
 PostPosted: Mon Aug 06, 2012 7:03 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
Code:
$ randpass() { [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"; tr -cd "$CHAR" </dev/urandom | head -c ${1:-32};}
$ pass=$(randpass '' 0)
$ echo $pass
zbBKmijoQsiMt1aC0nPkpPmYhWeWbaIA
$ echo $pass
zbBKmijoQsiMt1aC0nPkpPmYhWeWbaIA
$ echo $pass
zbBKmijoQsiMt1aC0nPkpPmYhWeWbaIA
$ echo $pass
zbBKmijoQsiMt1aC0nPkpPmYhWeWbaIA
more?


Top
 Profile  
 PostPosted: Wed Aug 08, 2012 11:56 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
blakesmoore wrote:
Yes, it doesn't work, each time i echo the varible a new password is displayed.

You mean each time you run the script.

Thanks to one script and one execution, generate the pass, hold it in a variable, set it to the user and then email it.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP