-2

I have an assigment, where i have two files namely, "user.txt", "pwd.txt" now I want to basically iterate over each value of these files, i.e "John" with all values in "pwd.txt", and then another value from "user.txt" with all values of "pwd.txt".

Here I want to implement threading.

here is my code,

import threading


f1 = open("user.txt", "r")
f2 = open("pwd.txt", "r")
threads = []
def brute():

    for letter in f1.readlines():
        print "[+]First Value: {}".format(letter)
        for second_letter in f2.readlines():
            print "[+]Second Value: {}".format(second_letter)
        
            threads.append(threading.Thread(target=runner, args=(letter,second_letter,)))


for thread in threads:

    thread.start()

for thread in threads:

    thread.join()
    
def runner(word1,word2):

    print("[+]I am just a worker class: {0}:{1}".format(word1,word2))

output is coming as

[+]First Value: john

[+]Second Value: test1234

[+]Second Value: socool!

[+]Second Value: abcde1234

[+]Second Value: password1

[+]Second Value: Passw0rd

[+]Second Value: adminRocks

[+]First Value: dean

[+]First Value: harry

[+]First Value: sam

[+]First Value: joseph

[+]First Value: rick

[+]I am just a worker class: john:test1234

[+]I am just a worker class: john:socool!

[+]I am just a worker class: john:abcde1234

[+]I am just a worker class: john:password1

[+]I am just a worker class: john:Passw0rd

[+]I am just a worker class: john:adminRocks

[Finished in 0.3s]

I am not sure how to print all the user values here with all the passwords in the password file. Any help much appreciated.

1 Answers1

0

Based on your post, the goal is to cross join the user and pwd files. For this, load in the pwd file once, then process each user (one per thread).

Note that I tested this using Python 3.8.

Try this code:

import threading

# create files for testing
user = "user1\nuser2\nuser3\nuser4\nuser5\nuser6\nuser7\nuser8\nuser9"
pwd = "pwd1\npwd2\npwd3\npwd4\npwd5\npwd6\npwd7\npwd8\npwd9"
with open("user.txt",'w') as f: f.write(user)
with open("pwd.txt",'w') as f: f.write(pwd)


##### main script #####

# load all pwds
f2 = open("pwd.txt", "r")
lstpwd = f2.readlines()

f1 = open("user.txt", "r")
threads = []
def brute():
    for letter in f1.readlines():
        print ("\n[+]First Value: {}".format(letter.strip()))
        for second_letter in lstpwd:
            print ("[++]Second Value: {}".format(second_letter.strip()))
            threads.append(threading.Thread(target=runner, args=(letter,second_letter,)))

    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()
    
def runner(word1,word2):
    print("[+]I am just a worker class: {0}:{1}".format(word1.strip(),word2.strip()))
    
brute()

Output

[+]First Value: user1
[++]Second Value: pwd1
[++]Second Value: pwd2
[++]Second Value: pwd3
.......    
[+]First Value: user2
[++]Second Value: pwd1
[++]Second Value: pwd2
[++]Second Value: pwd3
.......
[+]I am just a worker class: user1:pwd1
[+]I am just a worker class: user1:pwd2
[+]I am just a worker class: user1:pwd3
.......
[+]I am just a worker class: user2:pwd1
[+]I am just a worker class: user2:pwd2
[+]I am just a worker class: user2:pwd3
.......
Mike67
  • 11,175
  • 2
  • 7
  • 15
  • First of all thanks a lot mate!, also, I see your code is working completely fine, and I also tested the use case in python 2.7 which is working too, as I see you have first called f2.readlines() once you call this function without being in loop, will this create a list? and then fetch them eventually when the variable is called in the main_loop? – Saransh Rana Aug 24 '20 at 04:42
  • Yes - `lstpwd = f2.readlines()` creates a list from the lines in the file – Mike67 Aug 24 '20 at 15:39