Batch/vbs script to add a new entry in the hosts file when run

Hi,

Batch/vbs script to add a new entry in the hosts file when run

Can anyone help me with a script.

regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
need more info on this one, but essentially:

echo 12.34.45.67  hostname >> %systemroot%\system32\drivers\etc\hosts

should work.

If you want it parameterized:

@echo off
if [%1]==[] goto :usage
if [%2]==[] goto :usage

set ip=%1
set host=%2

echo %ip% %host% >> %systemroot%\system32\drivers\etc\hosts

goto :eof
:usage
echo.  Must supply the IP and hostname as parameters
echo.  Example:  UpdateHost  11.22.33.44  MyNewHost

Open in new window

0
bsharathAuthor Commented:
Hi Sirbounty
Should the code look as this
In the code at the end should i mention the host name and ip as below?
@echo off
if [%1]==[] goto :usage
if [%2]==[] goto :usage

set ip=%1
set host=%2

echo %ip% %host% >> %systemroot%\system32\drivers\etc\hosts

goto :eof
:usage
echo.  Must supply the IP and hostname as parameters
echo.  Example:  UpdateHost  11.22.33.44  MyNewHost
exchangeserver name ipaddress

Open in new window

0
Steve KnightIT ConsultancyCommented:
To save repeating you may want to check with a find if it is there already too:

@echo off
set host=hostname
set ip=1.1.1.1
find "%host" "%systemroot%\system32\drivers\etc\hosts"
if errorlevel 1 (
 echo %Host% not found so adding to HOSTS file:
echo %ip% %host% >> %systemroot%\system32\drivers\etc\hosts
)

Any reason why using hosts and not DNS, or is this for when people are connected to different networks etc?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

bsharathAuthor Commented:
dragon it you are right for other users
I used your code the command prompt does not close it goes on reading
0
bsharathAuthor Commented:
It gets stuck in this line
find "systemroot\system32\drivers\etc\hosts"
0
Steve KnightIT ConsultancyCommented:
sorry I missed a % it should be %host% instead.

steve
0
yehudahaCommented:
hey

just addded the vbs version
doesn't check if the supply arguments all redy in the hosts file
Set objfso = CreateObject("Scripting.FileSystemObject")
strHosts = "C:\WINDOWS\system32\drivers\etc\hosts"
If WScript.Arguments.Count = 0 Then
WScript.Echo  "Must supply the IP and hostname as parameters"
wscript.echo  "Example: " & WScript.ScriptName & " 11.22.33.44  MyNewHost"
WScript.Quit
Else
set objhosts = objfso.OpenTextFile(strHosts,8)
objhosts.WriteLine WScript.Arguments.Item(0) & "    " & WScript.Arguments.Item(1)
objhosts.Close
WScript.Echo "Done"
End If

Open in new window

0
Bill PrewCommented:
Looks like Steve missed a % sign, change the corresponding line to the following:

find "%host%" "%systemroot%\system32\drivers\etc\hosts"

Be aware though that in some versions of Windows the hosts file is protected from update unless running with elevated privileges.

~bp
0
Steve KnightIT ConsultancyCommented:
Thanks Bill, yes it was a typo, I mentioned that above but was holding baby at the time so didn't type much on mobile!

You could run this as a startup script, or other methods but generally speaking users won't have access to change the hosts table themselves (e.g. from a login script) if they aren't admins.

Steve
0
Steve KnightIT ConsultancyCommented:
Cool, glad if it helped.

"Windows Batch Scripting: 497,692 (2,308 more points until Sage)"... so close and yet v quiet today!

Steve
0
Bill PrewCommented:
Well, some of us are just trying to claw our way into the Wizard tier.  Hope you get some viable questions shortly.

~bp
0
bsharathAuthor Commented:
WOW... :-)
0
bsharathAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.