Login Register






Generate possible usernames filter_list
Author
Message
Generate possible usernames #1
This script generates possible username combinations based on whatever you feed the script with, but probably most optimal for real names. That's at least what the intentional use was Cool It can take a single name and a file with one name per line

./username.py John Doe
./username.py -f names.txt (Not restricted to .txt)

PS: It's my very first time writing code in Python so please feel free to suggest how to improve, optimize and clean the code Smile

Code:
#!/usr/bin/python import sys def makeUsernames(names): result = [] for name in names: name = name.replace("\n", '') name = name.lower() parts = name.split(' ') fname = parts[0] if len(parts) > 1: lname = parts[len(parts) - 1] result.append(fname + lname) result.append(lname + fname) result.append(fname[0] + lname) result.append(fname + lname[0]) result.append(lname[0] + fname) result.append(lname + fname[0]) result.append(fname + '.' + lname) result.append(lname + '.' + fname) result.append(fname[0] + '.' + lname) result.append(lname[0] + '.' + fname) result.append(fname + '_' + lname) result.append(lname + '_' + fname) result.append(fname[0] + '_' + lname) result.append(lname[0] + '_' + fname) result.append(fname + '-' + lname) result.append(lname + '-' + fname) result.append(fname[0] + '-' + lname) result.append(lname[0] + '-' + fname) else: result.append(fname) return result names = [] if len(sys.argv) < 2: sys.exit('Syntax error: Not enough arguments') elif sys.argv[1] == '-f': if len(sys.argv) != 3: sys.exit('Syntax error: You must supply an existing file') else: filename = sys.argv[2] try: with open(filename) as f: names = f.readlines() except IOError: sys.exit('IOError: No such file or directory') else: names = [' '.join(sys.argv[1:])] if len(names) > 0: result = makeUsernames(names) for name in result: print name else: sys.exit('Unable to generate data')

Some example outputs
Spoiler: Single name "Jane Doe"
Code:
janedoe doejane jdoe janed djane doej jane.doe doe.jane j.doe d.jane jane_doe doe_jane j_doe d_jane jane-doe doe-jane j-doe d-jane


Spoiler: File with the names "John Doe" and "Eric Smith"
Code:
johndoe doejohn jdoe johnd djohn doej john.doe doe.john j.doe d.john john_doe doe_john j_doe d_john john-doe doe-john j-doe d-john ericsmith smitheric esmith erics seric smithe eric.smith smith.eric e.smith s.eric eric_smith smith_eric e_smith s_eric eric-smith smith-eric e-smith s-eric



Update:
* Added sys.exit() on errors
* The makeUsersnames() function now returns the value instead of printing it
* If only a single name/word is supplied it return that word as it is
* Fixed errors reported by @Daque. I'm at least not able to recreate them anymore using the examples[/b]
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply

Generate possible usernames #2
This script generates possible username combinations based on whatever you feed the script with, but probably most optimal for real names. That's at least what the intentional use was Cool It can take a single name and a file with one name per line

./username.py John Doe
./username.py -f names.txt (Not restricted to .txt)

PS: It's my very first time writing code in Python so please feel free to suggest how to improve, optimize and clean the code Smile

Code:
#!/usr/bin/python import sys def makeUsernames(names): result = [] for name in names: name = name.replace("\n", '') name = name.lower() parts = name.split(' ') fname = parts[0] if len(parts) > 1: lname = parts[len(parts) - 1] result.append(fname + lname) result.append(lname + fname) result.append(fname[0] + lname) result.append(fname + lname[0]) result.append(lname[0] + fname) result.append(lname + fname[0]) result.append(fname + '.' + lname) result.append(lname + '.' + fname) result.append(fname[0] + '.' + lname) result.append(lname[0] + '.' + fname) result.append(fname + '_' + lname) result.append(lname + '_' + fname) result.append(fname[0] + '_' + lname) result.append(lname[0] + '_' + fname) result.append(fname + '-' + lname) result.append(lname + '-' + fname) result.append(fname[0] + '-' + lname) result.append(lname[0] + '-' + fname) else: result.append(fname) return result names = [] if len(sys.argv) < 2: sys.exit('Syntax error: Not enough arguments') elif sys.argv[1] == '-f': if len(sys.argv) != 3: sys.exit('Syntax error: You must supply an existing file') else: filename = sys.argv[2] try: with open(filename) as f: names = f.readlines() except IOError: sys.exit('IOError: No such file or directory') else: names = [' '.join(sys.argv[1:])] if len(names) > 0: result = makeUsernames(names) for name in result: print name else: sys.exit('Unable to generate data')

