Python Port Scanning System implementation method

Source: Internet
Author: User
Tags ipscan
This paper describes the implementation method of Python port scanning system. Share to everyone for your reference. The implementation method is as follows:

The main functions of the program are as follows:

1. Get all the extranet IP segments from your own API interface;
2. Use NMAP traversal to scan all IP segments,-ox generate the XML scan report;
3. Read the XML file using the Xml.etree.ElementTree module method, and write the IP, open port, corresponding service, etc. to the MySQL database.
The function is very simple, did not meet the boss tall on the demand, so this small project is so heroic hanging off! ~ ~ ~ completely has not considered the program abnormal termination, scan server abnormal the situation of the dishes.
Paste the code:
Copy the 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接口主路径, traverse get all IP list, format 127.0.0.1/24 with IPY module
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列表不为空, converted to ip/gateway format, and then formatted into IP/24 format
If IP:
Ip=ip.replace (' _ ', '/')
ip= (IP)
Iplist.append (str (IP))
Ipscan (IPLIST,NMAPATHX)

#传递ip地址文件和nmap路径
def ipscan (Iplist,nmapath):
#古老的去重, the IP address in the IP file is de-weighed
Newiplist=[]
Scaniplist=[]
For IP in IPList:
If IP not in newiplist:
Newiplist.append (IP)
#遍历所有ip段, batch scan, generate XML format report
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 '
#缩进哈 send the article when the temporary change, too lazy to shrink into the
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 it is re-warehousing
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 in the configuration file C:portscanconfig.ini is the API interface.

Interested friends can further refine the functionality of this instance. Hopefully this article will help you with Python programming.

  • 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.