| 1. |
Solve : help a novice - trying to modify hosts file to pad ip with zeros? |
|
Answer» Trying to pad hosts file so sorting will work properly. Trying to pad hosts file so sorting will work properly.Hosts file does not need sorting. Quote from: Geek-9pm on July 17, 2012, 09:56:03 PM Hosts file does not need sorting.While that is true it may help the user manage a large hosts file. But I personally would sort the hosts file by the Domain name. Not the IP address. But there are plenty of 3rd party hosts file editors out there and I would bet some of them have the capability to sort the hosts file when it is viewing it.Quote from: Squashman on July 18, 2012, 05:43:25 AM While that is true it may help the user manage a large hosts file. But I personally would sort the hosts file by the Domain name. Not the IP address. But there are plenty of 3rd party hosts file editors out there and I would bet some of them have the capability to sort the hosts file when it is viewing it.Exactly!Am I missing something here? If you add 2 zeroes to a decimal byte at the beginning of a dotted quad IP address you clobber it, because Windows interprets numbers with leading zeroes as octal. (0074 octal is 60). Code: [Select]C:\>ping www.google.com Pinging www.l.google.com [74.125.132.104] with 32 bytes of data: Reply from 74.125.132.104: bytes=32 time=33ms TTL=46 Reply from 74.125.132.104: bytes=32 time=31ms TTL=46 Reply from 74.125.132.104: bytes=32 time=60ms TTL=46 Reply from 74.125.132.104: bytes=32 time=38ms TTL=46 Ping statistics for 74.125.132.104: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 31ms, MAXIMUM = 60ms, Average = 40ms C:\>ping 74.125.132.104 Pinging 74.125.132.104 with 32 bytes of data: Reply from 74.125.132.104: bytes=32 time=29ms TTL=46 Reply from 74.125.132.104: bytes=32 time=28ms TTL=46 Reply from 74.125.132.104: bytes=32 time=31ms TTL=46 Reply from 74.125.132.104: bytes=32 time=45ms TTL=46 Ping statistics for 74.125.132.104: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 28ms, Maximum = 45ms, Average = 33ms C:\>ping 0074.125.132.104 Pinging 60.125.132.104 with 32 bytes of data: Request timed out. Request timed out. Request timed out. Request timed out. Ping statistics for 60.125.132.104: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss) Quote Am I missing something here? If you add 2 zeroes to a decimal byte at the beginning of a dotted quad IP address you clobber it, because Windows interprets numbers with leading zeroes as octal. (0074 octal is 60). Probably true, so why not use spaces for FIRST octet? This little piece of doggerel will show the mechanics of doing this, but as mentioned there is no reason to sort the Hosts file. Code: [Select]@echo off setlocal enabledelayedexpansion if exist sorted del sorted if exist unsorted del unsorted for /f "tokens=1-2*" %%i in (c:\windows\system32\drivers\etc\hosts) do ( for /f "tokens=1-4 delims=." %%r in ("%%i") do ( set "ip1= %%r" set "ip2=00%%s" set "ip3=00%%t" set "ip4=00%%u" echo !ip1:~-3!.!ip2:~-3!.!ip3:~-3!.!ip4:~-3! %%j >> unsorted ) ) sort /+1 unsorted /o sorted Be patient. Code runs at the speed of continental DRIFT. Note: if you change the sort key offset, you can sort on the domain name.Here is a discussion of this for UNIX http://www.madboa.com/geek/sort-addr/ The UNIX sort has more options that the Windows sort. Sorting IP address presents a challenge.This works here and handles lines with rems. The delims is set to dotTABspace and the TAB is required because Windows 7 at least converts hosts space delimiters to TABS. Input file is hosts.txt and output file is HOSTS Code: [Select]@echo off SETLOCAL ENABLEDELAYEDEXPANSION del *.tmp 2>nul FOR /F "tokens=1,2,3,4,* delims=. " %%a IN (hosts.txt) do ( set a=%%a set ip1= set ip2= set ip3= set ip4= if not "!a:~0,1!"=="#" ( set "ip1=00%%a" set "ip2=00%%b" set "ip3=00%%c" set "ip4=00%%d" echo !ip1:~-3!.!ip2:~-3!.!ip3:~-3!.!ip4:~-3! >>file.tmp echo !ip1:~-3!.!ip2:~-3!.!ip3:~-3!.!ip4:~-3! %%a.%%b.%%c.%%d %%e ) else ( >>file3.tmp echo %%a %%b %%c %%d %%e ) ) sort <file.tmp >file2.tmp FOR /F "tokens=1,*" %%b IN (file2.tmp) do ( >>file4.tmp echo %%c ) copy /b file3.tmp + file4.tmp hosts >nul del *.tmp pause endlocalModification to also cater for source files that do not contain any comment lines. Code: [Select]@echo off SETLOCAL ENABLEDELAYEDEXPANSION del *.tmp 2>nul FOR /F "tokens=1,2,3,4,* delims=. " %%a IN (hosts.txt) do ( set a=%%a set ip1= set ip2= set ip3= set ip4= if not "!a:~0,1!"=="#" ( set "ip1=00%%a" set "ip2=00%%b" set "ip3=00%%c" set "ip4=00%%d" echo !ip1:~-3!.!ip2:~-3!.!ip3:~-3!.!ip4:~-3! >>file.tmp echo !ip1:~-3!.!ip2:~-3!.!ip3:~-3!.!ip4:~-3! %%a.%%b.%%c.%%d %%e ) else ( >>file3.tmp echo %%a %%b %%c %%d %%e ) ) sort <file.tmp >file2.tmp FOR /F "tokens=1,*" %%b IN (file2.tmp) do ( >>file4.tmp echo %%c ) if exist file3.tmp ( copy /b file3.tmp + file4.tmp hosts >nul ) else ( move file4.tmp hosts >nul ) del *.tmp pause endlocal |
|