Solved

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

Posted on 2010-11-09
6
749 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
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 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
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). …

911 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

17 Experts available now in Live!

Get 1:1 Help Now