Python port scanning system implementation

Source: Internet
Author: User
Tags ipscan
This article mainly introduces how to implement the python port scanning system, which can scan Internet IP addresses and write data into the MySQL database, for more information about how to implement the python port scanning system, see the following example. Share it with you for your reference. The specific implementation method is as follows:

The main functions of this program are as follows:

1. obtain all Internet IP segments from your own API;
2. scan all IP segments with Nmap traversal, and generate an XML scan report using-oX;
3. use the xml. etree. ElementTree module to read XML files, write ip addresses, open ports, and corresponding services to the Mysql database.
The function is very simple and does not meet the requirements of the boss, so this small project is so brave and fails !~~~ We have not considered the abnormal termination of the program, and the scanning of the server is abnormal.
Paste the code:

The code is 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'
# Input the main path of the api interface, traverse and obtain the list of all ip addresses, and use the IPy module in the format of 127.0.0.1/24
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 '):
# If the ip address list is not empty, convert it to the ip address/Gateway format, and then format it into the ip address/24 format.
If ip:
Ip = ip. replace ('_','/')
Ip = (IP (ip ))
Iplist. append (str (ip ))
Ipscan (iplist, nmapathx)

# Transfer the IP address file and nmap path
Def ipscan (iplist, nmapath ):
# Old deduplication: deduplication of ip addresses in the ip file
Newiplist = []
Scaniplist = []
For ip in iplist:
If ip not in newiplist:
Newiplist. append (ip)
# Traverse all ip segments, scan in batches, and generate 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)

# The warehouse receiving module was written to me by a major hacker. it was simply modified, mainly the xml. etree. ElementTree module.
Def writeinmysql (scaniplist ):
Filepath = r "c: portscanscanres"
For ip in scaniplist:
Xmlfile = filepath + '\' + ip + '. XML'
# Indent: I changed it temporarily when I posted an article. I am too lazy to indent it.
Root = ET. parse (xmlfile). getroot ()
Allhost = root. findall ('host ')
Conn = MySQLdb. connect (host = '10. 5.10.57 ', user = 'nsadmin', passwd = 'nsadmin. com ', port = 3306, db = 'scandb', charset = 'utf8 ')
Cur = conn. cursor ()
For host in allhost:
Address = host. find ('address ')
# First, check whether the port is open.
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.exe cute (SQL, params)
Conn. commit ()
Cur. close ()
Conn. close ()
If _ name __= = "_ main __":
# Read the IP address apiurl and nmap Installation file path to be scanned in the configuration file
Config = ConfigParser. ConfigParser ()
Config. readfp (open (configpath, 'RB '))
Nmapathx = config. get ('nmap', 'nmapath ')
Ipinf = config. get ('IP', 'ipinf ')
Getiplist (ipinf)


Configuration file c: portscanconfig. ini contains the main url of the api interface and the nmap Installation Path.

If you are interested, you can further improve the functions of the instance. I hope 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.