The script needs Ruby (1.8), the Men & Mice CLI (mmcmd) and NMAP 4.7x installed on the same machine.
The script does autodiscovery of devices in IP Ranges defined in the Men & Mice System. It does not (in the current form) discover IP Networks (although that would also be possible, but a different script).
The script has been designed to be run from the Men & Mice Internal Scheduler on the Men & Mice Central System. However the script can be run from any other scheduler, but then the Administrator password must be hardcoded into the schedulers call or into the script. From the Men & Mice Scheduler, you can use the one-time password variables $u and $p (Scriptrunner, see the Men & Mice User Guide). The Scriptrunner account must be able to read/write all the IP Ranges that should have autodiscovery of devices. IP Ranges that should not have autodiscovery should not be visible to the scriptrunner account.
For the IP Devices, you must create the following Custom Fields in the Men & Mice System:
os
MAC-Address
NIC-Vendor
NetBIOS
Uptime
Network Distance
Ports
Applications
If you rename the fields, you need to adjust the calls to mmcmd in lines 81-85.
The script has been tested on Unix/Linux and Windows.
Code:
# discover.rb
# (c) 2009 by Men & Mice
# 2009.04.30 Carsten Strotmann
# discovers information in networks using NMAP
# requirement: Men & Mice CLI Version 6.x, nmap 4.5+
require 'rexml/document'
#isWindows = ((RUBY_PLATFORM =~ /(win|w)32$/) > 0)
isWindows = false
unless ARGV.length > 2
puts "Usage: discover.rb <central> <username> <password>"
exit
end
central = ARGV[0].chomp
username = ARGV[1].chomp
password = ARGV[2].chomp
IO.popen("mmcmd -s #{central} -u #{username} -p #{password} " + '"ipranges; quit;"') do | channel |
channel.each do | line |
net = line.split("/")
baseip = net[0]
mask = net[1].to_i
if (mask > 20 && ! baseip.include?(":")) then
IO.popen("nmap -O -oX - -A -T4 #{baseip}/#{mask}") {|x|
xmls = x.readlines
xml = REXML::Document.new(xmls.join)
xml.elements.each("//host") { |host|
ipv4 = REXML::XPath.first(host, "address[@addrtype='ipv4']")
ipv4addr = ipv4 ? ipv4.attribute("addr").to_s : ""
os = REXML::XPath.first(host, "os/osclass")
os = os ? os.attribute("osfamily") : nil
if (os) then
osv = REXML::XPath.first(host, "os/osclass").attribute("osgen")
osversion = osv ? osv.to_s : ""
osname = os ? os.to_s : ""
end
nb = REXML::XPath.first(host, "hostscript/script[@id='NBSTAT']")
netbios = nb ? nb.attribute("output").to_s : ""
up = REXML::XPath.first(host, "uptime")
up = up ? up.attribute("seconds") : nil
uptime = up ? up.to_s : ""
mac = REXML::XPath.first(host, "address[@addrtype='mac']")
macaddr = mac ? mac.attribute("addr").to_s : ""
macvendor = mac ? mac.attribute("vendor").to_s : ""
dist = REXML::XPath.first(host, "distance")
distance = dist ? dist.attribute("value").to_s : ""
portlist = ""
applicationlist = ""
ports = REXML::XPath.first(host, "ports")
ports.each { |port|
service = REXML::XPath.first(port, "service")
servicename = service ? service.attribute("name").to_s : ""
if (servicename) then
if (servicename.length > 0) then
if portlist.length > 0 then
portlist = portlist + ", "
end
portlist = portlist + servicename
end
end
appname = service ? service.attribute("product").to_s : ""
appversion = service ? service.attribute("version").to_s : ""
if (appname && appname.length > 0) then
if applicationlist.length > 0 then
applicationlist = applicationlist + ", "
end
applicationlist = applicationlist + appname + ((appversion.length > 0) ? "[" + appversion + "]" : "")
end
}
if (ipv4) then
if (isWindows) then
`mmcmd -q -s #{central} -u #{username} -p #{password} addDevice #{ipv4addr} os=\\\"#{osname} #{osversion}\\\" MAC-Address=\\\"#{macaddr}\\\" NIC-Vendor=\\\"#{macvendor}\\\" NetBIOS=\\\"#{netbios}\\\" Uptime=\\\"#{uptime}\\\" \\\"Network Distance\\\"=#{distance} Ports=\\\"#{portlist}\\\" Applications=\\\"#{applicationlist}\\\"; quit;`
`mmcmd -q -s #{central} -u #{username} -p #{password} modifyDevice #{ipv4addr} os=\\\"#{osname} #{osversion}\\\" MAC-Address=\\\"#{macaddr}\\\" NIC-Vendor=\\\"#{macvendor}\\\" NetBIOS=\\\"#{netbios}\\\" Uptime=\\\"#{uptime}\\\" \\\"Network Distance\\\"=#{distance} Ports=\\\"#{portlist}\\\" Applications=\\\"#{applicationlist}\\\"; quit;`
else
`mmcmd -q -s #{central} -u #{username} -p #{password} 'addDevice #{ipv4addr} os=\"#{osname} #{osversion}\" MAC-Address=\"#{macaddr}\" NIC-Vendor=\"#{macvendor}\" NetBIOS=\"#{netbios}\" Uptime=\"#{uptime}\" \"Network Distance\"=#{distance} Ports=\"#{portlist}\" Applications=\"#{applicationlist}\"; quit;'`
`mmcmd -q -s #{central} -u #{username} -p #{password} 'modifyDevice #{ipv4addr} os=\"#{osname} #{osversion}\" MAC-Address=\"#{macaddr}\" NIC-Vendor=\"#{macvendor}\" NetBIOS=\"#{netbios}\" Uptime=\"#{uptime}\" \"Network Distance\"=#{distance} Ports=\"#{portlist}\" Applications=\"#{applicationlist}\"; quit;'`
end
end
}
}
end
end
end