3 # Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation
17 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 from xml.dom.minidom import parse
25 print "Syntax: %s <xml-file> [<xml-file> ...]" % (sys.argv[0])
28 tcp_service_commands = {
36 udp_service_commands = {
43 def process_host(host_element):
48 for status_element in host_element.getElementsByTagName("status"):
49 status = status_element.getAttribute("state")
54 for address_element in host_element.getElementsByTagName("address"):
55 if not address_element.getAttribute("addrtype") in [ "ipv4", "ipv6" ]:
58 address = address_element.getAttribute("addr")
63 for hostname_element in host_element.getElementsByTagName("hostname"):
64 name = hostname_element.getAttribute("name")
67 services = hosts[name]["services"]
71 for port_element in host_element.getElementsByTagName("port"):
74 for state_element in port_element.getElementsByTagName("state"):
75 state = state_element.getAttribute("state")
80 port = int(port_element.getAttribute("portid"))
81 protocol = port_element.getAttribute("protocol")
84 serv = socket.getservbyport(port, protocol)
90 command = tcp_service_commands[serv]
91 elif protocol == "udp":
92 command = udp_service_commands[serv]
94 raise "Unknown protocol."
101 services[serv] = { "command": command, "port": port }
103 hosts[name] = { "name": name, "address": address, "services": services }
105 def print_host(host):
106 print "object Host \"%s\" {" % (host["name"])
107 print "\timport \"discovered-host\","
109 print "\taddress = \"%s\"," % (host["address"])
113 for serv, service in host["services"].iteritems():
114 print "object Service \"%s\" {" % (serv)
115 print "\timport \"discovered-service\","
117 print "\thost_name = \"%s\"" % (host["name"])
118 print "\tcheck_command = \"%s\"," % (service["command"])
120 print "\tvars.port = %s" % (service["port"])
124 for arg in sys.argv[1:]:
125 # Expects XML output from 'nmap -oX'
128 for host in dom.getElementsByTagName("host"):
131 for host in hosts.values():