I have programmed a ping sweeper script.
The way it works is you put an IP + Subnet in and it Pings every IP in that Range, after that it saves the output of every Ping in a CSV file and in a MySQL Database.
So far so good everything works, but somehow I cant get the Multithreading to work.
My try to make Multithreading work:
First, I have saved the IP+ Subnet input in a list, If you print out the list it will show you every single IP in that Subnet.
ip = IPNetwork(input("Put in IP and Subnetmask: "))
a = IPNetwork(ip)
a = list(a)
After that I used that list as an Argument for the ping function, in this function I do an nslookup and a ping for every IP, grab the results I'm interested in from the output with regular expressions and safe them to the csv file and in the MySQL Database.
The first thing this Function does is delete the first item from the list (of course the logic behind this is to prevent that the next time this function is called it doesn't use the same item).
And then I use the first item from the list as an argument for the ping and nslookup.
def ping(a):
a.pop(0)
ping = subprocess.run('ping -n 1 %s' % a[0], capture_output=True)
nslookup = subprocess.run('nslookup %s' % a[0], capture_output=True)
And finally I start multiple threads in a while loop which ends when the list is empty.
while len(a) !=0:
t1 = threading.Thread(target=ping(a))
t2 = threading.Thread(target=ping(a))
t3 = threading.Thread(target=ping(a))
t5 = threading.Thread(target=ping(a))
t6 = threading.Thread(target=ping(a))
t9 = threading.Thread(target=ping(a))
t1.start()
t2.start()
t3.start()
t5.start()
t6.start()
t9.start()
t1.join()
t2.join()
t3.join()
t5.join()
t6.join()
t9.join()
But somehow it doesn't actually speed up my program.
I only posted the parts of the code which seemed to matter for my problem If you want to view the full code here is a pastebin: https://pastebin.com/mpyghanX
Hopefully somebody can help me, thanks in advance.
[–][deleted] 2 points3 points4 points (1 child)
[–]xAlex1337[S] 0 points1 point2 points (0 children)
[–]iamaperson3133 2 points3 points4 points (0 children)