use strict; use warnings; my $file_read='C:/WINDOWS/system32/drivers/etc/hosts'; my $ha=[]; my $names={}; my $ips={}; #open (my $hf,'<',$file_read) or die "Can't open $file_read: $!";; my $hf=\*DATA; while (my $line=<$hf>) { chomp $line; # print $line."\n"; my $h={}; $h->{line}=$line; my ($pre,$comment)=split('#',$line,2); $h->{comment}=$comment if ($comment); if ($pre) { my @parts=split(/\s+/,$pre); if (scalar(@parts)>1) { my $ip=shift @parts; $h->{ip}=$ip; push @{$ips->{$ip}},@parts; $h->{names}=[@parts]; for my $name (@parts) {$names->{$name}=$ip; } } # parts } # pre push @$ha,$h; } #line use Data::Dumper; print Dumper($ha); print Dumper($ips); print Dumper($names); #open($out, '>', $file_write)|| die "\n error opening file $file_write \n"; my $out=\*STDOUT; print $out "#Hosts file\n"; print $out "#Last Modified -> ". localtime() . "\n"; print $out "# \n"; print $out "# localhost: Needs to stay like this to work\n"; print $out "127.0.0.1\t localhost\n"; print $out "# \n"; delete $names->{localhost} if ($names->{localhost}); my @ksort=sort {my $r1=$names->{$a} cmp $names->{$b}; return $r1 if($r1); $a cmp $b} keys(%$names); for my $name (@ksort) { print $out $names->{$name}."\t".$name."\n"; } __DATA__ # 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 ads.pointroll.com scanner2.malware-scan.com localhost adsys.townnews.com adimages.townnews.com ad.doubleclick.net pagead2.googlesyndication.com ad.yieldmanager.com view.atdmt.com ads.revsci.net servedby.advertising.com jeffcity30.autochooser.com performanceoptimizer.com cache.fimservecdn.com pixel.quantserve.com ads.yimg.com this.content.served.by.adshuffle.com img-cdn.mediaplex.com cache.fimservecdn.com adserving.cpxinteractive.com pixel.quantserve.com s0.2mdn.net 127.0.0.1 www.zip2save.com d1.openx.org c3.openx.org partner.googleadservices.com media.ljworld.com everythingmidmo.com www.everythingmidmo.com edge.quantserve.com pixel.quantserve.com ad-g.doubleclick.net ads.yimg.com ad.wsod.com s0.2mdn.net s0.2mdn.net 192.168.1.1 nat.mylan 192.168.1.100 dhcp1.nat.mylan 192.168.2.1 wifi.mylan 192.168.2.100 dhcp1.wifi.mylan 192.168.1.234 lxle0 lxle0.mylan 192.168.1.200 me me.mylan 192.168.1.200 me me.mylan 192.168.254.251 wan.mylan