Python calls nmap to scan the network segment host information to generate xml and pythonnmap

Source: Internet
Author: User

Python calls nmap to scan the network segment host information to generate xml and pythonnmap

1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 # Date: July 4 # Author: blue Line 5 # blog http://www.cnblogs.com/duanv/ 6 7 from IPy import IP 8 import threading 9 import nmap 10 import time 11 import sys 12 import subprocess 13 from xml. dom import minidom 14 15 def usage (): 16 print The script requires root privileges! '17 print' example: python scan. py 192.168.0.1/24 '18 19 # generate the template function of the xml file 20 def addResult (newresult): 21 global doc 22 global scan_result 23 24 ip = doc. createElement ("ip") 25 ip. setAttribute ("address", newresult ["address"]) 26 27 osclass = doc. createElement ("osclass") 28 osclass. appendChild (doc. createTextNode (newresult ["osclass"]) 29 ip. appendChild (osclass) 30 31 port = doc. createElement ("port") 32 33 tcp = doc. createElement ("tcp") 34 tcp. appendChild (doc. createTextNode (newresult ["tcp"]) 35 port. appendChild (tcp) 36 37 udp = doc. createElement ("udp") 38 udp. appendChild (doc. createTextNode (newresult ["udp"]) 39 port. appendChild (udp) 40 41 ip. appendChild (port) 42 scan_result.appendChild (ip) 43 44 # scan function, call nmap library 45 def ip_scan (ip ): 46 global nm 47 # Here we call the system ping command to determine if the host is alive 48 p = subprocess. popen ("ping-c 1-t 1" + ip, stdin = subprocess. PIPE, stdout = subprocess. PIPE, stderr = subprocess. PIPE, shell = True) 49 out = p. stdout. read () 50 # if there is no 100% packet loss, the host will survive. The determination of packet loss is to capture the system echo content. The test uses the 'mac OS x' system, the content to be crawled is 100. 0% packet loss '51 if' 100. 0% packet loss 'not in out: 52 try: 53 # Call nmap to scan the host operating system, and simultaneously perform SYN scan and UDP scan to detect open ports 54 nm. scan (ip, arguments = '-O-sS-sU-F') 55 sr = {'address': ip, 'ossclass ': str (nm [ip] ['ossclass ']) [1:-1], 'tcp': str (nm [ip]. all_tcp () [1:-1], 'udp': str (nm [ip]. all_udp () [1:-1]} 56 addResult (sr) 57 handle T: 58 pass 59 60 # loop, traverse unscanned IP 61 def loop (): 62 global mutex 63 global ipx 64 65 while 1: 66 # thread lock. Scan an IP address to remove the IP address from the IPX list 67 mutex. acquire () 68 # if there is no IP in the list, exit the loop and end the thread 69 if len (ipx) <= 0: 70 mutex. release () 71 break 72 ip = ipx [0] 73 ipx. remove (ipx [0]) 74 mutex. release () 75 # Call the scan function 76 ip_scan (str (ip) 77 78 # create a thread function. By default, 40 79 def creat_threads () are created (): 80 threads = [] 81 for I in range (40): 82 threads. append (threading. thread (target = loop,) 83 for t in threads: 84 t. start () 85 for t in threads: 86 t. join () 87 88 89 def start (): 90 # mutex: thread lock 91 global mutex 92 # ipx: store the list of IP address segments to be scanned 93 global ipx 94 # nm: nmap module scan object 95 global nm 96 # doc: xml Document Object 97 global doc 98 # scan_result: root element 99 global scan_result100 101 if '-H' = sys. argv [1]: 102 usage () 103 exit () 104 else: 105 # Get the IP segment to be scanned entered by the command line 106 ip = sys. argv [1] 107 # initialize some objects in the xml document 108 doc = minidom. document () 109 doc. appendChild (doc. createComment ("scan_result xml. ") 110 scan_result = doc. createElement ("scan_result") 111 doc. appendChild (scan_result) 112 113 # initialization parameter 114 ipx = [] 115 nm = nmap. portshares () 116 mutex = threading. lock () 117 118 # Call the IP function of the IPy module to save each IP address segment to the list 119 ipp = IP (ip, make_net = True) 120 for x in ipp: 121 ipx. append (x) 122 # Remove IP123 ipx = ipx [1:-1] 124 125 print ("please wait... ") 126 # calculation time 127 time_start = time. time () 128 # create thread 129 creat_threads () 130 131 time_end = time. time () 132 t = time_end-time_start133 print '* 48134 print' \ nTime: '+ str (t) +' s '135 print 'Scan results have been saved to scan_result.xml. \ n' 136 print '* 48137 138 # xml file operation 139 f = file ("scan_result.xml", "w") 140 f. write (doc. toprettyxml (indent = "\ t", newl = "\ n", encoding = "UTF-8") 141 f. close () 142 143 if _ name __= = '_ main _': 144 start ()

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.