def scan_subnets(subnets): nmap_args = ["-nsP"] + [item for item in subnets] vlans = nmap(nmap_args) ip_patt = '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' p = re.compile(ip_patt) #p.findall(vlans.stdout) active_ips = p.findall(vlans.stdout) #storage_ips = [iptools.IpRangeList(subnet) for subnet in subnets] subnet_ips = {subnet: iptools.IpRangeList(subnet) for subnet in subnets} ip_usage = {} for subnet, ip_range in subnet_ips.iteritems(): ip_list = [] for ip in ip_range: #print ip try: host = reversename.from_address(ip) #in case there are multiple reverse entries for a #given IP names = [nm.to_text() for nm in resolver.query(host, 'PTR')] name = ','.join(names) except resolver.NXDOMAIN: name = '' if ip in active_ips: ip_list.append([ip, 'ACTIVE', name]) else: ip_list.append([ip, 'INACTIVE', name]) ip_usage[subnet] = ip_list return ip_usage
def run(url): result = '' for port in ports: result += '%s' % ports[port] result += nmap(url.hostname, p=port).stdout result += '\n' return '%s' % result
def getHosts(): #this uses nmap to scan the lan for online hosts, then filters the text #for just the number raw = str(nmap("-sP", "-PA21,22,25,3389", "192.168.1.1/24")) splitraw = raw.split(" ") rawhosts = splitraw[-7] hosts = rawhosts[1] #this is a string btw return hosts
def nmap_scan(target): """ Performs an nmap portscan against the target on all TCP/UDP ports. """ scan = nmap([target, '-sS', '-sU', '-oX', '-']) dom = ET.fromstring(scan.stdout) result = [] for dhost in dom.findall('host'): host = dhost.find('address').get('addr') for dport in dhost.findall('ports/port'): proto = dport.get('protocol') port = int(dport.get('portid')) state = dport.find('state').get('state') result.append({ 'host': host, 'proto': proto, 'port': port, 'state': state }) return result
#Will automagically parse your ip and scan the network with nmap from sh import nmap, ifconfig ip = ifconfig("en1") ip = str(ip.split("\n")[3].split("inet ")[1].split(" netmask")[0]) ip_range = ip.split(".")[0] + "." + ip.split(".")[1] + "." + ip.split(".")[2] + "." + "0/24" print(nmap(ip_range))
def run(url): result = nmap(url.hostname) return '%s' % result.stdout
def ip_scan(self): self.printf("start ip scan : {}".format(self.ip_scan_input.text())) p = sh.nmap('-sV', 'www.zzu.edu.cn', _bg=True) self.printf("add a new nmap task") self.printf(str(p.is_alive()))