#!/bin/bash # # Script to register HOSTs in DHCP, using macs_id generated # # in LWAT # # # # list DHCP -> compare with LwatMACfile -> Add Macs2DHCP -> # # -> Regenerate LwatMacsfile # # # # Author: Jose Ricardo Pla Pons # # 09 Sep 2008 # # # # Launch with root/sudo/God privilege set -e #FILES MACSIDFILE="/var/log/n4d/macs_id" TEACHERFILE="/var/log/n4d/teachermachine" TEACHERFILETMP=$(mktemp /tmp/teacher_ordered.XXX) MACSIDFILEOLD=$(mktemp /tmp/macs_id_old.XXX) TMPMACLIST=$(mktemp /tmp/lwat2dhcp.XXX) TMPMACIDFILEOLD=$(mktemp /tmp/macs_id_old.XXX) INIMACLIST=$(mktemp /tmp/macsdhcp.XXX) SORTMACS=$(mktemp /tmp/sortmacs.XXX) MAC2DELETE=$(mktemp /tmp/mac2delete.XXX) NEWMACS=$(mktemp /tmp/newmacs.XXX) #TMPMACLIST="/tmp/lwat2dhcp" #INIMACLIST="/tmp/macsdhcp" #SORTMACS="/tmp/sortmacs" #MAC2DELETE="/tmp/mac2delete" #NEWMACS="/tmp/newmacs" #SCRiS ADD2DHCP='llxcfg-dhcpd addregister ' LISTDHCP='llxcfg-dhcpd listregister ' DEL2DHCP='llxcfg-dhcpd delregister ' #Programa initial_check(){ if [ $(id -u) -ne 0 ] ; then echo "Error: Only ROOT or USER with SUDO are allowed to run this script" exit fi if [ ! -e $MACSIDFILE ] ; then echo "Error: file $MACSIDFILE not found" exit fi cat $MACSIDFILE | sort > $SORTMACS cat $SORTMACS > $MACSIDFILE } checkmacs(){ echo "Checking current Macs in DHCP" $LISTDHCP | sort -n | sed -e "s/\t/;/g" | sort > $INIMACLIST cat $INIMACLIST > $MACSIDFILEOLD echo "CURRENT MACS IN DHCP : " echo "---> "`cat $INIMACLIST` echo " " echo "CURRENT MACS IN LWATLOGS : " echo "--->" `cat $MACSIDFILE` echo " " sed -i -e "/^00;/d" $MACSIDFILEOLD echo "DELETING DUPLICATE MACS : " for line in `cat $MACSIDFILE`; do sed -i -e "/^$line/d" $MACSIDFILEOLD done for line in `cat $MACSIDFILEOLD`; do echo $line | cut -d ";" -f1 >> $MAC2DELETE done for line in `cat $MAC2DELETE`; do echo "Deleting... HOST: " $line $DEL2DHCP $line done echo " " } addmac2dhcp(){ echo "ADDING MACS FROM LWATLOG : " echo " " sed -i -e "/^00;/d" $MACSIDFILE cat $MACSIDFILE > $TMPMACLIST for line in `cat $INIMACLIST`; do sed -i -e "/^$line/d" $TMPMACLIST done for line in `cat $TMPMACLIST`; do idmac=$(echo $line | sed -e "s/;/ /g") echo "Adding ..." $idmac $ADD2DHCP $idmac done $LISTDHCP | sort -n | sed -e "s/\t/;/g" > $TMPMACLIST echo " " } addmacs2lwatfile(){ echo " " echo "REGENERATING " $MACSIDFILE #echo "with ..." #diff $TMPMACLIST $MACSIDFILE | sed 1d #diff $TMPMACLIST $MACSIDFILE | sed 1d | cut -d " " -f2 > $NEWMACS #BUG WITH THIS LINE, (PARANOIC OPTION) cat $TMPMACLIST > $NEWMACS cat $NEWMACS > $MACSIDFILE #diff $TMPMACLIST $MACSIDFILE | sed 1d | cut -d " " -f2 >> $MACSIDFILE FREAK VERSION; DONT USE THIS } llxcfg(){ #echo "Borrando Temporales" rm $TMPMACLIST $INIMACLIST $SORTMACS $MAC2DELETE $NEWMACS llxcfg-dhcpd update } teachercomputer() { cat $TEACHERFILE | sort -u > $TEACHERFILETMP cat $TEACHERFILETMP > $TEACHERFILE TEACHERSPC='' while read line do AUX=$(echo $line | sed -e 's/^[0-9]$/0&/g') AUXPC=$(host "llx-pc$AUX" | cut -d" " -f4) TEACHERSPC="${AUXPC},${TEACHERSPC}" done < $TEACHERFILE llxcfg-systemvars add TEACHERS_LIST="$TEACHERSPC" } function restart_dhcp(){ llxcfg-dhcpd restart } withgui(){ MSG_50="$(gettext "Executing llxcfg-dhcpd update")" MSG_100="$(gettext "Completed")" ( echo "50" ; sleep 1 echo "# $MSG_50" ; sleep 1 restart_dhcp echo "100" ; sleep 1 echo "# $MSG_100" ; sleep 1 )| zenity --progress \ --title="$MSG_PROGRESS_TITLE" \ --text="$MSG_PROGRESS_TEXT" \ --percentage=0 } withoutgui(){ restart_dhcp } if [ $1 ]; then if [ $1 == -gui ]; then withgui else echo "USAGE: lwat2dhcp [launch in console mode] -gui [launch with gui] " fi else withoutgui fi