Skip to content

Instantly share code, notes, and snippets.

@kelp
Created September 9, 2008 23:29
Show Gist options
  • Save kelp/9789 to your computer and use it in GitHub Desktop.
Save kelp/9789 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Generate the pxelinux config for new systems.
#
#set -e
usage(){
cat << EOF
Usage: $0 --host=<hostname> --ip=<ip> --netmask=<netmask> --gateway=<gateway>
Options:
--host=<hostname> Name for new host
--ip=<ip> ip for new host
--netmask=<netmask> Netmask for new host
--gateway=<gateway> Gateway for new host
--i386 Set the arch to i386, default is amd64
--verbose Verbose mode. Silent without this
--help Print this help
EOF
exit 1
}
if [ $# -lt 4 ]; then
usage
fi
host=""
ip=""
netmask=""
gateway=""
verbose=""
arch=""
SHORTOPTS="H:i:n:g:vhI"
LONGOPTS="host:,ip:,netmask:,gateway:,i386,verbose,help"
OPTS=$(getopt -o $SHORTOPTS --long $LONGOPTS -n "$progname" -- "$@")
while [ $# -gt 0 ]; do
case $1 in
-H|--host)
host="$2"
shift 2;;
-i|--ip)
ip="$2"
shift 2;;
-n|--netmask)
netmask=$2
shift 2;;
-g|--gateway)
gw=$2
shift 2;;
-v|--verbose)
verbose="true"
shift;;
-I|--i386)
arch=".i386"
shift;;
-h|--help)
usage;;
--)
shift
break;;
*)
usage;;
esac
done
if [[ -z "$host" || -z "$ip" || -z "$netmask" || -z "$gw" ]]; then
usage
fi
if [ "$verbose" = "true" ]; then
set -x
fi
# Templates to generate configs for new hosts
PXETEMPLATE=/var/lib/tftpboot/hardy${arch}/pxelinux.cfg/default.template
SEEDTEMPLATE=/var/www/preseed/hardy-preseed.template
NAMESERVERS="10.1.0.52 10.1.0.51 10.1.0.84"
PRESEED="/var/www/preseed/hardy-preseed-${host}.txt"
for i in $(echo ${ip}| sed -e "s/\./ /g"); do
HEXIP=${HEXIP}`printf '%02X' $i`
done
sed "s/_HOST_/${host}/g" $PXETEMPLATE > /var/lib/tftpboot/hardy${arch}/pxelinux.cfg/$HEXIP
sed "s/_HOST_/${host}/" $SEEDTEMPLATE > $PRESEED
sed -i "s/_IP_/${ip}/" $PRESEED
sed -i "s/_NETMASK_/${netmask}/" $PRESEED
sed -i "s/_GW_/${gw}/" $PRESEED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment