_UsUrPeR_
August 23rd, 2009, 03:40 AM
Hey all.
I have "finished" this quick script to create a globalconfig.xml out of a server's dhcpd.conf list for italc, and I KNOW that I have not done this properly. I mean, it works, but I would like to get some hints about shortening this up.
To be specific, I am sure that there's a better way to create and assign arrays than I have done in Findfile(). Inspect() is also the same way. The reason Inspect is there is to ensure that the file entered is a valid dhcpd.conf.
Thanks in advance.
#!/bin/bash
Pause()
{
key=""
echo
echo "This will create an italc globalconfig.xml"
echo "in the directory where this program has been run."
echo -n "Press any key to continue..."
stty -icanon
key=`dd count=1 2>/dev/null`
stty icanon
}
Locate()
{
Location=""
echo
echo -n "The location of your ltsp dhcpd.conf [/etc/ltsp/dhcpd.conf]:"
read Location
if [[ $Location = "" ]]
then
Location="/etc/ltsp/dhcpd.conf"
fi
}
FindFile()
{
if [[ ! -e "$Location" ]]
then
echo "File not found!"
exit 1
fi
cat $Location |grep fixed-address|cut -d " " -f2|cut -d ";" -f1 > /tmp/cat_ip
cat $Location |grep "hardware ethernet"|cut -d " " -f3|cut -d ";" -f1 > /tmp/cat_mac
}
Inspect()
{
RETURN=`ls -alh /tmp/cat_ip |cut -d " " -f5`
if [[ $RETURN == "0" ]]
then
echo "Sorry, that was not a proper dhcpd.conf"
rm /tmp/cat_ip
exit 1
else
Enumerate
fi
}
Enumerate()
{
i=1
while [[ `sed -n "$i{p;q;}" /tmp/cat_ip` != "" ]]
do
ip_array[$i]=`sed -n "$i{p;q;}" /tmp/cat_ip`
mac_array[$i]=`sed -n "$i{p;q;}" /tmp/cat_mac`
let "i=$i+1"
done
}
Create()
{
o=1
count=`wc -l /tmp/cat_ip|cut -d " " -f1`
echo '<?xml version="1.0"?>' > ./globalconfig.xml
echo '<!DOCTYPE italc-config-file>' >>./globalconfig.xml
echo '<globalclientconfig version="1.0.9" >' >>./globalconfig.xml
echo ' <body>' >>./globalconfig.xml
echo ' <classroom name="SchoolTech Classroom" >' >>./globalconfig.xml
while [[ $o -le $count ]]
do
echo ' <client hostname"'${ip_array[$o]}'" mac="'${mac_array[$o]}'" type="0" id="'$o'" name="client'$o'" />' >>./globalconfig.xml
let "o=$o+1"
done
echo ' </classroom>' >>./globalconfig.xml
echo ' </body>' >>./globalconfig.xml
echo '</globalclientconfig>' >>./globalconfig.xml
rm /tmp/cat_ip
rm /tmp/cat_mac
}
Pause
Locate
FindFile
Inspect
Create
echo
echo "Your file is complete."
exit 1
I have "finished" this quick script to create a globalconfig.xml out of a server's dhcpd.conf list for italc, and I KNOW that I have not done this properly. I mean, it works, but I would like to get some hints about shortening this up.
To be specific, I am sure that there's a better way to create and assign arrays than I have done in Findfile(). Inspect() is also the same way. The reason Inspect is there is to ensure that the file entered is a valid dhcpd.conf.
Thanks in advance.
#!/bin/bash
Pause()
{
key=""
echo
echo "This will create an italc globalconfig.xml"
echo "in the directory where this program has been run."
echo -n "Press any key to continue..."
stty -icanon
key=`dd count=1 2>/dev/null`
stty icanon
}
Locate()
{
Location=""
echo
echo -n "The location of your ltsp dhcpd.conf [/etc/ltsp/dhcpd.conf]:"
read Location
if [[ $Location = "" ]]
then
Location="/etc/ltsp/dhcpd.conf"
fi
}
FindFile()
{
if [[ ! -e "$Location" ]]
then
echo "File not found!"
exit 1
fi
cat $Location |grep fixed-address|cut -d " " -f2|cut -d ";" -f1 > /tmp/cat_ip
cat $Location |grep "hardware ethernet"|cut -d " " -f3|cut -d ";" -f1 > /tmp/cat_mac
}
Inspect()
{
RETURN=`ls -alh /tmp/cat_ip |cut -d " " -f5`
if [[ $RETURN == "0" ]]
then
echo "Sorry, that was not a proper dhcpd.conf"
rm /tmp/cat_ip
exit 1
else
Enumerate
fi
}
Enumerate()
{
i=1
while [[ `sed -n "$i{p;q;}" /tmp/cat_ip` != "" ]]
do
ip_array[$i]=`sed -n "$i{p;q;}" /tmp/cat_ip`
mac_array[$i]=`sed -n "$i{p;q;}" /tmp/cat_mac`
let "i=$i+1"
done
}
Create()
{
o=1
count=`wc -l /tmp/cat_ip|cut -d " " -f1`
echo '<?xml version="1.0"?>' > ./globalconfig.xml
echo '<!DOCTYPE italc-config-file>' >>./globalconfig.xml
echo '<globalclientconfig version="1.0.9" >' >>./globalconfig.xml
echo ' <body>' >>./globalconfig.xml
echo ' <classroom name="SchoolTech Classroom" >' >>./globalconfig.xml
while [[ $o -le $count ]]
do
echo ' <client hostname"'${ip_array[$o]}'" mac="'${mac_array[$o]}'" type="0" id="'$o'" name="client'$o'" />' >>./globalconfig.xml
let "o=$o+1"
done
echo ' </classroom>' >>./globalconfig.xml
echo ' </body>' >>./globalconfig.xml
echo '</globalclientconfig>' >>./globalconfig.xml
rm /tmp/cat_ip
rm /tmp/cat_mac
}
Pause
Locate
FindFile
Inspect
Create
echo
echo "Your file is complete."
exit 1