# 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"; }