#!/bin/bash
# Script that autogenerates /etc/ethers for rarp booting and
# makes links to a boot file that the clients can get over tftp.

# Author Wilhelm Person wilper(a)ludd.ltu.se

# NOTICE!
# This script will overwrite any already existing /etc/ethers file,
# and relink any conflicting links.

# The script relies on entries in the /etc/hosts file to determine 
# IP numbers for net-booting machines.

# A maximum of 9 simultaneously netbooting machines are supported, 
# Adding support for more machines is only a matter of adding more 
# machines in the /etc/hosts file and increasing the tail count in 
# the "for"-statement below. 


# This file will be served to anyone who wants to boot.
BOOTFILE=/tftpboot/debian_3.0.img


J=0
rm /etc/ethers
echo "# Autogenerated /etc/ethers file." > /etc/ethers

for I in `grep "request from" /var/log/daemon.log | cut -d ' ' -f 10|uniq|tail -n 9`;
do
	let "J = $J + 1"
	BOXNAME="box-$J"
	BOXIP=`grep $BOXNAME /etc/hosts|cut -d " " -f 1`
	BOXIP2=`echo $BOXIP | sed s/"\."/\ /g`
	BOXFILE=/tftpboot/`printf "%02X%02X%02X%02X\n"  $BOXIP2`.SUN4M
	echo "Adding $I as $BOXNAME with ip $BOXIP and bootfile $BOXFILE"
	echo "$I $BOXNAME" >> /etc/ethers 
	ln -sf $BOOTFILE $BOXFILE
done


