Menu

SetComputerNameByDhcpScript

Juan Jose Pablos

Last modified 5 years ago Last modified on 05/05/09 12:28:42

The following code can be placed in your site/config.pl. It sets the computer name of the build from the hostname of the computer as it is set by your DHCP server and is tested with the Linux boot disk.

So for example, if I am using dhcpd under Linux as my dhcp server, and I have this in my /etc/dhcpd.conf configuration file:

host vm-build-test.mydomain.com {
   hardware ethernet 00:0C:23:6B:63:4F;
   fixed-address 192.168.14.223;
}

Then when I boot the machine with this MAC, my DHCP server will assign it that address and the hostname vm-build-test.mydomain.com. Whether the client doing the DHCP request takes any notice of it depends on the client, but Unattended does when booted using the Linux boot disk. The script will set the hostname of the computer to the first part of the DNS name shown above (the bit before the first period (.)). You can then query the hostname and use it to set the computername, which unattended will then use in the answer file it creates. All this happens before Windows Setup starts.

Final notes:

  • Tested with the Linux boot disk; can't use the DOS boot disk so can't test that but it would be worth you trying
  • Tested by network booting, but should have no problems using the CD or floppy
  • Will override anything in your site/unattend.txt
  • Hostnames must be fully qualified DNS names
  • If the script fails to split the name, it will fail quietly and unattended will fall back to watever is in your site/unattend.txt or you typed in when it prompted you
  • The script gets run by unattended just before Windows Setup starts, so if you haven't put anything in your site/unattend.txt you will be prompted and then this script will run. Stick anything in site/unattend.txt to stop unattended prompting you.
  • I put the two sleep lines in so I would have a chance to see what was happening - feel free to take them out.

    use warnings;
    use strict;
    use Sys::Hostname;

    Start by trying to decide the computer name from the DHCP DNS name

    my $host = hostname;
    my $default_computer_name = $u->{'UserData'}->{'ComputerName'};
    print "Trying to determine the computer name...\n";
    print "Hostname name from DHCP is \"$host\"\n";
    $host =~ m/^(([A-Z]|[a-z]|[0-9]|-)+)(..*)/;
    my $short_host = $1;
    my $dns_domain = $3;
    if( ! defined($short_host))
    {
    # if we couldn't get a value for the hostname from the DHCP assigned
    # value, don't worry. Just go with the unattend,txt default of value
    # entered at prompt
    print "WARNING: Couldn't set computer name from hostname. Hostname should be fully qualified domain name using only A-Z, a-z, 0-9 and -.\n\nUsing default from unattend.txt or prompt of $default_computer_name instead\n";
    sleep 10;
    }
    else
    {
    print "Success! Computer name will be \"$1\". \nDomain name is: $3\n";
    $u->{'UserData'}->{'ComputerName'} = $short_host;
    sleep 3;
    }

[IanGibbs] <flash666 at yahoo.com>

cool


Related

Wiki: IanGibbs
Wiki: Scripts
Wiki: SetComputerNameFromDnsScriptWithFallback

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.