#(1), the 111.206.217.0/24, 218.75.110.0/24 two network segment IP address into the PROXY.NMAP_IP table, the Type field is inserted according to the individual ordinal number. defreadipbyaddr (): Ip_list= [] forIinchRange (255): I='111.206.217.%s'%I J='218.75.110.%s'%i ip_list.append (i) Ip_list.append (j)returnip_listdefreadipbyfile (name): Frlist= Open (name,'R'). ReadLines () ip_list= [] forIpinchFrlist:ip=Ip.strip () ip_list.append (IP)returnip_listdefInsertip ():#IPList = readipbyaddr ()IPList = Readipbyfile ('Iplist.txt'); Conn= Db_conn ('192.168.88.103','Root','Cosmysql','Proxy') Cursor=conn.cursor () forJinchIplist:cmdsql="INSERT into NMAP_IP (IP) VALUES ('%s ')"%(j)Printcmdsql cursor.execute (cmdsql) cursor.close () conn.close ()#(2), using Nmap to scan the above IP open port, and distinguish between the HTTP port and other ports, and update the database;defscan_ip (): Conn= Db_conn ('192.168.88.103','Root','Cosmysql','Proxy') Cursor=conn.cursor () cursor.execute ('Select IP from nmap_ip') Result= Cursor.fetchall ()#Get all IP lists forIpinchResult:nmap="Nmap%s | grep ' Open '"%IP[0]#Constructing Namp Scan statementsNmap_result = Os.popen (nmap). ReadLines ()#Scan and get results ifNmap_result = = []:#Host is not turned on PrintIp[0],': noopen!' Else : PrintIp[0],': Uped,next updatedb' #format like: [' 80/tcp open http\n ', ' 443/tcp open https\n ']; DB Like:ip,http_port,other_port,title,typeHttp_port ="'Other_port="' forPort_resultinchNmap_result:#遍历scan results. and constructs the SQL UPDATE statement.Port_result = Port_result.strip (). Replace ('/tcp',"'). Split (' ') ifPort_result[len (Port_result)-1] = ='http': Http_port=Port_result[0]Else: Other_port= Other_port + port_result[0] +','Update_sql="Update nmap_ip set http_port= '%s ', other_port= '%s ' where ip= '%s ';"%(http_port,other_port,ip[0])PrintUpdate_sql Update_reslut=Cursor.execute (Update_sql) cursor.close ()#close the cursor.Conn.close ()#close the connection.#(3), access the above IP HTTP port, get title and update the database. defupdate_title ():Try: Conn= Db_conn ('192.168.88.103','Root','Cosmysql','Proxy') Cursor=conn.cursor () cursor.execute ("Select Ip,http_port from nmap_ip where length (http_port) > 0;") Result=Cursor.fetchall () pattern= Re.compile ('<title> (. *?) </title>') forIpinchResult:url='http://%s:%s'% (Ip[0],ip[1].replace ('/tcp',"')) PrintURLTry: Response=urllib2.urlopen (URL) HTML=response.read () Items=Re.findall (pattern,html) Update_sql="' Update nmap_ip set title= '%s ' where id= '%s ';"%(ITEMS[0][0],IP)Print 'Update title:', Update_sql cursor.execute (update_sql)exceptException, E:PrintePass exceptException, E:PrintePassdefmain4 ():#Insertip () #scan_ip ()Update_title ()if __name__=='__main__':MAIN4 ()
where Iplist.txt resembles the following:
101.1.16.123101.227.252.130101.4.136.34101.4.136.66101.4.60.43101.4.60.46101.4.60.47101.71.27.120103.246.244.161103.248.2 54.172103.248.254.173103.248.254.174103.248.254.175103.27.24.113103.27.24.114103.27.24.114106.37.177.251110.173.0.58110.1 73.0.58110.173.54.154110.75.120.170111.12.128.167
Python example-nmap scan IP and update