1.

Solve : Delete all lines past specific line in "Host" file??

Answer»

I was reading another tpoic and it got me thinking. Here at our college we have users that somehow will add entries to the "HOSTS" file to add adresses.

I breifly googled this but did find anything for it, but do you guys think its possible to delete all line past a specific line?

I would like to delete ANY text below line 19. See below for example.

Code: [Select]# 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 (THIS IS LINE 19)
if you have gawk for windows (see my sig)
Code: [Select]c:\test> gawk "NR<=19" hostfile > temp
c:\test> ren temp hostfile

the BASIC logic is : from line 1 to line 19, print.
you can also do it with a batch programming, set a counter, increment it inside a for loop looping over the host file, when it reaches 19, stop. if it hasn't reach 19, REDIRECT to a temp file using >> operator. After all is done, rename it back to original host file nameI am suspicious. Surely a college HOSTS file is more likely to have entries past line 19, that is entries blocking named sites by redirecting them to localhost. We have a DNS server that controls most of our DNS entries. In addition there is nothing to be suspicious of, because deleting the extra lines a student adds will only allow us to control the DNS entries on our servers. That way student can not route their traffic elsewhere. I was only using 19 as an example anyway.Quote from: TheHoFL on May 30, 2009, 02:29:51 AM

I was only using 19 as an example anyway.

I know you were. I meant "lines beyond the first (localhost)".

Why aren't you locking down the Hosts files?

The students already should not be able to access the file, but some students find a way past our security. We have removed the right click menu, access to "My computer", etc... and yet, somehow they manage to figure out how to get past some of our security. So, i was looking at adding a bit of code (if possible) to a logon script. The code would always delete below whatever line we specified. That way even if they add entries, the entries will always be wiped out.You could just create a default hosts file set up the way you want it, call it Hosts.Default for example, and include a script to copy it to overwrite the Hosts file at each startup or logon.

Without knowing what OS the student machines are using, it is hard to propose a SCRIPTING SOLUTION.
All the PCs are running Windows XP Pro. I can't believe i didn't think about that!!


Discussion

No Comment Found