?????Python?????????
???????????? ???????[ 2014/11/17 16:17:48 ] ??????????????? ???? ???
????????д?????СС???????????????£?
????1????????API????????е?????IP?Σ?
????2????Nmap ??????????е?IP?Σ?-oX ????XML????豨?棻
????3????xml.etree.ElementTree??鷽?????XML???????ip???????????????????д??Mysql?????
??????????????????????????????????????С????????μ??????~~~????????????????????????????????Ъ????????
???????????
#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?????·??????????????е?ip?б????IPy???????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('
'):
#?ж????ip?б???????????ip/????????????????ip/24????
if ip:
ip=ip.replace('_'??'/')
ip=(IP(ip))
iplist.append(str(ip))
ipscan(iplist??nmapathx)
#????ip????????nmap·??
def ipscan(iplist??nmapath):
#???????????ip????е?ip??????????
newiplist=[]
scaniplist=[]
for ip in iplist:
if ip not in newiplist:
newiplist.append(ip)
#????????ip?Σ???????裬????xml???????
for ip in newiplist:
filename=nowtime+ip.split('/')[0]+'.xml'
filepath=r"c:portscanscanres\"
nmapcmd=nmapath+' -PT '+ip.strip('
')+' -oX '+filepath+filename
os.system(nmapcmd)
scaniplist.append(ip)
writeinmysql(scaniplist)
#??????????????д?????? ???????????????????xml.etree.ElementTree??顣
def writeinmysql(scaniplist):
filepath=r"c:portscanscanres"
for ip in scaniplist:
xmlfile=filepath+'\'+ip+'.xml'
#?????? ?????μ?????????????????????
root=ET.parse(xmlfile).getroot()
allhost=root.findall('host')
conn=MySQLdb.connect(host='10.1.11.11'??user='nxadmin'??passwd='nxadmin.com'??port=3306??db='scandatabase'??charset='utf8')
cur= conn.cursor()
for host in allhost:
address = host.find('address')
#?????ж????????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.execute(sql??params)
conn.commit()
cur.close()
conn.close()
if __name__=="__main__":
#?????????????????IP apiurl??nmap??????·??
config=ConfigParser.ConfigParser()
config.readfp(open(configpath??'rb'))
nmapathx=config.get('nmap'??'nmapath')
ipinf=config.get('ip'??'ipinf')
getiplist(ipinf)
???????????c:portscanconfig.ini???????api?????url??nmap???·????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11