?
Solved

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

Posted on 2010-11-09
6
Medium Priority
?
765 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 1000 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 56

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1000 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 56

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Many times while working on a computer regardless of any Operating System, lag and crashes seem to creep in, hindering your working speed. Sometimes, it can also cause your work to be lost unexpectedly and as a result, you are unable to meet your de…
This is a little timesaver I have been using for setting up Microsoft Small Business Server (SBS) in the simplest possible way. It may not be appropriate for every customer. However, when you get a situation where the person who owns the server is i…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

777 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