python生成xml文件(xml.dom.minidom)

有个脚本一直调用一个xml文件,想让他更加灵活。就有了今天的这个需求。

程序使用配置文件生成任务,但是配置文件手动编写太过繁琐,配合调用脚本完成自动化

# -*-coding:utf-8 -*-
# BY WANGCC

import xml.dom.minidom as minidom

class Nsf_currency:
    task_type='domain'
    os_match = 'yes'
    messtypes='5;6;7;8;9;10;11;12;13;14;15;16;17;18;20;21;22;23;24;25;26;27;28'
    isguesspwd='no'
    port_strategy='user'
    port_speed='3'
    port_tcp='T'
    port_udp='yes'
    live='yes'
    live_icmp='yes'
    live_udp='no'
    live_tcp='yes'
    live_tcp_ports='21,22,23,25,80,443,445,139,3389,6000'
    scan_level='3'
    timeout_plugins='40'
    timeout_read='5'
    enable_unsafe_plugins='no'
    scan_alert='yes'
    alert_msg=''
    scan_huawei='no'
    check_addtional='no'
    scan_oracle='yes'
    ifdebug='no'
    encoding='UTF-8'

class Nsf_conf(Nsf_currency):
    def __init__(self,targets, port_strategy_userports):
        self.targets=targets
        self.port_strategy_userports=port_strategy_userports

dom = minidom.getDOMImplementation().createDocument(None, 'config', None)
root = dom.documentElement
employee = dom.createElement('server')
root.appendChild(employee)

targets = dom.createElement('targets')
targets.appendChild(dom.createTextNode('\n\t'))
root.appendChild(targets)
report = dom.createElement('report')
report.appendChild(dom.createTextNode('\n\t'))
root.appendChild(report)
pwdguess = dom.createElement('pwdguess')
pwdguess.appendChild(dom.createTextNode('\n\t'))
root.appendChild(pwdguess)
#----------------
taskname = dom.createElement('taskname')
taskname.appendChild(dom.createCDATASection(''))
root.appendChild(taskname)
#--------------------plugin_template_id
plugin_template_id = dom.createElement('plugin_template_id')
plugin_template_id.appendChild(dom.createTextNode('0'))
root.appendChild(plugin_template_id)
#---------------------
scanpri = dom.createElement('scanpri')
scanpri.appendChild(dom.createTextNode('2'))
root.appendChild(scanpri)

def dom_createE(name,value):
    element = dom.createElement('key')
    element.setAttribute('name', name)
    element.setAttribute('value', value)
    employee.appendChild(element)
    return

def writeInfoToXml(object):
    #内容参数
    dom_createE(name='task_type',value=Nsf_currency.task_type)
    dom_createE(name='targets',value=object.targets)
    dom_createE(name='os_match',value=object.os_match)
    dom_createE(name='messtypes',value=object.messtypes)
    dom_createE(name='isguesspwd', value=object.isguesspwd)
    dom_createE(name='port_strategy', value=object.port_strategy)
    dom_createE(name='port_strategy_userports', value=object.port_strategy_userports)
    dom_createE(name='port_speed', value=object.port_speed)
    dom_createE(name='port_tcp', value=object.port_tcp)
    dom_createE(name='port_udp', value=object.port_udp)
    dom_createE(name='live', value=object.live)
    dom_createE(name='live_icmp', value=object.live_icmp)
    dom_createE(name='live_udp', value=object.live_udp)
    dom_createE(name='live_tcp', value=object.live_tcp)
    dom_createE(name='live_tcp_ports', value=object.live_tcp_ports)
    dom_createE(name='scan_level', value=object.scan_level)
    dom_createE(name='timeout_plugins', value=object.timeout_plugins)
    dom_createE(name='timeout_read', value=object.timeout_read)
    dom_createE(name='enable_unsafe_plugins', value=object.enable_unsafe_plugins)
    dom_createE(name='scan_alert', value=object.scan_alert)
    dom_createE(name='scan_huawei', value=object.scan_huawei)
    dom_createE(name='check_addtional', value=object.check_addtional)
    dom_createE(name='scan_oracle', value=object.scan_oracle)
    dom_createE(name='ifdebug', value=object.ifdebug)
    dom_createE(name='encoding', value=object.encoding)

    with open('test.xml', 'w', encoding='utf-8') as f:
        dom.writexml(f, addindent='\t', newl='\n', encoding='utf-8')


if __name__ == '__main__':
    object=Nsf_conf('10.10.10.10,20.20.20.20','1-100,443,445')
    writeInfoToXml(object)
    print(object.targets)

如上图所示,有点乱,还有很多可以优化的地方,先整出来,再优化吧
以下是今天借鉴的文章地址。
python 通过xml获取测试节点和属性
python中minidom对xml的常用处理
官方文档

查看最多的文档

使用minidom来处理XML的示例(Python 学习)(转载)

poc漏洞测试工具

你可能感兴趣的:(python生成xml文件(xml.dom.minidom))