in reply to update winxp hosts file with new IP address
# 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.
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.# 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"; }
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 | |
by notasaint (Novice) on May 09, 2008 at 02:25 UTC |