The implementation method of Python port scanning system _python

Source: Internet
Author: User
Tags ipscan

This article illustrates the implementation of the Python port scanning system. Share to everyone for your reference. The implementation methods are as follows:

The main functions of the program are as follows:

1. From its own API interface to obtain all of the extranet IP segment;
2. Scan all IP segments with NMAP traversal,-ox generate XML scan reports;
3. Use the Xml.etree.ElementTree module method to read XML files, IP, open ports, correspondence services, etc. to the MySQL database.
The function is very simple, did not meet the eldest brother tall demand, so this small project is so heroic hang off! ~ ~ ~ all have not considered the program abnormal termination, scan the server abnormal rest of the food.
Put the code:

Copy Code code as follows:
#coding: Utf-8
Import sys,os,time,subprocess
Import MySQLdb
Import Re,urllib2
Import Configparser
From ipy import IP
Import Xml.etree.ElementTree as ET

Nowtime = Time.strftime ('%y-%m-%d ', Time.localtime (Time.time ()))
Configpath=r ' C:portscanconfig.ini '
#传入api接口主路径, traversal gets all the IP listings, formatted as 127.0.0.1/24 with IPY modules
def getiplist (Ipinf):
serverarea=[' tj101 ', ' tj103 ', ' dh ', ' DX '
Iplist=[]
For area in Serverarea:
Ipapi=urllib2.urlopen (Ipinf+area). Read ()
For IP in Ipapi.split (' n '):
#判断如果ip列表不为空, convert to ip/gateway format, then format into IP/24 format
If IP:
Ip=ip.replace (' _ ', '/')
ip= (IP (IP))
Iplist.append (str (IP))
Ipscan (IPLIST,NMAPATHX)

#传递ip地址文件和nmap路径
def ipscan (Iplist,nmapath):
#古老的去重, the IP address in the IP file to the heavy
Newiplist=[]
Scaniplist=[]
For IP in IPList:
If IP not in newiplist:
Newiplist.append (IP)
#遍历所有ip段, batch scanning, generating XML format reports
For IP in newiplist:
Filename=nowtime+ip.split ('/') [0]+ '. xml '
Filepath=r "C:portscanscanres\"
nmapcmd=nmapath+ '-pt ' +ip.strip (' rn ') + '-ox ' +filepath+filename
Os.system (Nmapcmd)
Scaniplist.append (IP)
Writeinmysql (Scaniplist)

#入库模块是某大婶发写好的给我 I simply modified the HA, mainly the Xml.etree.ElementTree module.
def writeinmysql (scaniplist):
Filepath=r "C:portscanscanres"
For IP in scaniplist:
xmlfile=filepath+ ' +ip+ '. xml '
#缩进哈 the article when the temporary change, too lazy to indent
Root=et.parse (xmlfile). Getroot ()
Allhost=root.findall (' host ')
Conn=mysqldb.connect (host= ' 10.5.10.57 ', user= ' nxadmin ', passwd= ' nxadmin.com ', port=3306,db= ' scandb ', charset= ') UTF8 ')
Cur= Conn.cursor ()
For host in Allhost:
Address = Host.find (' address ')
#首先判断端口是不是open的, if the storage is again
For port in Host.find (' ports '). FindAll (' Port '):
If Port.find (' state '), attrib[' state ']== ' open ':
ip=address.attrib[' addr ']
portval=port.attrib[' Portid ']
State=port.find (' state ')-attrib[' state '
sql = "INSERT into Portscan (ip,port,state) VALUES (%s,%s,%s)"
Params=[ip,portval,state]
Cur.execute (Sql,params)
Conn.commit ()
Cur.close ()
Conn.close ()
If __name__== "__main__":
#读取配置文件中要扫描的IP Apiurl and Nmap installation file path
Config=configparser.configparser ()
CONFIG.READFP (Open (Configpath, ' RB '))
Nmapathx=config.get (' Nmap ', ' Nmapath ')
Ipinf=config.get (' IP ', ' Ipinf ')
Getiplist (Ipinf)

The main Url,nmap installation path for the API interface is c:portscanconfig.ini in the configuration file.

Interested friends can further refine the function of the instance. I hope this article will help you with your Python programming.

Related Article

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.