处理nmap生成xml

作者: admin 分类: 未分类 发布时间: 2018-01-02 21:33
#!/usr/bin/env python
# coding=utf-8
 
import sys, time
import xmltodict
 
def main():
    fp_content = ''
    shuchu = open('nmap.txt', 'w+')
    try:
        with open(sys.argv[1]) as fp:
            fp_content = fp.read().replace('\n', '')
    except IOError:
        print 'File IO Error'
        sys.exit(-1)
 
    nmap_xml = xmltodict.parse(fp_content)
    nmaprun = nmap_xml['nmaprun']
    scanhost = nmaprun['host']
    for i in scanhost:
        address = i['address']['@addr']
        port1 = dict(i)
        try:
            if int(port1['ports']['port']['@portid']) > 0:
                port2 = port1['ports']['port']['@portid']
                if port2 == '80':
                    print 'http://'+address+'/'
                elif port2 == '443':
                    print 'https://'+address+'/'
                else:
                    print 'http://'+address+':'+port2+'/'
        except:
            port2 = i['ports']['port']
            for z in port2:
                x = z['@portid']
                if x == '80':
                    print 'http://'+address+'/'
                elif x == '443':
                    print 'https://'+address+'/'
                else:
                    print 'http://'+address+':'+x+'/'
if __name__ == '__main__':
    time_start = time.time()
    try:
        main()
    except KeyboardInterrupt:
        print 'Killed by user'
        sys.exit(0)
    print "Spend {0} seconds.\n".format(time.time() - time_start)
标签云