Tuesday, August 2, 2016

script to scan alive hosts on network with multithreading

#!/usr/bin/python

import thread
import time
import re
import subprocess
import ipaddress
import threading
import sys
# Define a function for the thread
n = 0
exitFlag = 0

class myThread (threading.Thread):
    def __init__(self, threadID, name, counter,baseAddr,nHost,device):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.baseAddr = baseAddr
        self.nHost = nHost
        self.device = device
    def run(self):
        print "Starting " + self.name
        handler_mgmt(self.name, self.counter, self.baseAddr, self.nHost, self.device)
        print "Exiting " + self.name

def testconn(address, device):
    command = ["arping", address, "-c", "1", "-I", device]
    testresult = subprocess.Popen(command ,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if re.search("Received 1", testresult.stdout.read()):
        return True
    return False
  

def handler_mgmt( threadName, delay, baseAddr, nHost, device):
    count = 0
    global n
    ipBase = ipaddress.IPv4Address(baseAddr)
    counter = nHost
    while counter:
        if exitFlag:
            threadName.exit()
        for i in range(nHost):
            ipToCheck = str(ipBase + i + 1)
            result = testconn(ipToCheck, device)
            if count == 1000:
                time.sleep(delay)
                count = 0
            count += 1
            print ipToCheck + " : " + str(result)
            counter -= 1
      


if len(sys.argv) != 3:
    print "usage, "
    print "$ sudo python ipscan.py interface subnet"
    print "example"
    print "$ sudo python ipscan.py eth0 192.168.0.0/24"
    sys.exit()
interface = sys.argv[1]
networks = sys.argv[2].decode('utf-8')



try:
    subnet = ipaddress.ip_network(networks)
    subnetLen = len(list(subnet.hosts()))
    nForThread = subnetLen / 4
    rForThread = subnetLen % 4
except:
    print "please provide the correct subnet address"
finally:
    pass

baseAddr = ipaddress.IPv4Address(re.sub(r"/.*","",networks))


thread1 = myThread(1, "Thread-1" , 1,str(baseAddr).decode("utf-8"), nForThread, interface)
thread2 = myThread(2, "Thread-2" , 1,str(baseAddr+ nForThread).decode("utf-8"), nForThread, interface)
thread3 = myThread(3, "Thread-3" , 1,str(baseAddr + nForThread * 2).decode("utf-8"), nForThread, interface)
thread4 = myThread(4, "Thread-4" , 1,str(baseAddr + nForThread * 3).decode("utf-8"), nForThread + rForThread, interface)

# Start new Threads
thread1.start()
thread2.start()
thread3.start()
thread4.start()




#################################

1- save it as ipscan.py
2- run it -- python ipscan.py exit-interface subnet -- example below

$ sudo python ipscan.py eth0 192.168.0.0/29
Starting Thread-1
Starting Thread-2
Starting Thread-3
Starting Thread-4

192.168.0.1 : True
Exiting Thread-1
192.168.0.2 : False
 Exiting Thread-2
192.168.0.3 : False
Exiting Thread-3
192.168.0.4 : False
192.168.0.5 : False
192.168.0.6 : False
Exiting Thread-4

No comments:

Post a Comment