I have writen a simple code that should ping several ips at the same time using threading methode. It seems like the code is running well and able to execute the task but the problem is that the script keeps pinging the same ips several times, although the range
for the threads set to be as the number of the ips in the list.
Here is the code:
import os from threading import * from time import * def ipList(): while True: ip = input("Insert an ip") ip_list.append(ip) if ip == "end": ip_list.pop() break def ipPinger(ip_list): for ip in ip_list: response = os.popen(f"ping -c 4 {ip}").read() if "--- " in response: result = response[response.find("--- "):] print(result) ip_list = [] ipList() print(ip_list) q = input("Are you sure that this is the right list? Y/N") if q == "Y": print("Loading...") else: ipList() num = 0 for i in range(len(ip_list)): t = Thread(target=ipPinger, args=(ip_list[num + i:],)) t.start()
and here is the output i get:
Insert an ip8.8.8.8 Insert an ip1.1.1.1 Insert an ip8.8.4.4 Insert an ipend ['8.8.8.8', '1.1.1.1', '8.8.4.4'] Are you sure that this is the right list? Y/NY Loading... --- 1.1.1.1 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3005ms rtt min/avg/max/mdev = 8.132/9.569/10.592/1.017 ms --- 8.8.8.8 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3004ms rtt min/avg/max/mdev = 47.767/48.436/49.066/0.547 ms --- 8.8.4.4 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3004ms rtt min/avg/max/mdev = 47.001/47.400/48.047/0.434 ms
the upper pings printed after 4s and the lower pings printed after another 4s
--- 1.1.1.1 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3006ms rtt min/avg/max/mdev = 8.620/8.661/8.742/0.047 ms --- 8.8.4.4 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3005ms rtt min/avg/max/mdev = 47.378/48.118/48.791/0.571 ms --- 8.8.4.4 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3004ms rtt min/avg/max/mdev = 47.025/48.122/49.402/1.023 ms
I tried to set a different range to the threads, different ways of setting the args in Threads but didnt find a solutins.
ps. I’m total newbie in code so maybe I dont see something super obvies but I really didnt found a solution for this on the web so just wanted to understand what I`m doing wrong here
Advertisement
Answer
It seems the problem is that you’re splitting the list of num+i and forward and not grabbing the index value of num+i. (Also since num is never incremented in your bottom loop, atm it serves no function)
Let’s say I have a list that looks like this:
['1.1.1.1','8.8.8.8','8.8.4.4']
Currently you’re splitting it like this over the 3 iterations of spawning threads:
['1.1.1.1','8.8.8.8','8.8.4.4'] ['8.8.8.8','8.8.4.4'] ['8.8.4.4']
This is simply caused by your : operator in your indexing when spawning the thread here:
for i in range(len(ip_list)): t = Thread(target=ipPinger, args=(ip_list[num + i:],)) #Colon operator in indexing t.start()
Since your IpPing function takes a list as an argument and a loop through that list it will then loop through the entire list and ping them all, for each thread.
I would also like you to consider that instead of spawning a thread for each IP to ping (in case a big list may be supplied), that you instead may want to spawn either a fixed amount of threads or a variable amount of threads specified by the user.
Then have each thread pop values from the same list, or handle accessing that list through locks. Since the pinging here takes long and the access to the object is very short.
Good luck with your coding! :-)
Edit: Wording.