== host2base ==

'''Problem:''' Es ist mühsam, die Hosts mit ihren IP's in die Base.txt einzutragen, zumal die oft in einer /etc/hosts Datei schon verfügbar sind. Auch ist es mühsam, das geänderte Format von 2.0.x nach 2.1.x zu konvertieren. 

'''Lösung:''' Folgendes Skript liest eine /etc/hosts Datei ein und wandelt die Einträge in Base.txt konformes Format um. Es werden beide Formate ausgegeben (2.0.x und 2.1.x). Die Ausgabe kann in eine Datei umgeleitet und diese dann mit einem Editor in die Base.txt eingefügt werden.

'''Beispiel einer Hosts Datei'''
  {{{
# Test hosts

192.168.10.1    ruby.intern.net         ruby
192.168.10.100  router1.intern.net      router1
192.168.10.10   server1.intern.net      server1
192.168.10.50   client1.intern.net      client1
192.168.10.51   client2.intern.net      client2
192.168.10.52   client3.intern.net      client3
}}}

'''Beispielausgabe des Skripts'''
  {{{
# ruby host2base /etc/hosts

---- Base Format <= Version 2.0.x
HOSTS_N='6'
HOST_1='192.168.10.1 ruby'
HOST_2='192.168.10.100 router1'
HOST_3='192.168.10.10 server1'
HOST_4='192.168.10.50 client1'
HOST_5='192.168.10.51 client2'
HOST_6='192.168.10.52 client3'
---- Base Format >= Version 2.1.x
HOSTS_N='6'
HOST_1_IP='192.168.10.1'
HOST_1_NAME='ruby'
HOST_2_IP='192.168.10.100'
HOST_2_NAME='router1'
HOST_3_IP='192.168.10.10'
HOST_3_NAME='server1'
HOST_4_IP='192.168.10.50'
HOST_4_NAME='client1'
HOST_5_IP='192.168.10.51'
HOST_5_NAME='client2'
HOST_6_IP='192.168.10.52'
HOST_6_NAME='client3'
}}}

'''Skript:''' (download: http://www.reintechnisch.de/pub/fli4l/)
  {{{
#! /usr/bin/ruby

# host2base - convert /etc/hosts to fli4l base Format
# date: 11.02.2004, author: Winfried Mueller <wm AT reintechnisch DOT de>

def use
  $stderr.puts "use: host2base hostfile"
end

if $*.length != 1 
  use
  exit 1
end

$hostfile = $*[0]

if !File.file?( $hostfile )
  $stderr.puts "File1 [#{$hostfile}]not found."
  use
  exit 1
end

class Host
  def initialize( host, ip )
    @host = host
    @ip   = ip
  end
  attr_reader :host, :ip
end

# parse hosts file
$hosts = Array.new
File.foreach( $hostfile ) do |line|
  next if line =~ /^\s*$/
  next if line =~ /^\s*#/
  rxp_ip='\d+\.\d+\.\d+\.\d+'
  if line =~ /^\s*(#{rxp_ip})\s+(.+?)\s+(.+?)\s*$/
    $hosts << Host.new( $3, $1 ) 
  end
end

# output format 1
puts "---- Base Format <= Version 2.0.x"
puts "HOSTS_N='#{$hosts.length}'"
num = 1
$hosts.each do |host|
  puts "HOST_#{num.to_s}='#{host.ip} #{host.host}'"
  num += 1
end

# output format 2
puts "---- Base Format >= Version 2.1.x"
puts "HOSTS_N='#{$hosts.length}'"
num = 1
$hosts.each do |host|
  puts "HOST_#{num.to_s}_IP='#{host.ip}'"
  puts "HOST_#{num.to_s}_NAME='#{host.host}'"
  num += 1
end
}}}

Zur Ausführung muß Ruby installiert sein. Für Windows bekommt man das Paket unter 
http://rubyinstaller.sourceforge.net/ Unter Linux sollte es bei jeder Distribution mit dabei sein. (debian: apt-get install ruby )

Das Skript als Datei "host2base" speichern und es entweder mittels chmod 700 ausführbar machen (nur Linux) oder es mit "ruby host2base hostfile"  auf der Kommandozeile/Shell starten. 

Mehr zu [[Ruby]]

Fragen und Kommentare an mich: WinfriedMueller