Solved

Before running batch file how to detect the available entry in host file

Posted on 2010-11-09
6
758 Views
Last Modified: 2012-06-22
Hello Experts,

Need urgent help!

I want to update windows hosts file for certain entries like IP-Address & Host-name by a DOS batch script.

But before adding these entries i want to check if any entry already exists in
hosts file or not. If exists, it will not be added.

In hosts file, existing entries i.e. ip and hostname may be separated by white-space
or tab-space which i don't know before hand. So while checking if the entry exists or not i just have to compare the strings ignoring the tab/white-space in between ip and host-name.

Valuable advices will be highly appreciated.

Thanks
0
Comment
Question by:newbie27
[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
6 Comments
 
LVL 7

Expert Comment

by:TechnoChat
ID: 34099348
You can use following command to check a specific entry on host file..

more %windir%\system32\drivers\etc\hosts | findstr string

Eg.
Following command will output the current localhost details.
more %windir%\system32\drivers\etc\hosts | findstr localhost

Thanks
Saugata
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 250 total points
ID: 34100062
Personally I'd do it slightly different but same sort of tact. The /i deals with upper/lower case in findstr and you can use || to check if the errorlevel returned is 0 (i.e. not found) and then display the need to add and append it to the file.  Give it a try anyway:

Steve

@echo off
set hosts="%windir%\system32\drivers\etc\hosts"

call :check host1 1.1.1.1
call :check host2 1.1.1.2
call :check host3 1.1.1.3

exit /b

:check
REM Check if first parameter exists in hosts file.  If it doesn't then echo new value to end of file
findstr /i "%1" || (
  echo %1  %2 >>%hosts%
  echo Adding %1  %2 to HOSTS table
)

0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 34101056
Keep in mind though that on Windows 7 and Vista the hosts file is typically protected, and you'll need admin access to update it.

Also, is it important that the exact match of hostname and IP address already be in the table, or as Steve has demonstrated, is looking only one okay?  The other thing Steve's code won't catch is if that line is commented out.

Steve, what do you think about using FINDSTR with a regex expression, looking for the hostname followed by the IP address with just white space between them?  In addition, maybe we could rig a regex that makes sure there isn't a semi colon at the left of those?

~bp
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 34101181
Good idea. I didn't have much time so went for the easy option, I suppose it depends what is likely to be in the table for this environment, and as you say many users may not be able to update / access HOSTS for writing anyway.

I'm busy at the mo. so go for it with the alternate regex version as you wish.

Steve
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 34101506
Okay, here's a change that would get closer.  The problem with this is that FINDSTR regex support doesn't include any way to look for tab characters.  So if that is really needed, we would need to use a more ribust GREP tool, like GNU GREP.  @newbie27, would that be acceptable?

@echo offset hosts="%windir%\system32\drivers\etc\hosts"call :check host1 1.1.1.1call :check host2 1.1.1.2call :check host3 1.1.1.3exit /b:checkREM Check if entry exists in hosts file.  If it doesn't then echo new value to end of filefindstr /i /r /c:"^ *%2  *%1 " || (  echo %1  %2 >>%hosts%  echo Adding %1  %2 to HOSTS table)
~bp
0
 
LVL 8

Author Comment

by:newbie27
ID: 34111888
ok thanks for your input folks, let me try and get back to you shortly !!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

726 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