LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Possible passwords from one column text file. (http://www.linuxquestions.org/questions/programming-9/possible-passwords-from-one-column-text-file-4175453540/)

andrew44 03-10-2013 04:13 PM

Possible passwords from one column text file.
 
Hi,

I have lost the password for an encrypted volume.

I have written a script to input the passwords.

I have written down all the possible passwords into a text file. I know the password I have is a combination of two of these

The file contains something like

password1
password2
password3

I want something to generate a new list of passwords covering all the permutations with a space between each.

password1 password2
password1 password3
password2 password1
password2 password3

you get the idea.

Any help is much appreciated!

rayfordj 03-10-2013 05:09 PM

Probably not the best, but this may get you along your way...

Code:

for i in $(cat /tmp/pw-file ); do for j in $(cat /tmp/pw-file ); do echo ${i} ${j}; done; done
where /tmp/pw-file is your file with passwords

andrew44 03-10-2013 05:42 PM

Thanks
 
Perfect thanks very much.

If I want three sets of passwords how would I do that?

for i in $(cat passwords ); do for j in $(cat passwords ); do for k in $(cat passwords ); do echo ${i} ${j} ${k}; done; done

Doesn't work?

devnull10 03-10-2013 05:45 PM

You're missing a "done".

andrew44 03-10-2013 05:49 PM

Thanks again!

andrew44 03-10-2013 06:11 PM

What happens if I have spaces in the line?

honi soit qui mal y pense

becomes
i soit qui mal y pe i soit qui mal y pe

if I run

for i in $(cat passwords ); do for j in $(cat passwords ); do echo ${i} ${j}; done; done

chrism01 03-10-2013 11:23 PM

First, put the script on multiple lines; much easier to read. Second, set bash env var IFS to newline eg
http://ubuntuforums.org/showthread.php?t=820403
Code:

# set IFS to newline only; default is space or tab or newline
IFS=$'\n'

for i in $(cat passwords )
do
    for j in $(cat passwords )
    do
        echo ${i} ${j}
    done
done

Note that if you need to reset the IFS during the script, save the old value away to a var and re-assign as reqd.

When the script finishes, IFS will of course be whatever the calling shell value was; it doesn't get changed unless you source the script or run it directly at the cli.

millgates 03-11-2013 04:43 AM

Quote:

Code:

for i in $(cat passwords )

It is much better to read files with while and read (Don't read lines with for):

Code:

while read i; do
    ...
done < passwords

Quote:

Code:

echo ${i} ${j}

The braces here are not necessary. On the other hand, the quotes are:

Code:

echo "${i} ${j}"
Another alternative:

Code:

mapfile -t words<passwords
for i in "${words[@]}"; do
    for j in "${words[@]}"; do
        echo "$i $j"
    done
done


andrew44 03-17-2013 07:26 PM

Thanks to everyone that helped. Problem solved.


All times are GMT -5. The time now is 04:15 AM.