Help - Search - Members - Calendar
Full Version: How to Delete Expired HOSTS Entries
B.I.S.S. Forums > Bluetack Software > HOSTS Section > Host Submissions
TOMaxwell
My HOSTS had grown to well over a 1.1MB and the burden on RAM Paging got to be annoying. I have actively added to my HOSTS file for YEARS, and had well over 34,000 entries at the beginning of the year. So I have spent much of this year cleaning out older HOSTS Entries that are no longer connected to their Former Domains.

At first, I used several batch files to a.) lookup each HOST on the Domain Name Server, b.) create a list of Expired HOSTS, c.) compare the 2 lists and d.) delete the Expired HOSTS Entries. Whew! That was a Beyatch because it took over 52 hours to run, and I had to cut the Hosts file up into small chunks to only do a little at a time.

*This* Batch file will read your HOSTS file and query the Domain Name Server to see if those Bass Turds are still operating. Unlike my previous attempts at cleaning the HOSTS, This one is a single step process. It will return a text file with the names of exisiting HOSTS entries, as opposed to a list of the dead entries that you later had to remove yourself in subsequent step(s).


Directions:
  • Copy and paste into your text editor of choice and name it " Good_Hosts.bat " (without the quotes) using path C:\WINDOWS\system32\drivers\etc\ or the correct path to your Windows NT or XP Machine. (Sorry, this will not work in previous versions of Windows).
  • Double Click the batch file and let it run during a time your compooter would otherwise be idle.
  • After it has completed, open up the newly generated Good_Hosts.txt file and copy/paste all of the entries into your existing HOSTS file, making sure to overwrite all existing entries.
  • ALWAYS Backup your HOSTS HOSTS before making any changes.

When I ran this overnight, it only took about 4.5 hours with about 23,000 Good HOSTS Entries, but my HOSTS file was already cleaned up and had only 8 new dead entries. ~YES, they DO Go Out of Business!

Your results may vary, and this may take a very long time to run if you have a LOT of expired Host names. The timeout is set for 10 seconds, and e.g., I had 11,000 expired entries, which swallowed 45 hours of CPU Time.

Also note that it will suck a lot of memory during that time, in case you are a DC Freaque.


CODE
@echo off

if not exist Good_Hosts.txt > Good_Hosts.txt echo #New Hosts File

echo This batch file will read the HOSTS file looking for
echo machine names that have a DNS "A" entry. A list
echo of those machines will be appended to a file named
echo "Good_Hosts.txt" in the default directory (usually
echo the same directory the batch file is in).

:: Wake the system up by pinging the primary DNS
call :PINGDNS

:: now read the first two words in each line of the HOSTS file

if not exist hosts goto DONE
for /f "tokens=1,2" %%x in (hosts) do call :TESTLINE %%x %%y
goto DONE

:TESTLINE
:: Arguments (dirty) - IP, MachineName
:: Did we get two arguments (was it a blank line)?

if [%2]==[] goto DONE

:: Is it a commented line?

echo %1 | find "#" > nul
if not errorlevel 1 goto DONE

:: Do an NS lookup. If "Address" shows up twice, it is good.

nslookup -type=A %2 2>nul | find /c "Address" | find "2" > nul
if not errorlevel 1 call :LOG %1 %2 else :TESTAGAIN %1 %2
echo %2
goto DONE

:TESTAGAIN
:: Arguments (clean) - IP, MachineName
:: Use ping as a time delay in case NS needed more time to
:: look up or in case my !@#$%?! DHCP lease expired again.

call :PINGDNS

:: Do an NS lookup. If "Address" shows up twice, it is good.

nslookup -type=A %2 2>nul | find /c "Address" | find "2" > nul
if not errorlevel 1 call :LOG %1 %2
goto DONE

:LOG
:: Arguments (clean) - IP, MachineName
:: Add the IP/name entry to the list.
echo %1 %2 >> Good_Hosts.txt
goto DONE

:PINGDNS
:: Used as a delay or to wake up the system by pinging the primary DNS.
:: Read the ipconfig command and separate things by the colon.

for /f "tokens=1,2 delims=:" %%x in ('ipconfig /all') do call :PINGDNS2 "%%x" %%y

:PINGDNS2
:: Arguments (dirty) - IpconfigEntry, IpconfigValue
:: If %1 (the beginning of the ipconfig line) has "DNS Servers"
:: in it, then %2 (the end of the ipconfig line) has the DNS IP.

echo %1 | find "DNS Servers" > nul
if errorlevel 1 goto DONE

:: Ping the DNS server, but limit the hop count so we *probably* will
:: get out of our local network, but *probably* won't harrass the DNS.

ping -i 4 %2 > nul
ping -i 4 %2 > nul

:DONE
r00ted
I've seen many attempts at batch coding. I know XeroGrid's Supertrick XG website has a "VerifyHOSTS" bundle, which contains 2 different batch files (tho, both appear to do the same thing...but I didn't really look at the code itself to see the difference(s)), and then a cool dude, Eric Phelp, also had some batch files on his site (can't remember the url tho, thanks for reminding me wink.gif ) that did about the same thing.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.