# 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 name. # 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 #### # 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 not open $path2hostsfile for reading"; @hostsfile = <$from_hostsfile>; } foreach my $line (@hostsfile) { next unless $line =~/^\s*$RE{net}{IPv4}\s+legba/; # skip unless it is the default entry $line = "$ARGV[0] legba\n"; # change the default host. $line is an alias for the corresponding element of @hostsfile! } { open(my $to_hostsfile, '>', $path2hostsfile) or die "Could not open $path2hostsfile for writing"; print $to_hostsfile @hostsfile; } } else { print "Missing or wrong argument!\nusage: set_default_host.pl default_IP\n"; }