ANTFARM/Traceroute/Traceroute 脚本
外观
< ANTFARM | Traceroute
此脚本的目的是解析 traceroute 文件并提取信息,以供 Antfarm 的图形视图使用。
#!/usr/bin/env ruby # # Copyright (2008) Sandia Corporation. # Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, # the U.S. Government retains certain rights in this software. # # Original Author: Michael Berg, Sandia National Laboratories <mjberg@sandia.gov> # Modified By: Bryan T. Richardson, Sandia National Laboratories <btricha@sandia.gov> # Further Modifications By: Melissa Myerly & Cassandra Trevino, Sandia National Laboratories # This library is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or (at # your option) any later version. # # This library is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License # along with this library; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # def parse(file) puts "Filename: " #This is here for convenience, it simply prints the filename you are parsing to the screen. puts file
使用正则表达式解析 traceroute 文件。从 traceroute 文件中解析出的项目包括主机名、时间、跳数和 IP 地址。本脚本不使用时间或跳数项目。
hostname_regexp = Regexp.new('(\S+)\.[a-z]*(\d)*')
time_regexp = Regexp.new('(<*\d+\s*ms\s*){1,3}')
hop_regexp = Regexp.new('(\d){1,3}')
ipv4_regexp = Regexp.new('((\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3})')
所有用于在数据库中存储数据的项目都设置为 nil。
hostname = nil source_addr = nil source_hostname = nil dest_addr = nil dest_hostname = nil list = File.open(file) firstline = list.gets()
对于 traceroute 文件中的每一行,都会检查数据库中的记录以确保主机名或 IP 地址不存在,以便可以将其添加到数据库中。当从 traceroute 文件中解析出每个 IP 地址和主机名时,就会发生这种情况。
source_addr = ipv4_regexp.match(firstline)
source_hostname = hostname_regexp.match(firstline)
list.each do |line|
dest_addr = ipv4_regexp.match(line)
dest_hostname = hostname_regexp.match(line)
if dest_hostname
if ipv4_regexp.match( "#{dest_hostname}" )
dest_hostname = nil
end
end
source_iface = IpInterface.find_or_initialize_by_address("#{source_addr}")
if source_iface.new_record?
source_iface.node_name = "#{source_addr}" #Assign the node name in the database to the source address
source_iface.node_device_type = 'TRACEROUTE' #Informing the database this is from TRACEROUTE data
source_iface.save false
end
dest_iface = IpInterface.find_or_initialize_by_address("#{dest_addr}")
if dest_iface.new_record?
dest_iface.node_name = "#{dest_addr}"
dest_iface.node_device_type = 'TRACEROUTE'
dest_iface.save false
end
以下内容用于将从 traceroute 文件中提取的信息输入数据库。source_iface.layer3_interface.id 指的是源地址,dest_iface.layer3_interface.id 指的是目标地址。
traffic = Traffic.first(:conditions => { :source_layer3_interface_id => source_iface.layer3_interface.id,
:target_layer3_interface_id => dest_iface.layer3_interface.id })
unless traffic
traffic = Traffic.create :source_layer3_interface => source_iface.layer3_interface,
:target_layer3_interface => dest_iface.layer3_interface,
:description => "TRACEROUTE"
end
以下 3 个 puts 语句不是必需的,它们用于方便起见,以及验证正确的主机名和关联的 IP 地址是否已放置到数据库中的正确表中。
puts "Source Hostname Addr #{source_hostname} #{source_addr} "
puts "Dest Hostname Addr #{dest_hostname} #{dest_addr}"
puts "end of line"
源地址和主机名必须成为循环的下一次迭代的目标地址和主机名(以及 Antfarm 生成的图形的意义)。
source_addr = dest_addr
source_hostname = dest_hostname
end
end
if ARGV[0] == '--help'
#print_help
else
ARGV.each do |arg|
if File.directory?(arg)
Find.find(arg) do |path|
if File.file?(path)
parse(path)
end
end
else
parse(arg)
end
end
end