Solved

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

Posted on 2010-11-09
6
745 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 51

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 51

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
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). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

708 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

14 Experts available now in Live!

Get 1:1 Help Now