Solved

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

Posted on 2016-07-27
5
40 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 24

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 53

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 53

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 24

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

816 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

9 Experts available now in Live!

Get 1:1 Help Now