speed up pingsweeper

speed up pingsweeper

Postby Seguino » Mon Jul 25, 2016 7:43 am

Hi,
I made a pingsweeper that is doing his job very nicely. But with long ranges it takes a lot of time, because it is going to wait for every ping that it gets no response to to time out and then start the next one.
Very short in pseudo code:
enter range
while loop through range
ping target x
if answer show host

But if there is no answer its waiting for the ping to time out before continueing.
I did the same in bash scripting and I could add a & sign to run it nearly parallel (not waiting for answer but continue loop). Does something like that exist in Python?
I tried google but could not really find an answer. The things that came up was about multithreat-ing with multiple processor cores. Not what I am looking for.
Seguino
 
Posts: 3
Joined: Mon Jul 25, 2016 7:36 am

Re: speed up pingsweeper

Postby Ofnuts » Mon Jul 25, 2016 9:39 am

See the threading module, or the multiprocessing one.
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: speed up pingsweeper

Postby Seguino » Thu Jul 28, 2016 6:13 pm

That was exactly what I meant by what I did not need, but I solved the issue, altough i cheated a little:
Here is the code:

Code: Select all
import os
1
while 255:
    
hostname "192.168.150.%s" x
    response 
= (os.system("ping -c 1 %s |grep 'bytes from' |cut -d' ' -f 4 &" hostname))
    
1


you notice the & achter the cut command in the bash script?
This makes run the pings near simultanously. That is what I mean. Does that exist in Python?
Last edited by micseydel on Thu Jul 28, 2016 6:33 pm, edited 1 time in total.
Reason: Code tags.
Seguino
 
Posts: 3
Joined: Mon Jul 25, 2016 7:36 am

Re: speed up pingsweeper

Postby Ofnuts » Thu Jul 28, 2016 7:33 pm

You didn't solve the problem with Python, but with bash... In Python this would done by spawning each "ping" instance using Popen in thread. Threading isn't about using multiple cores, but having multiple code flows running together. In fact, in Python, multithreading cannot be used to run on multiple cores in parallel, which is why there is a multiprocessing module. So you run multiple threads, but in the same core....

Code: Select all
#! /usr/bin/python
# -*- coding: iso-8859-15 -*-


import threading,time,random

RUNS=10
THREADS=10


class Test( threading.Thread ):
    id=None
   
    def __init__(self,id):
        super(Test,self).__init__()
        print "Creating thread #%d" % id
        self.id=id

    def run(self):
        print "Starting thread #%d" % self.id
        for _ in range(RUNS):
            time.sleep(random.uniform(.5,2.5))
            print 'Thread #%d still alive' % self.id
        print "Ending thread #%d" % self.id

# create the threads
threads=[Test(i) for i in range(THREADS)]
         
# make them run         
for t in threads:
    t.start()
   
# make sure they are all ended before existing:
for t in threads:
    t.join()
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: speed up pingsweeper

Postby Seguino » Sun Jul 31, 2016 4:45 pm

Thank you very mucht for your explanation. I see now that I was wrong.
Going to dive into it when time permits
Seguino
 
Posts: 3
Joined: Mon Jul 25, 2016 7:36 am

Re: speed up pingsweeper

Postby wavic » Wed Aug 03, 2016 1:58 pm

In bash & after a command means the the command will run in the background. This will let you use the terminal for something else during the long running command &.

Look at this
wavic
 
Posts: 165
Joined: Wed May 25, 2016 8:51 pm


Return to Networking

Who is online

Users browsing this forum: No registered users and 1 guest

cron