Commit fbb56b98 authored by Robert Ricci's avatar Robert Ricci

New script: dhcpd_makeconf

Usage: dhcpd_makeconf [-h] [-v] <templatefile>
templatefile should be a skeleton dhcp.conf file, without entires for each node.
The string '%%nodetype=<type>' will be expanded to entries for all nodes of type
<type> in the following form:
host $ip {
	hardware ethernet $mac;
	option host-name '$node_id';
	fixed_address $ip;
}

The generated dhcp conf file is printed on stdout.

The -v option causes dhcpd_makeconf to use virtual names, rather than
physical ones, for hostnames.
parent e7443458
......@@ -1022,6 +1022,7 @@ outfiles="$outfiles Makeconf GNUmakefile \
db/webcontrol db/node_status db/genelists db/genelists.proxy \
discvr/GNUmakefile \
db/libdb.pm db/inuse db/avail db/mac2if db/nodeip db/showgraph \
db/dhcpd_makeconf \
ipod/GNUmakefile \
lib/GNUmakefile \
os/GNUmakefile os/split-image.sh os/imagezip/GNUmakefile \
......
......@@ -144,6 +144,7 @@ outfiles="$outfiles Makeconf GNUmakefile \
db/webcontrol db/node_status db/genelists db/genelists.proxy \
discvr/GNUmakefile \
db/libdb.pm db/inuse db/avail db/mac2if db/nodeip db/showgraph \
db/dhcpd_makeconf \
ipod/GNUmakefile \
lib/GNUmakefile \
os/GNUmakefile os/split-image.sh os/imagezip/GNUmakefile \
......
......@@ -10,7 +10,7 @@ include $(OBJDIR)/Makeconf
BIN_SCRIPTS = mac2if nalloc nfree nodeip
SBIN_SCRIPTS = avail inuse showgraph if2port backup webcontrol node_status \
genelists genelists.proxy
genelists genelists.proxy dhcpd_makeconf
LIBEXEC_SCRIPTS =
LIB_SCRIPTS = libdb.pm
......
#!/usr/bin/perl -w
use Mysql;
use Getopt::Long;
#
# Configure variables
#
my $DBNAME = "@TBDBNAME@";
my %opt = ();
GetOptions(\%opt,"h","v");
if ($opt{h}) { exit &usage; }
my $dbh = Mysql->connect("localhost",$DBNAME,"script","none");
$infile = shift @ARGV; # || exit &usage;
open(IF,"<$infile") or die "Unable to open $infile for reading\n";
while (<IF>) {
if (/^(\s*)\%\%nodetype=(\w+)/) {
my $spaces = $1;
my $nodetype = $2;
my $query = "SELECT n.node_id, i.IP, i.MAC ";
if ($opt{v}) {
$query .= ", r.vname ";
}
$query .= "FROM nodes AS n LEFT JOIN interfaces AS i ON n.node_id = i.node_id ";
$query .= "LEFT JOIN node_types AS t ON n.type = t.type ";
if ($opt{v}) {
$query .= "LEFT JOIN reserved AS r ON n.node_id = r.node_id ";
}
$query .= "WHERE n.type='$nodetype' AND i.card = t.control_net ";
$query .= "ORDER BY n.priority";
my $sth = $dbh->query($query);
while (@row = $sth->fetchrow) {
my $ip = $row[1];
my $mac = $row[2];
my $node_id;
if ($opt{v} && $row[3]) {
$node_id = $row[3];
} else {
$node_id = $row[0];
}
# Need to make MAC look right..
$mac =~ s/(..)\B/$1:/g;
print "${spaces}host $ip {\n";
print "${spaces}\thardware ethernet $mac;\n";
print "${spaces}\toption host-name \"$node_id\";\n";
print "${spaces}\tfixed-address $ip;\n";
print "${spaces}}\n\n";
}
} else {
# It's a regular line
print;
}
}
sub usage {
print "Usage: $0 [-h] [-v] <templatefile>\n";
print "-h Show this message\n";
print "-v Use virtual names, when possible, for hostnames\n";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment