Solved

How to add an entry with a condition to hosts file?

Posted on 2016-07-27
5
35 Views
Last Modified: 2016-09-01
I need to write a script to verify if one specific entry "10.1.17.21  server1" exists in the hosts file or not. If yes, do nothing, else insert that entry into the hosts file.  
This script will be put in the login script to assure all users with this entry in their hosts file.  

Can you help please?
0
Comment
Question by:Castlewood
  • 2
  • 2
5 Comments
 
LVL 23

Expert Comment

by:NVIT
ID: 41731497
findstr /i "10.1.17.21  server1" c:\windows\system32\drivers\etc\hosts

if errorlevel 1 echo "10.1.17.21  server1" >>c:\windows\system32\drivers\etc\hosts

Open in new window


I don't think you can put it in a users login script because hosts may be protected... Unless user is an admin.

You could put it in a gpo in the Startup script, which should work.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41731505
Keep in mind this will need to happen with elevated user permissions on most modern versions of Windows.

~bp
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points (awarded by participants)
ID: 41731520
NVIT, I might be overthinking it a bit, I do that, but you might want to make it a bit more robust.  A couple of thoughts that come to mind:

- If for some reason the entry is in the file, but commented out (likely a # on the left part of the line), then we won't add it again, even though we would want to I think.  So perhaps use REGEX for the search and require the IP address to be at the start of the line, preceded only by whitespace.

- Using REGEX, you could handle multiple spaces other than what is assumed between the IP and host name.

- It's possible that we could add the same host name twice to the file, with different IPs, using this approach, but that risk seems too low to code for.  I think Windows would just pick the first matching host name in the file and ignore the second.

~bp
0
 
LVL 23

Accepted Solution

by:
NVIT earned 400 total points (awarded by participants)
ID: 41753316
Bill,

This seems to work.

findstr /r /b "10.1.17.21 *server1" c:\windows\system32\drivers\etc\hosts
if errorlevel 1 echo 10.1.17.21  server1 >>c:\windows\system32\drivers\etc\hosts

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now