Solved

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

Posted on 2016-07-27
5
48 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

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 56

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 56

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 25

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

627 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