Some example outputs
Spoiler: Single name "Jane Doe"
Code:
janedoe doejane jdoe janed djane doej jane.doe doe.jane j.doe d.jane jane_doe doe_jane j_doe d_jane jane-doe doe-jane j-doe d-jane


Spoiler: File with the names "John Doe" and "Eric Smith"
Code:
johndoe doejohn jdoe johnd djohn doej john.doe doe.john j.doe d.john john_doe doe_john j_doe d_john john-doe doe-john j-doe d-john ericsmith smitheric esmith erics seric smithe eric.smith smith.eric e.smith s.eric eric_smith smith_eric e_smith s_eric eric-smith smith-eric e-smith s-eric



Update:
* Added sys.exit() on errors
* The makeUsersnames() function now returns the value instead of printing it
* If only a single name/word is supplied it return that word as it is
* Fixed errors reported by @Daque. I'm at least not able to recreate them anymore using the examples[/b]
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply

RE: Generate possible usernames #3
That's a good idea. Simple and good.
But the UI is not foolproof.

This can not happen:
Code:
if sys.argv[2] == '': print 'You must include a file'

If you give not enough arguments, the array is just shorter, which results in an illegal access when you try to get the value at index 2, see here:

Code:
$ python gennames.py -f] Traceback (most recent call last): File "gennames.py", line 36, in <module> if sys.argv[2] == '': IndexError: list index out of range

Check for length of your array instead:
Code:
if len(sys.argv) != 3:

Also while this works perfectly:
Code:
$ python gennames.py data

This doesn't:
Code:
$ python gennames.py -f data No such file or directory Traceback (most recent call last): File "gennames.py", line 49, in <module> makeUsernames(names) NameError: name 'names' is not defined

The file definitely exists.
But even if it wouldn't exist, it is not good that there is a python error output for the user.
This is because you call

Code:
makeUsernames(names)

No matter what errors happened before.
You should only call this function if the code before succeeded so far.

For flexibility reasons and separation of concerns you should try to make your function makeUsernames(names) return a list of names instead of printing them. The UI and the logic should always be separated.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Generate possible usernames #4
That's a good idea. Simple and good.
But the UI is not foolproof.

This can not happen:
Code:
if sys.argv[2] == '': print 'You must include a file'

If you give not enough arguments, the array is just shorter, which results in an illegal access when you try to get the value at index 2, see here:

Code:
$ python gennames.py -f] Traceback (most recent call last): File "gennames.py", line 36, in <module> if sys.argv[2] == '': IndexError: list index out of range

Check for length of your array instead:
Code:
if len(sys.argv) != 3:

Also while this works perfectly:
Code:
$ python gennames.py data

This doesn't:
Code:
$ python gennames.py -f data No such file or directory Traceback (most recent call last): File "gennames.py", line 49, in <module> makeUsernames(names) NameError: name 'names' is not defined

The file definitely exists.
But even if it wouldn't exist, it is not good that there is a python error output for the user.
This is because you call

Code:
makeUsernames(names)

No matter what errors happened before.
You should only call this function if the code before succeeded so far.

For flexibility reasons and separation of concerns you should try to make your function makeUsernames(names) return a list of names instead of printing them. The UI and the logic should always be separated.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Generate possible usernames #5
Thanks for the feedback @Deque! I'll try to fix up those things Smile

In the future I'll add support for middle names etc as well.. But for a complete rookie like myself, first and last name is more than enough to keep track of hehe Wink
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply

RE: Generate possible usernames #6
Thanks for the feedback @Deque! I'll try to fix up those things Smile

In the future I'll add support for middle names etc as well.. But for a complete rookie like myself, first and last name is more than enough to keep track of hehe Wink
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply

RE: Generate possible usernames #7
Nice idea bro Smile
Well done ! Thanks for sis @Deque .
I learned something .

Reply

RE: Generate possible usernames #8
Nice idea bro Smile
Well done ! Thanks for sis @Deque .
I learned something .

Reply

RE: Generate possible usernames #9
Updated the code in the OP Smile If you have any ideas on missing combinations that should be in it, please let me know.. Again, thanks for your feedbacks Smile

Btw @Deque, I renamed my names.txt to just names and it did work as expected.. Can you please check again to see if it's still happening?
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply

RE: Generate possible usernames #10
Updated the code in the OP Smile If you have any ideas on missing combinations that should be in it, please let me know.. Again, thanks for your feedbacks Smile

Btw @Deque, I renamed my names.txt to just names and it did work as expected.. Can you please check again to see if it's still happening?
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply