none
Edit Host file RRS feed

  • Question

  • Requirement :

    How do I edit Host file using script? I tried to do the same using batch file, is appended below.

    @echo off

    set hostspath=%windir%\System32\drivers\etc\hosts

    echo IP         abcd >> %hostspath%
    echo IP1     xyz.it.edu >> %hostspath%
    exit

    If I am putting this in startup script but everytime when clients boot their machines, same lines are being added repeatedly...Kindly help me in doing the script which will check the existing lines and if lines are there then script will get terminated else host file will get edited.

    Monday, March 25, 2013 7:36 AM

Answers

  • Hi Bill,

    I am trying to implement the same using Vbscript. We are having few systems, stiil using XP. One application which is going to come up in our env and dev team requires enties to be done in the HOST FILE.

    What is wrong with putting this in the DNS server?

    What does XP have to do with this.  The hosts file is a hangover from Unix days and is used when we have no DNS servers.

    Also note that you format for the edit from your original post appears to be the wrong format.

    In VBScript - open the file - seek the line - oif not found append the line.

    See the repository for examples of how to use files with VBScript.


    ¯\_(ツ)_/¯

    • Marked as answer by Tech Olive Monday, April 8, 2013 9:29 AM
    Thursday, March 28, 2013 8:17 AM
  • One application which is going to come up in our env and dev team requires enties to be done in the HOST FILE.

    But why? As jrv says, it is best to use DNS rather than edit hosts files on individual machines. Editing hosts files on individual machines scales exceedingly poorly (this problem was why DNS was invented in the first place!), and is going to be problematic on newer versions of Windows that use UAC (only administrators can change the hosts file). In addition, some anti-malware software prevent changes to hosts files by default because this is common activity of malware.

    So from a security and scalability point of view: You really need a very, very, very good reason for wanting to do this, because it is almost always not recommended.

    Bill

    • Marked as answer by Tech Olive Monday, April 8, 2013 9:29 AM
    Thursday, March 28, 2013 2:26 PM
    Moderator

All replies

  • You should not use the hosts file.  It is legacy and no longer useful in WIndows Vista and later.  Use DNS.

    You cannot use a script like this to add to a file once.  YOU need to check the file to determin if it has already been changed.

    VBScript would be a better choice for this.  Since it is a one off issue I would recommend using PowerSHell remotely once.

    Again.  Have your admins put this in the DNS server it is a safer way to do it in a domain.


    ¯\_(ツ)_/¯

    Monday, March 25, 2013 7:50 AM
  • Here is how you can do it if you wish to go ahead in spite of JRV's advice:

    @echo off
    set hostspath=%windir%\System32\drivers\etc\hosts
    type %HostsPath% | find /i "xyz.it.edu" && goto :eof
    echo IP         abcd >> %hostspath%
    echo IP1     xyz.it.edu >> %hostspath%

    • Proposed as answer by biorpg Monday, December 28, 2015 7:16 AM
    Monday, March 25, 2013 8:21 AM
  • Why do you need to edit hosts files on client machines?

    Bill

    Monday, March 25, 2013 2:20 PM
    Moderator
  • Hi Bill,

    I am trying to implement the same using Vbscript. We are having few systems, stiil using XP. One application which is going to come up in our env and dev team requires enties to be done in the HOST FILE.

    Thursday, March 28, 2013 8:11 AM
  • Hi Bill,

    I am trying to implement the same using Vbscript. We are having few systems, stiil using XP. One application which is going to come up in our env and dev team requires enties to be done in the HOST FILE.

    What is wrong with putting this in the DNS server?

    What does XP have to do with this.  The hosts file is a hangover from Unix days and is used when we have no DNS servers.

    Also note that you format for the edit from your original post appears to be the wrong format.

    In VBScript - open the file - seek the line - oif not found append the line.

    See the repository for examples of how to use files with VBScript.


    ¯\_(ツ)_/¯

    • Marked as answer by Tech Olive Monday, April 8, 2013 9:29 AM
    Thursday, March 28, 2013 8:17 AM
  • One application which is going to come up in our env and dev team requires enties to be done in the HOST FILE.

    But why? As jrv says, it is best to use DNS rather than edit hosts files on individual machines. Editing hosts files on individual machines scales exceedingly poorly (this problem was why DNS was invented in the first place!), and is going to be problematic on newer versions of Windows that use UAC (only administrators can change the hosts file). In addition, some anti-malware software prevent changes to hosts files by default because this is common activity of malware.

    So from a security and scalability point of view: You really need a very, very, very good reason for wanting to do this, because it is almost always not recommended.

    Bill

    • Marked as answer by Tech Olive Monday, April 8, 2013 9:29 AM
    Thursday, March 28, 2013 2:26 PM
    Moderator
  • An interesting not:

    My developers used to pull this kind of thing all of the time because they did not understand networking and were basically lazy (us programmers are all guilty of that (being lazy)).  I would kick it back.  These programmers used to have another very annoying habit.  They always built and tested their programs from an admin account.  When the program wouldn't work their answer was that the program required admin privileges to run.  SInce I was a trined and certified network engineer and a programmer they could not get away with this.  Other managers didn't fare so well.

    The administrator of a network should never allow a programmer to dictate how to configure a network.  This is just insanity and usually comes from a lack of understanding of programing, networks, security and software design.

    Use the code in the repository to alter teh file if you insist on doing this but, as Bil and I have noted, you shouldn't do it for many, many reasons.


    ¯\_(ツ)_/¯

    Thursday, March 28, 2013 2:48 PM
  • I have convinced them to put the same in DNS server and it's working fine ... Thanks all of you for sharing knowledge and support.
    Monday, April 8, 2013 9:29 AM
  • I have convinced them to put the same in DNS server and it's working fine ... Thanks all of you for sharing knowledge and support.

    I am glad to see that you have chosen a more stable and modern approach to name resolution.  There should be only one source of name resolution in a network that supports AD or you can have issues in the future.  The 'hosts' file is all but obsolete and, as such, can easily be forgotten as a source of names.  That is partly why Microsoft has removed even 'localhosts' from this file in the newest systems.


    ¯\_(ツ)_/¯

    Monday, April 8, 2013 10:42 AM
  • findstr /c:"127.0.0.1       localhost" "%windir%\system32\drivers\etc\hosts"
    If errorlevel=0 echo 127.0.0.1       localhost >>"%windir%\system32\drivers\etc\hosts"

    David Candy

    • Proposed as answer by biorpg Monday, December 28, 2015 7:13 AM
    • Unproposed as answer by Bill_StewartModerator Wednesday, December 30, 2015 4:14 PM
    Monday, April 8, 2013 10:45 AM
  • findstr /c:"127.0.0.1       localhost" "%windir%\system32\drivers\etc\hosts"
    If errorlevel=0 echo 127.0.0.1       localhost >>"%windir%\system32\drivers\etc\hosts"

    David Candy


    David - too little too late - read rest of thread.

    ¯\_(ツ)_/¯

    Monday, April 8, 2013 10:51 AM
  • Believe it or not, there still are platforms that do not use DNS for various reasons. You must be working in a pretty limited or static environment to respond with such a suggestion.
    Tuesday, July 8, 2014 1:49 PM
  • Believe it or not, there still are platforms that do not use DNS for various reasons. You must be working in a pretty limited or static environment to respond with such a suggestion.

    It is not a home user environment and in  workgroups it is very easy to set up a DNS server.  Every server has one available.  IN every case of a domain there is a DNS server.

    The OP found that adding a DNS server was the answer.  Read the whole thread before you start nagging.


    ¯\_(ツ)_/¯

    Tuesday, July 8, 2014 1:54 PM

  • David - too little too late - read rest of thread.

    ¯\_(ツ)_/¯

    Too late? Agreed - but it is not the fault of the only person to answer the question. As the only answer to actual question, how is it too little?

    The question "Why?" is a lazy, invalid response to any question unless it is required to determine which of multiple answers is most correct, not to avoid admitting lack of knowledge or supplying an answer you don't wish for others to have.


    • Edited by biorpg Monday, December 28, 2015 7:11 AM
    Monday, December 28, 2015 7:09 AM