in reply to update winxp hosts file with new IP address

My hosts file looks like this:
# Copyright (c) 1993-1999 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host nam +e. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host 127.0.0.1 localhost 192.168.1.108 chrome 192.168.1.105 legba

Suppose the aptly named legba machine is the default and you need to change its IP address.

# set_default_host.pl use strict; use Regexp::Common qw /net/; if (scalar @ARGV == 1 and $ARGV[0]=~/^$RE{net}{IPv4}$/) { my $path2hostsfile = 'C:/Windows/System32/drivers/etc/hosts'; my @hostsfile; { open(my $from_hostsfile, '<', $path2hostsfile) or die "Could n +ot open $path2hostsfile for reading"; @hostsfile = <$from_hostsfile>; } foreach my $line (@hostsfile) { next unless $line =~/^\s*$RE{net}{IPv4}\s+legba/; # skip unles +s it is the default entry $line = "$ARGV[0] legba\n"; # change the default host. $li +ne is an alias for the corresponding element of @hostsfile! } { open(my $to_hostsfile, '>', $path2hostsfile) or die "Could n +ot open $path2hostsfile for writing"; print $to_hostsfile @hostsfile; } } else { print "Missing or wrong argument!\nusage: set_default_host.pl defa +ult_IP\n"; }
Save this program as set_default_host.pl and run it in a "dos box" (command line). It takes one argument: the new default IP.

The program checks if this argument is present and is indeed an IP.

You will have to adapt the program slightly so the regex matches your default host. It is unlikely your default host will be called 'legba' although obviously that is highly recommended!

Update: added "or die ..." to open

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: update winxp hosts file with new IP address
by notasaint (Novice) on May 08, 2008 at 11:20 UTC

    Thank you all for your responses. You are correct in that I should have provided more detail. Normally I would have. I will blame the late hour of the posting and my failed attempts to get anything working on my own.

    CountZero, you hit the situation precisely on the head. Thank you very much for the detailed example! The regular expressions search for the correct line to replace the IP address on is the main part I was having issues with.

    -nas

      This is what I came up with based on the information provided and the desire to have the user prompted for the updated IP address.

      Please let me know if I'm making some serious Perl mistakes or if this looks good. Hints about better coding are always appreciated.

      Update: Removed the code here. I hadn't tested it on the intended workstation before posting it and it turned out that it didn't work properly because the laptop image didn't include the Regex module.

      Thanks once again for the guidance!