From 7ff4171b79f59d858d693d84125a8e52795304c4 Mon Sep 17 00:00:00 2001 From: Jed Reynolds Date: Mon, 14 Oct 2019 15:37:47 -0700 Subject: [PATCH] Script modifies dhclient conf files to add hostnames --- add-dhcp-hostname.pl | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 add-dhcp-hostname.pl diff --git a/add-dhcp-hostname.pl b/add-dhcp-hostname.pl new file mode 100755 index 00000000..cb005389 --- /dev/null +++ b/add-dhcp-hostname.pl @@ -0,0 +1,57 @@ +#!/bin/perl +# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- +# use this script after bringing up stations with a DHCP lease +# then admin-down the stations. Go into /home/lanforge/vr_conf +# and change the settings with this script on all the files +# in a manner like this: +# +# root# cd /home/lanforge/vr_conf +# root# ~lanforge/scripts/add-dhcp-hostname.pl dhclient_sta*conf +# +# At this point you can bring up your stations. If you need to +# check a lanforge dhcpd lease file, please look at the dhcp database +# found in /home/lanforge/vr_conf/vrcx_br1_dhcp_lease.db +# entries should have a "client-hostname: " entry. +# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- +use strict; +use warnings; +use diagnostics; +$| = 1; + +if (@ARGV < 1) { + print "No files requested.\n"; + exit 1; +} +my @chars = ("A".."Z", "a".."z", "0".."9"); + +my $rand_name = ""; +for my $fname (@ARGV) { + print "File: $fname "; + + my @lines = `cat $fname`; + chomp @lines; + die("Unable to open $fname for writing: $!") + unless open(my $fh, ">", $fname); + + my $ifname=''; + for my $line (@lines) { + next if ($line =~ /^\s*$/); + next if ($line =~ /^. LANforge-generated/); + next if ($line =~ /^. Remove first/); + next if ($line =~ /^. automatically over-write/); + next if ($line =~ /^send host-name/); + if ($line =~ /^# sta\d+/) { + ($ifname) = $line =~ /^# (sta\d+)\s*$/; + } + print $fh "$line\n"; + } + $rand_name = ""; + $rand_name .= $chars[ rand @chars] for 1..8; + #print "* "; + #print "$rand_name\n"; + print $fh "interface \"$ifname\" {\n"; + print $fh " send host-name \"$rand_name\";\n"; + print $fh "}\n"; + close $fh; +} +print "done\n";